Problem-J-完全背包

来源:互联网 发布:linux cp 合并文件夹 编辑:程序博客网 时间:2024/06/07 00:57

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. <br>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.<br>
 

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. <br><br>The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.<br>
 

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.''. <br><br>Output a blank line after each test case.<br>
 

Sample Input
1 0 1 2 0 01 0 0 0 1 10 0 0 0 0 0
 

Sample Output
Collection #1:Can't be divided.Collection #2:Can be divided.

题意:

6种石头,给出每种个数,第i种石头价值i,将石头平均分为两堆,

解题思路:

首先想到如果总数为奇数则can't,然后只要能堆成一堆价值为一半的石头就ok,那么就是一个完全背包,注意通过a[i]%10来优化否则会超时,因为多出的10的倍数个完全可以分为两堆,

代码:

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;inline int max(int &a,int &b){    return a>b?a:b;}#define inf 1000000000int main(){    int a[7],dp[60005];//用dp[i]来存放是否能堆成i    int i,j,k,sum,cas=0;    while(scanf("%d",&a[1])!=EOF)    {        scanf("%d%d%d%d%d",&a[2],&a[3],&a[4],&a[5],&a[6]);        if((a[6]+a[1]+a[2]+a[3]+a[4]+a[5])==0) break;        for(i=1;i<7;i++)            a[i]%=10;  //不用会超时        cas++;        printf("Collection #%d:\n",cas);        sum=a[1];        for(i=2;i<=6;i++)            sum+=a[i]*(i);        if(sum%2!=0) {printf("Can't be divided.\n\n"); continue;}        sum=sum/2;        for(i=0;i<=sum;i++)            dp[i]=0;        dp[0]=1;        for(i=1;i<=6;i++)        {            for(j=1;j<=a[i];j++)            {                for(k=sum;k>=i;k--)                {                    if(dp[k-i]) //只有前一种情况成立,当前情况才成立                    dp[k]=1;                }            }        }        if(dp[sum]==0) printf("Can't be divided.\n");        else printf("Can be divided.\n");        printf("\n");    }    return 0;}

一个很好的代码:http://www.cnblogs.com/zhourongqing/archive/2012/08/01/2619011.html

原创粉丝点击