hdu1059Dividing 多重背包二进制优化

hdu1059



#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int dp[440000],sum,n[7];

int main()
{
	int i,j,k,left,T=1;
	while(scanf("%d%d%d%d%d%d",&n[1],&n[2],&n[3],&n[4],&n[5],&n[6]))
	{
		sum=1*n[1]+2*n[2]+3*n[3]+4*n[4]+5*n[5]+6*n[6];
		if(sum==0)	break;
		printf("Collection #%d:\n",T++);
		memset(dp,-0x3f,sizeof(dp[0])*sum+100);
		dp[0]=0;
		if(sum%2==1){
			printf("Can't be divided.\n\n");
			continue;
		}
		sum/=2;
		for(i=1;i<=6;i++)
		{
			k=1,left=0;
			while(k<=n[i]/2)
			{
				for(j=sum;j>=k*i;j--)
					if(dp[j]<dp[j-k*i]+k*i)	dp[j]=dp[j-k*i]+k*i;
				left+=k;                    //为最后一个数字做准备
				k*=2;                      //double
			}
			k=n[i]-left;
			for(j=sum;j>=k*i;j--)
				if(dp[j]<dp[j-k*i]+k*i)	dp[j]=dp[j-k*i]+k*i;
		}
		
		if(dp[sum]>=0)	printf("Can be divided.\n\n");
		else	printf("Can't be divided.\n\n");
	}
	return 0;
}



你可能感兴趣的:(hdu1059Dividing 多重背包二进制优化)