HDU 1059 Dividing——多重背包变形

Dividing

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


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.

题意:有六种不同价值(下标代表价值)的弹珠,每种价值有一定的数量,问能否将这些总价值均匀的平分给两个人。

题解:多重背包的变形

#include<cstdio>///完全背包变形
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=120010;
int dp[maxn],w[maxn],v[maxn],a[6];
int main()
{
    int ncase=0;
    while(1)
    {
        ncase++;
        int times,sum=0;
        for(int i=1;i<=6;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i]*i;
        }
        if(sum==0) break;
        printf("Collection #%d:\n",ncase);
        if(sum%2){
            printf("Can't be divided.\n\n");
            continue;
        }
        int item=0;
        for(int i=1;i<=6;i++){
            for(int j=1;j<=a[i];j<<=1){
                w[++item]=j*i;
                v[item]=j*i;
                a[i]-=j;
            }
            if(a[i]){
                w[++item]=a[i]*i;
                v[item]=a[i]*i;
            }
        }
        sum/=2;
        memset(dp,0,sizeof(dp));///初始化,可以随便初始化(只要不要初始化成dp[j-w[i]]=j-w[i])
        for(int i=1;i<=item;i++){
            for(int j=sum;j>=w[i];j--)
                dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
        }
        ///在j限制下,取到刚好等于dp[sum]==sum就行
        if(dp[sum]==sum) printf("Can be divided.\n\n");
        else printf("Can't be divided.\n\n");
    }
    return 0;
}

也可以是深搜

#include<cstdio>///深搜
#include<algorithm>
#include<cstring>
using namespace std;
int num[7],flag,sum;
void dfs(int S,int tot)///贪心,首先从最大开始选择,一直贪心,直到选到符合情况。想想为什么可以贪心呢?你想啊,
{                       ///假设既然能平分,那么是不是所有的都会有人选,一个人对于一个物品,那他无非就是选与不选,
    if(S==sum){        ///所以可以贪心
        flag=1;return;
    }
    if(flag==1) return;
    for(int i=6;i>0;i--)///相当于枚举,从最后开始,假设num[i]有n个,那么会递归调用开辟n个空间,每个空间的S值为
    {                  ///从小到大i,2*i,3*i......k*i(1<=k<=n,k*i<=sum)),紧接着又在每个空间跟num[i-1]类似上面的递归调用
        if(num[i]>=1){///在递归过程中只要S的值==sum,就一直返回就好了,很暴力
            if(S+i<=sum) {
                num[i]--;
            dfs(S+i,i);
            if(flag==1) return;
            }
        }
    }
    return;
   /// 1 2 3 0 0 1
}
int main()
{
    int ncase=0;
    while(1)
    {
        ncase++;
         sum=0;
        for(int i=1;i<=6;i++){
            scanf("%d",&num[i]);
            sum+=num[i]*i;
        }
        if(sum==0) break;
        printf("Collection #%d:\n",ncase);
        if(sum%2){
            printf("Can't be divided.\n\n");continue;
        }
        sum/=2;///注意除以2
        flag=0;
        dfs(0,6);
        if(flag) printf("Can be divided.\n\n");
        else printf("Can't be divided.\n\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ljd201724114126/article/details/80453800