题目
分析
首先吃饭越长应该越先开始打饭
安排好顺序后
设 d p [ i ] [ j ] dp[i][j] dp[i][j]表示前 i i i个人,在1号窗口打饭总时间j,最早吃完饭的时间
那么 d p [ i ] [ j + a [ i ] . x ] = min { d p [ i ] [ j + a [ i ] . x ] , m a x ( d p [ i − 1 ] [ j ] , a [ i ] . y + j + a [ i ] . x ) } dp[i][j+a[i].x]=\min\{dp[i][j+a[i].x],max(dp[i-1][j],a[i].y+j+a[i].x)\} dp[i][j+a[i].x]=min{dp[i][j+a[i].x],max(dp[i−1][j],a[i].y+j+a[i].x)}
用滚动数组滚掉第一维
代码
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define rr register
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;
struct rec{int x,y;}a[201];
int n,dp[40101];
inline signed iut(){rr int ans=0; rr char c=getchar();while (!isdigit(c)) c=getchar();while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();return ans;
}
bool cmp(rec a,rec b){return a.y>b.y;}
signed main(){n=iut(); memset(dp,42,sizeof(dp)); dp[0]=0;for (rr int i=1;i<=n;++i) a[i]=(rec){iut(),iut()};sort(a+1,a+1+n,cmp); rr int sum=0,ans=2147483647;for (rr int i=1;i<=n;++i){for (rr int j=sum;~j;--j){dp[j+a[i].x]=min(dp[j+a[i].x],max(dp[j],a[i].y+j+a[i].x));dp[j]=max(dp[j],a[i].y+a[i].x+sum-j);}sum+=a[i].x;}for (rr int i=1;i<=sum;++i) ans=ans<dp[i]?ans:dp[i];return !printf("%d",ans);
}