标签:
http://acm.hdu.edu.cn/showproblem.php?pid=1171
Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 28483 Accepted Submission(s): 10027
Problem Description
Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don‘t know that Computer College had ever been split into Computer College and Software College in 2002.
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).
Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.
A test case starting with a negative integer terminates input and this test case is not to be processed.
Output
For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.
Sample Input
2
10 1
20 1
3
10 1
20 2
30 1
-1
Sample Output
题意:这个题是一个多重背包,b的最大容量为总价值/2;
网上提供的有用母函数或者是纯暴力的方式,将每一种可达状态标记成1,最后找最接近最大容量的值,例如第二个案例 可以只找小于最大容量的可能达到的状态,及10 20 30 40
这种思路应用到背包上面就是将多重背包转化成0 1背包,
•第i件物品可以分为q[i]件物品,价值分别为w[i], 2*w[i], 3*w[i], ……,费用分别为c[i], 2*c[i], 3*c[i], ……。
即把k个物品i合成一个物品
优化
•把k个物品i合成一个物品
•只保留k= 1, 2, 4, …, 2^(p-1), q[i]-2^p+1的物品
•其中p为满足q[i]-2^p+1>0的最大整数
•这样可以保证这些物品的组合能够取到从1到q[i]的所有值且肯定不会超过q[i]
但是多重背包有一个更快的模板
1 #include <cstdio>
2 #include <algorithm>
3 #include <cstring>
4 using namespace std;
5 #define N 55
6
7 int val;
8 int f[N*5000];
9
10 //每件物品只能使用一次
11 void onezeropack(int v,int c)
12 {
13 int j;
14 for(j=val; j>=v; j--)
15 {
16 f[j]=max(f[j-v]+c,f[j]);
17 }
18 }
19 //每件物品可以无限使用
20 void completepack(int v,int c)
21 {
22 int j;
23 for(j=v; j<=val; j++)
24 {
25 f[j]=max(f[j-v]+c,f[j]);
26 }
27 }
28 //每件物品有限次使用
29 void multiplepack(int v,int c,int num)
30 {
31 if(c*num>=val)
32 {
33 completepack(v,c);
34 return;
35 }
36 int k=1;
37 while(k<num)
38 {
39 onezeropack(k*v,k*c);
40 num=num-k;
41 k=k*2;
42 }
43 onezeropack(num*v,num*c);
44 }
45
46
47 int v[N], num[N];
48 int main()
49 {
50 int n;
51 while(~scanf("%d", &n), n >= 0)
52 {
53 for(int i = 0; i < n; i++) scanf("%d %d", &v[i], &num[i]);
54 val = 0;
55 for(int i = 0; i < n; i++) val += v[i]*num[i];
56 int tm = val;
57 val /= 2;
58 memset(f, 0, sizeof(f));
59 for(int i = 0; i < n; i++) multiplepack(v[i], v[i], num[i]);
60 printf("%d %d\n", tm-f[val], f[val]);
61 }
62 return 0;
63 }
Big Event in HDU(多重背包套用模板)
标签:
原文地址:http://www.cnblogs.com/shanyr/p/4674659.html