Luo Gu P2577 lunch linear [greedy] [dp]

Topic : https://www.luogu.org/problemnew/show/P2577

Meaning of the questions : n Everyone has a personal Dafan time and dinner time, they will be divided into two teams. Everyone immediately after the hit rice to eat. Ask how you can make overall arrangements for the shortest time to eat.

Ideas : First, greed is still good think. The total time to eat a team that is actually time to eat + Dafan end of the latest period.

Those who are in front of a team, after dinner if they do not exceed the total time queuing time, do not need to consider them. The team of queuing time if arranged, is fixed. With the order of arrangement it does not matter.

So we should take a long time to eat put forward. Because the queuing time for dinner but not affect the order of time is affected by the order.

Then we can consider how to give them divided into two teams.

First, consider $ dp [i] [j] [k] $ represent the first $ i $ taking into account the individual, the first time the team line up is $ j $ The second team is $ k $ queuing time when the first all eat End the meal time.

However, we found that $ j $ and $ k $ there is a relationship, because the two teams total queuing time is fixed. So we can use the $ sum [i] $ prefix to maintain the queue time and. You can reduce a dimension of.

So we can use $ dp [i] [j] $ represent the first $ i $ taking into account the individual, the first team line up is the $ j $ the first time all meal times. At this time, the second team is clearly queuing time is $ sum [i] - j $

Then there $ dp [i] [j] = min (max (dp [i-1] [j-get [i]], j + eat [i]), max (dp [i - 1] [j], sum [i] - j + eat [i])) $ $ I $ represent the first row in the first team of individuals and teams ranked second.

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<map>
 4 #include<set>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<vector>
 8 #include<cmath> 
 9 #include<stack>
10 #include<queue>
11 #include<iostream>
12 
13 #define inf 0x3f3f3f3f
14 using namespace std;
15 typedef long long LL;
16 typedef pair<int, int> pr;
17 
18 int n;
19 const int maxn = 205;
20 struct node{
21     int get, eat;
22 }stu[maxn];
23 int sum[maxn];
24 int dp[maxn][maxn * maxn * 2];
25 
26 bool cmp(node a, node b)
27 {
28     return a.eat > b.eat;
29 }
30 
31 int main()
32 {
33     scanf("%d", &n);
34     memset(dp, 0x3f, sizeof(dp));
35     for(int i = 1; i <= n; i++){
36         scanf("%d%d", &stu[i].get, &stu[i].eat);
37     }
38     sort(stu + 1, stu + 1 + n, cmp);
39     
40     for(int i = 1; i <= n; i++){
41         sum[i] = sum[i - 1] + stu[i].get;
42     }
43     dp[0][0] = 0;
44     for(int i = 1; i <= n; i++){
45         for(int j = 0; j <= sum[i]; j++){
46             if(j >= stu[i].get)dp[i][j] = min(dp[i][j], max(dp[i - 1][j - stu[i].get], j + stu[i].eat));
47             dp[i][j] = min(dp[i][j], max(dp[i - 1][j], sum[i] - j + stu[i].eat));
48         }
49     }
50     
51     int ans = inf;
52     for(int j = 0; j <= sum[n]; j++){
53         ans = min(ans, dp[n][j]);
54     }
55     printf("%d\n", ans);
56     return 0;
57 }

 

Guess you like

Origin www.cnblogs.com/wyboooo/p/11106723.html