hdu 1059 (多重背包) Dividing

这里;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 }
时间: 2024-11-03 22:11:17

hdu 1059 (多重背包) Dividing的相关文章

HDU 1059 多重背包+二进制优化

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16909    Accepted Submission(s): 4729 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

hdu 1059 多重背包

题意:价值分别为1,2,3,4,5,6的物品个数分别为a[1],a[2],a[3],a[4],a[5],a[6],问能不能分成两堆价值相等的. 解法:转化成多重背包 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<iostream> 5 using namespace std; 6 7 int dp[120010]; 8 int a[10]; 9 10 int

hdu 1171 Big Event in HDU(母函数|多重背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意:有n种物品,给出每种物品的价值和数目,要将这些物品尽可能的分成相等的两份A和B且A>=B ,输出A,B. 母函数可以过,但感觉最直接的方法应该是多重背包. 母函数的话,也是按总价值的一半求,从一半到小枚举,直到找到系数不为0的就是B. #include <stdio.h> #include <iostream> #include <map> #include <

hdu 5445 多重背包

Food Problem Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1243    Accepted Submission(s): 368 Problem Description Few days before a game of orienteering, Bell came to a mathematician to sol

hdu 2191 多重背包 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活

http://acm.hdu.edu.cn/showproblem.php?pid=2191 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的选拔 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17930    Accepted

HDU1171--Big Event in HDU(多重背包)

Big Event in HDU   Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1139 Accepted Submission(s): 444 Problem Description Nowadays, we all know that Computer College is the biggest department in HD

Big Event in HDU(多重背包套用模板)

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

HDU 2191 多重背包

悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 22865    Accepted Submission(s): 9661 Problem Description 急!灾区的食物依然短缺!为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市

hdu 2191 多重背包,自己写模板

背景:wa了几次,都是小失误:把i--写成i++之类的,写的时候一定要想到具体用意.还有就是一定要至少写三组测试数据!!!!!!! 学习:模板化写多重背包. #include<cstdio> #include<iostream> #include<cstring> using namespace std; int t,v,n; int c[109],w[109],num[109],F[109]; void zeroonebag(int cost,int weight){