这里;http://acm.hdu.edu.cn/showproblem.php?pid=1059

题意是有价值分别为1,2,3,4,5,6的商品各若干个,给出每种商品的数量,问是否能够分成价值相等的两份.

联想到多重背包,稍微用二进制优化一下。(最近身体不适,压力山大啊)

 

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #define inf 70000
 5 using namespace std;
 6 int dp[inf];
 7 int sum;
 8 void pack(int price)
 9 {
10     for(int i = sum; i >= price; i--) dp[i] = max(dp[i], dp[i - price] + price);
11 }
12 int main()
13 {
14     int a[8],i,q=1,j;
15     while (~scanf("%d %d %d %d %d %d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]))
16     {
17         if (a[1]==0&&a[2]==0&&a[3]==0&&a[4]==0&&a[5]==0&&a[6]==0)
18             break;
19         memset(dp,-inf,sizeof(dp));
20         dp[0]=0;
21         printf("Collection #%d:\n",q++);
22         sum=0;
23         for (i=1;i<=6;i++)
24             sum+=a[i]*i;
25         if (sum%2!=0)
26         {
27             printf("Can't be divided.\n\n");
28             continue;
29         }
30         sum/=2;
31         for (i=1;i<=6;i++)
32         {
33             if (i*a[i]>=sum)
34             {
35                 for (j=i;j<=sum;j++)
36                     dp[j]=max(dp[j],dp[j-i]+i);
37             }
38             else
39             {
40                 int k = 1;
41                 while(k < a[i])
42                 {
43                    pack(k * i);
44                    a[i] -= k;
45                    k += k;
46                 }
47                 pack(a[i] * i);
48             }
49         }
50         if (dp[sum]==sum)
51             printf("Can be divided.\n\n");
52         else
53             printf("Can't be divided.\n\n");
54     }
55     return 0;
56 }

 

posted on 2015-10-19 21:13  蜘蛛侦探  阅读(163)  评论(0编辑  收藏  举报