hdu1171 Big Event in HDU(多重背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1171

多重背包题目不难,但是有些点不能漏或错。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cstdlib>
 6 #include<cmath> 
 7 #define lson l, m, rt<<1
 8 #define rson m+1, r, rt<<1|1
 9 #define IO ios::sync_with_stdio(false);cin.tie(0);
10 #define INF 1e9
11 typedef long long ll;
12 using namespace std;
13 int n, a[100010], b[100010], dp[500010];
14 int main()
15 {
16     IO;
17     while(cin >> n){
18         memset(dp, 0, sizeof(dp)); //不要忘了初始化
19         if(n < 0) break;
20         int sum = 0;
21         for(int i = 0; i < n; i++){
22             cin >> a[i] >> b[i];
23             sum += a[i]*b[i];
24         }
25         int half=sum/2;
26         for(int i = 0; i < n; i++){
27             for(int k = 0; k < b[i]; k++){//多重背包循环是在这里,而不是下面 
28                 for(int j = half; j >= a[i]; j--){//sum会超时
29                     dp[j] = max(dp[j], dp[j-a[i]]+a[i]);
30                 }
31             }
32         }
33         cout << sum-dp[half] << " " << dp[half] << endl;
34     }
35     return 0;
36 }

 

posted @ 2018-04-19 22:54  Surprisez  阅读(136)  评论(0编辑  收藏  举报