HDU 1059 Dividing(多重背包)
Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9533 Accepted Submission(s): 2596
Problem Description
Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value.
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
Input
Each line in the input describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., n6, where ni is the number of marbles of value i. So, the example from above would be described by the input-line ``1 0 1 2 0 0''. The maximum total number of marbles will be 20000.
The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
Output
For each colletcion, output ``Collection #k:'', where k is the number of the test case, and then either ``Can be divided.'' or ``Can't be divided.''.
Output a blank line after each test case.
Output a blank line after each test case.
Sample Input
1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0
Sample Output
Collection #1:
Can't be divided.
Collection #2:
Can be divided.
Source
Recommend
JGShining
经典的多重背包问题,二进制思想优化
题意:Marsha和Bill收集了很多的石子,他们想把它们平均分成两堆,但不幸的是,有的石子大且美观,有的就没有。于是,他们就给这些石子从1到6编号,表示石子的价值,以便他们可以按照价值平分,但是他们也意识到这很难,因为石子是不能切割的,石子的总价值也未必是偶数,就算是偶数也不一定能平分,例如,他们分别有一个价值为1和3的石子,2个价值为4的石子,这种情况下,就不能均分了。
分析:多重背包
#include<iostream> using namespace std; int dp[120010]; int m; int Max(int x,int y) { return x>y?x:y; } void ZeroOnePack(int c,int w)//01背包 { for(int i=m;i>=c;i--) dp[i]=Max(dp[i],dp[i-c]+w); } void CompletePack(int c,int w)//完全背包 { for(int i=c;i<=m;i++) dp[i]=Max(dp[i],dp[i-c]+w); } void MutiplePack(int c,int w,int p)//多重背包 { if(c*p>=m) CompletePack(c,w); else { for(int i=1;i<=p;i*=2) { ZeroOnePack(i*c,i*w); p-=i; } ZeroOnePack(p*c,p*w); } } int main() { int i,sum,total=0; int n[8]; while(1) { sum=0; for(i=1;i<=6;i++) { scanf("%d",&n[i]); n[i]%=10;//加上这句时间从800多MS降到0MS sum+=i*n[i]; } if(!sum) break; printf("Collection #%d:\n",++total); if(sum%2)//sum为奇数,不能平分 { printf("Can't be divided.\n\n"); continue; } m=sum/2; for(i=0;i<=m;i++) dp[i]=0; for(i=1;i<=6;i++) MutiplePack(i,i,n[i]);//重量用价值替代 if(dp[m]==m)//若两个人分得的价值都等于总价值的一半,则可以平分 printf("Can be divided.\n\n"); else printf("Can't be divided.\n\n"); } return 0; }