Big Event in HDU

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16449    Accepted Submission(s): 5809


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
20 10 40 40
题意:
  已知有N种价值的物品,每种价值为v,有m件,现将物品分为两部分,使每部分价值尽量相等(如果不等则使第一部分大于第二部分)。
 
代码一:多重背包的做法
背包问题的初始化:
  求最大价值:要求恰好装满背包,那么在初始化时除了dp[0]为0其它dp[1..V]均设为-∞ 
  求最小价值:要求恰好装满背包,那么在初始化时除了dp[0]为0其它dp[1..V]均设为∞
 1 #include <cstdio>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 struct goods
 7 {
 8     int val;
 9     int cnt;
10 }bag[55];
11 int dp[130000];
12 
13 int max(int a, int b)
14 {
15     return a > b ? a : b;
16 }
17 
18 int MultiplePack(int sum, int n)
19 {
20     for(int i = 0; i < n; ++i)
21     {
22         // 我发现如果下边不做判断直接将其都 转化为 01 背包也能 AC,应该也是对的吧 
23         if(bag[i].val * bag[i].cnt >= sum)//等同与完全背包
24         {
25             for(int j = bag[i].val; j <= sum; ++j)
26                 dp[j] = max(dp[j], dp[j-bag[i].val] + bag[i].val);
27         }
28         else     //转化为 0-1 背包,用二进制打包 
29         {
30             int k = 1;
31             while(bag[i].cnt >= k)
32             {
33                 for(int j = sum; j >= bag[i].val*k; --j)
34                     dp[j] = max(dp[j], dp[j-bag[i].val*k] + bag[i].val*k);
35                 bag[i].cnt -= k;
36                 k <<= 1;
37             }
38             for(int j = sum; j >= bag[i].val*bag[i].cnt; --j)
39                 dp[j] = max(dp[j], dp[j-bag[i].val*bag[i].cnt] + bag[i].val*bag[i].cnt);
40         }
41     }
42     return dp[sum];
43 }
44 
45 int main()
46 {
47     int n, sum, t;
48     while(scanf("%d", &n))
49     {
50         if(n < 0)
51             break;
52         sum = 0;
53         memset(dp, 0, sizeof(dp));
54         for(int i = 0; i < n; ++i)
55         {
56             scanf("%d%d", &bag[i].val, &bag[i].cnt);
57             sum += bag[i].val * bag[i].cnt;
58         }
59         t = MultiplePack(sum/2, n);
60         if((t<<1) > sum)
61             printf("%d %d\n", t, sum-t);
62         else
63             printf("%d %d\n", sum-t, t);
64     }
65     return 0;
66 }

代码二:母函数做法

 1 #include <cstdio>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 struct goods
 7 {
 8     int val;
 9     int cnt;
10 }bag[55];
11 int c1[130000], c2[130000];
12 
13 int main()
14 {
15     int n, sum, t;
16     while(scanf("%d", &n))
17     {
18         if(n < 0)
19             break;
20         sum = 0;
21         for(int i = 0; i < n; ++i)
22         {
23             scanf("%d%d", &bag[i].val, &bag[i].cnt);
24             sum += bag[i].val * bag[i].cnt;
25         }
26         memset(c1, 0, sizeof(c1));
27         memset(c2, 0, sizeof(c2));
28         for(int i = 0; i <= bag[0].val*bag[0].cnt; i += bag[0].val)
29             c1[i] = 1;
30         for(int i = 2; i <= n; ++i)
31         {
32             for(int j = 0; j <= sum/2; ++j)
33             {
34                 for(int k = 0; k <= bag[i-1].val*bag[i-1].cnt; k += bag[i-1].val)
35                     c2[j+k] += c1[j];
36             }
37             for(int j = 0; j <= sum/2; ++j)
38             {
39                 c1[j] = c2[j];
40                 c2[j] = 0;
41             }
42         }
43         for(int i = sum/2; i >= 0; --i)//找到最接近于 sum/2 的分法 
44         {
45             if(c1[i])
46             {
47                 printf("%d %d\n", sum-i, i);
48                 break;
49             }
50         }
51     }
52     return 0;
53 } 

 

 
posted on 2013-04-27 16:29  可笑痴狂  阅读(326)  评论(0编辑  收藏  举报