hdu1059 Dividing(完全背包)

Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20080    Accepted Submission(s): 5631


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.
 

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.
 

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.
 

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
Mid-Central European Regional Contest 1999
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:   1114  1025  2191  1024  1081 
 

这两天家里一直停电 刚刚来电一会儿   都烦死了、一到夏天就停我们农村的电难过

闲话不多说。这道题和nyoj546(第五届河南省程序设计大赛)这道题一样,只不过宝珠变为了6个。以前在nyoj上AC了,今天同样的方法用在杭电上发现

以前的方法是错误的,也说明了nyoj的数据太弱了。。。以前的思路也是错误的。

这道题的思路就是计算出所有宝珠的价值,然后把总价值除以2。转换成完全背包的思想。如果你对背包问题还不懂。百度搜搜背包九讲把 

其实背包做的多了也就慢慢理解了。。唉!

这里还有一道完全背包的题。点击打开链接

看代码把:

#include <stdio.h>
#include <string.h>
int main()
{
    int ncase=1,allsum,dp[60005],num[6];
    while(1)
    {
        allsum=0;
        for(int i=0;i<6;i++)
        {
        	scanf("%d",&num[i]);
			allsum+=num[i]*(i+1);
        }
        if(allsum==0)
        break;
        if(allsum%2)
        {
            printf("Collection #%d:\nCan't be divided.\n\n",ncase++);
            continue;
        }
        else
        {
			memset(dp,0,sizeof(dp));
			for(int i=0;i<=num[0];i++)//节省点时间、、
			dp[i]=1;
            for(int i=1;i<6;i++)
            {
            	if(num[i])
            	for(int j=allsum/2;j>=0;j--)//一定要倒着来哦。。如果你不信 可以自己换个方向。输出结果看看0.0我刚刚试过
				{
					if(dp[j])
					{
						for(int k=1;k<=num[i];k++)
						{
							if(j+k*(i+1)>allsum/2||dp[j+k*(i+1)]) break;
							dp[j+k*(i+1)]=1;
						}
					}
				}
            }
            if(dp[allsum/2])
            printf("Collection #%d:\nCan be divided.\n\n",ncase++);
            else
            printf("Collection #%d:\nCan't be divided.\n\n",ncase++);
        }
    }
    return 0;
}


你可能感兴趣的:(HDU,完全背包,1059,hdu1059)