Nick has n bottles of soda left after his birthday. Each bottle is described by two values: remaining amount of soda ai and bottle volumebi (ai ≤ bi).
Nick has decided to pour all remaining soda into minimal number of bottles, moreover he has to do it as soon as possible. Nick spends xseconds to pour x units of soda from one bottle to another.
Nick asks you to help him to determine k — the minimal number of bottles to store all remaining soda and t — the minimal time to pour soda into k bottles. A bottle can't store more soda than its volume. All remaining soda should be saved.
The first line contains positive integer n (1 ≤ n ≤ 100) — the number of bottles.
The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 100), where ai is the amount of soda remaining in the i-th bottle.
The third line contains n positive integers b1, b2, ..., bn (1 ≤ bi ≤ 100), where bi is the volume of the i-th bottle.
It is guaranteed that ai ≤ bi for any i.
The only line should contain two integers k and t, where k is the minimal number of bottles that can store all the soda and t is the minimal time to pour the soda into k bottles.
4
3 3 4 3
4 7 6 5
2 6
2
1 1
100 100
1 1
5
10 30 5 6 24
10 41 7 8 24
3 11
In the first example Nick can pour soda from the first bottle to the second bottle. It will take 3 seconds. After it the second bottle will contain 3 + 3 = 6 units of soda. Then he can pour soda from the fourth bottle to the second bottle and to the third bottle: one unit to the second and two units to the third. It will take 1 + 2 = 3 seconds. So, all the soda will be in two bottles and he will spend 3 + 3 = 6seconds to do it.
思路:dp
可以先将最小的杯子数求出来。
dp[i][j][k]表示前i个杯子,容量为j时,选了k个的最大剩余水量,然后状态转移看代码;
1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 #include<stdlib.h> 6 #include<queue> 7 #include<set> 8 #include<vector> 9 #include<map> 10 #include<stack> 11 #include<deque> 12 using namespace std; 13 typedef long long LL; 14 int ans[1005]; 15 int id[1005]; 16 typedef struct node 17 { 18 int x; 19 int y; 20 bool operator<(const node&cx)const 21 { 22 if(y == cx.y)return cx.x > x; 23 else return y < cx.y; 24 } 25 } ss; 26 priority_queue<ss>que; 27 bool cmp(node p,node q) 28 { 29 return p.y < q.y; 30 } 31 ss ak[10005]; 32 int dp[105][10005][105]; 33 int main(void) 34 { 35 int n,m; 36 scanf("%d",&n); 37 int i,j; 38 int sum = 0; 39 int uu ; 40 for(i = 1; i <= n; i++) 41 { 42 scanf("%d",&ak[i].x); 43 sum+=ak[i].x; 44 } 45 uu = sum; 46 int vv = 0; 47 for(i = 1; i <= n; i++) 48 { 49 scanf("%d",&ak[i].y); 50 vv+=ak[i].y; 51 } 52 sort(ak+1,ak+n+1,cmp); 53 int s; 54 for(i = 0; i < 105; i++) 55 { 56 for(j = 0; j <= 10000; j++) 57 { 58 for(s = 0; s < 105; s++) 59 { 60 dp[i][j][s] = -1e9; 61 } 62 } 63 } 64 for(i = 0; i < 105; i++) 65 { 66 67 dp[i][0][0]=0; 68 } 69 int cn = 0; 70 int nn = n; 71 while(sum > 0) 72 { 73 sum -= ak[nn].y; 74 cn++; 75 nn--; 76 } 77 int maxx = 0; 78 for(i = 1; i <= n; i++) 79 { 80 for(s = vv; s >= ak[i].y; s--) 81 { 82 for(j = cn; j >= 1; j--) 83 { 84 dp[i][s][j] = max(dp[i][s][j],dp[i-1][s][j]); 85 dp[i][s][j] = max(dp[i][s][j],dp[i-1][s-ak[i].y][j-1]+ak[i].x); 86 if(j == cn&&s >= uu) 87 maxx = max(maxx,dp[i][s][j]); 88 } 89 } 90 for(s = ak[i].y-1; s >= 0; s--) 91 { 92 for(j = cn ; j >= 1; j--) 93 { 94 dp[i][s][j] =max(dp[i-1][s][j],dp[i][s][j]); 95 if(j == cn&&s >= uu) 96 maxx = max(maxx,dp[i][s][j]); 97 } 98 } 99 } 100 printf("%d %d ",cn,uu-maxx); 101 return 0; 102 }