ACM 1014 Dividing 背包问题

Dividing
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 54590   Accepted: 13973

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.

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.

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 Code

Problem: 1014		
Memory: 488K		Time: 16MS
Language: C++		Result: Accepted
Source Code
#include <iostream>

using namespace std;
const int MAX=120000;
const int len=7;
int f[MAX];
int n[len];
int V;
const int init=-9999;
int max(int a,int b)
{
    return a>b?a:b;
}
void zeroOnePack(int cw)
{
    for(int v=V;v>=cw;v--)
    f[v]=max(f[v],f[v-cw]+cw);
}
void completePack(int cw)
{
    for(int v=cw;v<=V;v++)
    f[v]=max(f[v],f[v-cw]+cw);
}
void multiplePack(int cw,int amount)
{
    if(cw*amount>V)
    {
        completePack(cw);
        return;
    }
    int k=1;
    while(k<amount)
    {
        zeroOnePack(k*cw);
        amount=amount-k;
        k=k*2;
    }
    zeroOnePack(amount*cw);
}
int main()
{
    int count=0;
    while(1)
    {
        count++;
        int i;
        int bin=0;
        int sum=0;
        for(i=1;i<len;i++)
        {cin>>n[i]; bin|=n[i];sum+=i*n[i];}
        if(bin==0) break;
        if(sum%2!=0)
        cout<<"Collection #"<<count<<":\n"<<"Can't be divided."<<endl<<endl;
        else
        {
            sum/=2;
            V=sum;
            f[0]=0;
            for(i=1;i<=V;i++)
                f[i]=init;
            for(i=1;i<len;i++)
            multiplePack(i,n[i]);
            if(f[V]!=V)
             cout<<"Collection #"<<count<<":\n"<<"Can't be divided."<<endl<<endl;
            else
             cout<<"Collection #"<<count<<":\n"<<"Can be divided."<<endl<<endl;
        }

    }
    return 0;
}


你可能感兴趣的:(C++,算法,ACM)