P2577 [ZJOI2005]午餐

)逼着自己做DP

题意:

  有n个人打饭,每个人都有打饭时间和吃饭时间。有两个打饭窗口,问如何安排可以使得总用时最少。

思路:

  1)可以发现吃饭时间最长的要先打饭。(我也是看别人题解才知道)

  2)然后就是对于前i个人,他不是在一号窗口打饭,就是在二号窗口打饭。所以用dp[i][j]表示前i个人,在一号窗口打饭j时间的总用时。因为dp[i][k = sum - j] 就表示前i个人在二号窗口用时k的总用时。

  

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/ const int maxn = ;
int dp[maxn][maxn*maxn],sum[maxn];
struct node
{
int t,w;
}a[maxn];
bool cmp(node a,node b){
return a.w > b.w;
} int main(){
int n;
scanf("%d", &n);
for(int i=; i<=n; i++){
scanf("%d%d", &a[i].t, &a[i].w);
}
sort(a+,a++n,cmp);
for(int i=; i<=n; i++) sum[i] = sum[i-] + a[i].t;
memset(dp, inf, sizeof(dp));
dp[][] = ;
for(int i=; i<=n; i++){
for(int j=sum[i]; j>=; j--){
if(j >= a[i].t)dp[i][j] = min(dp[i][j], max(dp[i-][j-a[i].t], j + a[i].w));
dp[i][j] = min(dp[i][j], max(dp[i-][j], sum[i] - j + a[i].w));
}
}
int ans = inf;
for(int i=; i<= sum[n]; i++) ans = min(ans, dp[n][i]);
printf("%d\n", ans);
return ;
}

最新文章

  1. 【.net 深呼吸】设置序列化中的最大数据量
  2. JAVA通信系列二:mina入门总结
  3. 深入理解CSS3 Animation 帧动画
  4. 关于一道数据库例题的解析。为什么σ age&gt;22 (πS_ID,SCORE (SC) ) 选项是错的?
  5. iOS实现屏幕旋转
  6. linux入门级常用命令
  7. YCbCr 编码格式(YUV)---转自Crazy Bingo的博客
  8. wp7 xml
  9. zabbix命令:zabbix_get获取item数据
  10. 加上固件密码,Mac更安全
  11. [Cycle.js] Read effects from the DOM: click events
  12. bootstrap使用中遇到的问题(二)
  13. latex编辑器
  14. ST-LINK V2 DIY笔记(一)
  15. Vmware ESXi日志文件存放目录地址
  16. Vue-cli 搭建web服务介绍
  17. 开启Centos系统的SSH服务
  18. jquery滚动事件
  19. 模拟jQuery中的ready方法及实现按需加载css,js
  20. 教你调用数据库读取短信 记事本 通讯录文件,让ios5的短信恢复到ios4

热门文章

  1. windows下用easybcd引导ubuntu出现grub的解决方案
  2. python_0基础开始_day03
  3. Docker——理解好镜像和容器的关系
  4. CentOS7.6源码编译安装PHP 7.3.8
  5. C#的委托事件总结
  6. 如何为 caddy 添写自定义插件
  7. 理解MySQL(一)--MySQL介绍
  8. Streaming+Sparksql使用sql实时分析 rabbitmq+mongodb+hive
  9. c# 三步递交模式调用同一个存储过程
  10. SAP无法激活表问题