Luo Gu P2577 [ZJOI2005] lunch explanations

Daily questions day56 punch

Analysis

Algorithms: greedy + dp

Greedy easy to think: eat slower to Dafan save time, so people first by dinner time descending order.

Then is the dp: First, we should think of f [i] [j] [k]: before i individual, No. 1 in the window Dafan total time j, the No. 2 overall time window Dafan k

Of course, this space will burst, so expect to remove one-dimensional.

f [i] [j] denotes the front i individuals, No. 1 Dafan total time window j, the first time the meal

We find it convenient that j + k = i Dafan individual sum, k = sum (i) -j. It is possible to maintain a prefix and.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define maxn 200+10
 6 #define INF 2147483647
 7 #define rep(i,s,e) for(register int i=s;i<=e;++i)
 8 #define dwn(i,s,e) for(register int i=s;i>=e;--i)
 9 using namespace std;
10 inline int read()
11 {
12     int x=0,f=1;
13     char c=getchar();
14     while(c<'0'||c>'9') {if(c=='-') f=-1; c=getchar();}
15     while(c>='0'&&c<='9') {x=x*10+c-'0'; c=getchar();}
16     return f*x;
17 }
18 inline void write(int x)
19 {
20     if(x<0) {putchar('-'); x=-x;}
21     if(x>9) write(x/10);
22     putchar(x%10+'0');
23 }
24 int n;
25 int sum[maxn];
26 int dp[maxn][maxn*maxn];
27 struct node
28 {
29     int a,b;
30 }lunch[maxn];
31 bool cmp(node x,node y)
32 {
33     return x.b>y.b;
34 }
35 int main()
36 {
37     n=read();
38     rep(i,1,n) {lunch[i].a=read();lunch[i].b=read();}
39     sort(lunch+1,lunch+n+1,cmp);
40     rep(i,1,n) sum[i]=sum[i-1]+lunch[i].a;
41     memset(dp,127,sizeof(dp));
42     dp[0][0]=0;
43     rep(i,1,n)
44         rep(j,0,sum[i])
45         {
46             if(j>=lunch[i].a) dp[i][j]=min(dp[i][j],max(dp[i-1][j-lunch[i].a],j+lunch[i].b));
47             if(sum[i]-j>=lunch[i].a) dp[i][j]=min(dp[i][j],max(dp[i-1][j],sum[i]-j+lunch[i].b));
48         }
49     int ans=INF;
50     rep(i,0,sum[n]) ans=min(ans,dp[n][i]);
51     write(ans);
52     return 0;
53 }

Please Gangster treatise(Anyway, I do not know what that means treatise)

Guess you like

Origin www.cnblogs.com/handsome-zyc/p/12023999.html