Dividing
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 71505 | Accepted: 18647 |
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.
Input
Each line in the input file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , 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 collection, 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
题目大意:有六种石头,给你各种石头的个数,每种石头的价值就是其输入的顺序,求是否有一种方法可以分这些石头为两个相等的部分。
解题思路:
本来用多重背包写的,然后超时了。。。查找相关代码却都是说用深搜写,我在练习动规耶。。所以就找了一个大神的01背包的代码参谋。
以总价值的一半为下标的上界(总价值可被2乘除时),待dp完成后查看,查当容量为总价格的一半的时候,是否存在可取的石头数。
动规方程为:dp[j]=max(dp[j],dp[j-c[i]]+w[i])
其中w[i]表示组成c[i]价值需要 多少块石头
AC代码
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int n,w[20005],p,m,c[20005],dp[120005];//c[]表示石头的价值 ,w[i]表示组成c[i]价值需要 多少块石头
int main()
{
int sum,i,j,k,t=0;
while(1)
{
sum=m=n=0;
for(i=0;i<6;i++)
{
scanf("%d",&p);
n+=p;
sum+=p*(i+1);
k=1;
while(k<p)
{
c[m]=(i+1)*k;
w[m]=k;//printf("p=%d c[%d]=%d w[%d]=%d\n",p,m,c[m],m,w[m]);
m++;
p=p-k;
k=k*2;
}
if(p!=0)
{
c[m]=(i+1)*p;
w[m]=p;
// printf("p=%d c[%d]=%d w[%d]=%d\n",p,m,c[m],m,w[m]);
m++;
}
}
if(n==0) break;
if(sum%2!=0)
{
printf("Collection #%d:\nCan't be divided.\n",++t);
}
else
{
sum=sum/2;
memset(dp,-1,sizeof(dp));
dp[0]=0;
for(i=0;i<m;i++)
for(j=sum;j>=c[i];j--)
if(dp[j-c[i]]>=0)//如果j-c[i]的价值可以有石头组成,那么就可以把c[i]这块石头放进去使价值可以达到j
{
dp[j]=max(dp[j],dp[j-c[i]]+w[i]);
//printf("dp[%d]=%d\n",j,dp[j]);
}
if(dp[sum]>=0)
printf("Collection #%d:\nCan be divided.\n",++t);
else
printf("Collection #%d:\nCan't be divided.\n",++t);
}
printf("\n");
}
return 0;
}