HDU1059 多重背包 多重部分和问题DP

来源:互联网 发布:有个腿长的女朋友知乎 编辑:程序博客网 时间:2024/04/30 22:56

Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20691 Accepted Submission(s): 5827

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

这里有两种做法,其一,可以抽象成多重背包。 。当初直接当做01直接超时了。 。

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#define MAX_PATH 200000+10using namespace std;int dp[MAX_PATH];int li[7];int sum;void CoP(int value){    for(int i = value;i<=sum;i++)        dp[i] = max(dp[i],dp[i-value]+value);}void ZOP(int value){    for(int i = sum;i>=value;i--)        dp[i] = max(dp[i],dp[i-value]+value);}void MP(int value,int number){    if(value*number>=sum)        CoP(value);    else{        int k = 1;        while(k<number){            ZOP(k*value);            number -= k;            k<<=1;        }        ZOP(number*value);    }}int main(){//    freopen("data.in","r",stdin);    int i,j,k,CNT = 1;    while(scanf("%d%d%d%d%d%d",&li[1],&li[2],&li[3],&li[4],&li[5],&li[6])){        int n;        sum = 0;        j = 0;        for(i = 1;i<=6;i++)            if(li[i])                sum += i*li[i];        if(!sum)            break;        memset(dp,0,sizeof(dp));        printf("Collection #%d:\n",CNT++);        if(sum%2)            printf("Can't be divided.\n\n");        else{            sum/=2;            for(i = 1;i<=6;i++)                MP(i,li[i]);            if(dp[sum] == sum)                printf("Can be divided.\n\n");            else                printf("Can't be divided.\n\n");        }    }    return 0;}

其二,对于这种多重部分和的问题,其实有更好的解决方法,定义dp[i+1][j]为用前i个数相加得j时 第i种数还剩多少个。这样的话,如果dp[i][j]>=0,那么dp[i+1][j] = li[i],即第i种数一个没用。如果不可达的话dp[i+1][j] = -1,其余情况我们抽一个数i刚好可以达到的话,转移方程就是dp[i+1][j] = dp[i+1][j-i]-1.等于从dp[i+1][j-i]里再用了一个i。所以减一。最后判断dp[sum/2]>=0就可以了。这个做法可以在O(nk)时间内算出结果。在oj中用时109ms,而上面的多重背包做法用了748ms做法。

#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;int dp[200000];int li[7];int sum;int main(){//    freopen("data.in","r",stdin);    int i,j,k,CNT = 1;    while(scanf("%d%d%d%d%d%d",&li[1],&li[2],&li[3],&li[4],&li[5],&li[6])){        sum = 0;        for(i = 1;i<=6;i++)            if(li[i])                sum += i*li[i];        if(!sum)            break;        memset(dp,-1,sizeof(dp));        dp[0] = 0;        printf("Collection #%d:\n",CNT++);        if(sum%2)            printf("Can't be divided.\n\n");        else{            sum/=2;            for(i = 1;i<=6;i++)                for(j = 0;j<=sum;j++)                    if(dp[j]>=0)                        dp[j] = li[i];                    else if(j<i||dp[j-i]<=0)                        dp[j] = -1;                    else                        dp[j] = dp[j-i] - 1;            if(dp[sum] >= 0)                printf("Can be divided.\n\n");            else                printf("Can't be divided.\n\n");        }    }    return 0;}
0 0
原创粉丝点击