【多重背包+二进制优化】POJ 1014 Dividing

http://poj.org/problem?id=1014

Dividing

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 75632   Accepted: 19825

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

Mid-Central European Regional Contest 1999

以下代码 Presentaion Error 格式错误,不知道错在哪儿了

#include 
#include 
#include 
using namespace std;
const int nmax=61000;

int c[10];//记录读入的6个数字 
int dp[nmax];
//本题中int v[i]和w[i]都是i; 
int weight[nmax];
int value[nmax];

int main(int argc, char** argv) {
	int coll_cnt=1;
	int sum;
	while(cin>>c[1]>>c[2]>>c[3]>>c[4]>>c[5]>>c[6]){
		memset(weight,0,sizeof(weight));
		memset(value,0,sizeof(value));
		if(c[1]+c[2]+c[3]+c[4]+c[5]+c[6]==0){
		//if(c[1]==0&&c[2]==0&&c[3]==0&&c[4]==0&&c[5]==0&&c[6]==0){
			break;
		}
		sum=c[1]*1+c[2]*2+c[3]*3+c[4]*4+c[5]*5+c[6]*6;//计算sum巧妙 
		printf("Collection #%d:\n",coll_cnt);
		coll_cnt++;
		if(sum%2==1){
			printf("Can't be divided.\n");
		    continue;
		}
		sum/=2;
	    //------------多重背包+二进制优化---------------
	    int cnt=0;
	    for(int i=1;i<=6;i++){//遍历6个c[i] 
	        //对每一种类的c[i]件物品进行二进制分解
		    for(int j=1;j<=c[i];j++){
			    //v[i]和w[i]默认都是i,
			    value[cnt]=j*i;
			    weight[cnt]=j*i; 
			    cnt++;
		    	c[i]-=j;
		    }
		    if(c[i]>0){
			    //v[i]和w[i]默认都是i,
			    value[cnt]=c[i]*i;
				weight[cnt]=c[i]*i;
				cnt++;
			}
		}
		//------------转换为01背包解决---------------
		memset(dp,0,sizeof(dp));
		for(int i=0;i=weight[i];j--){
		    	dp[j]=max(dp[j],dp[j-weight[i]]+value[i]);
			}
	   	}
		
		if(dp[sum]!=sum){
			printf("Can't be divided.\n"); 
		}else{
		    printf("Can be divided.\n"); 
		}
	} 
	return 0;
}

 

下面是AC的

#include
#include
#include
using namespace std;
 
int a[10],sum;
int str[1000],pos;
int dp[61000];
 
int main()
{
    int coun=1;
    while(scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6])!=EOF)
    {
        if(a[1]+a[2]+a[3]+a[4]+a[5]+a[6]==0)
            break;
        sum=(a[1]*1+a[2]*2+a[3]*3+a[4]*4+a[5]*5+a[6]*6);
        printf("Collection #%d:\n",coun++);
        if(sum%2==1){
            puts("Can't be divided.\n");
            continue;
        }
        sum/=2;
 
//-------------------------二进制优化------------------------------------------//
        pos=0;
        for(int i=1;i<=6;i++){
            for(int j=1;j<=a[i];j<<=1){
                str[pos++]=j*i;
                a[i]-=j;
            }
 
            if(a[i]>0)
                str[pos++]=a[i]*i;
        }
//---------------------------0 1背包-------------------------------------------//
        memset(dp,0,sizeof(dp));
        for(int i=0;i=str[i];j--)
                dp[j]=max(dp[j],dp[j-str[i]]+str[i]);
 
 
        if(dp[sum]!=sum)
            puts("Can't be divided.\n");
        else
            puts("Can be divided.\n");
    }
    return 0;
}

 

你可能感兴趣的:(机试)