Dividing POJ - 1014 多重背包二进制优化
作者:互联网
多重背包模型 写的时候漏了一个等号找了半天 i<<=1 !!!!!!
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 int a[500]; 6 int dp[120006]; 7 int m[120006]; 8 int main(){ 9 int n=6; 10 int kase=1; 11 while(scanf("%d",&a[0])==1){ 12 int sum=a[0]; 13 int flag=0; 14 for(int j=1;j<=a[0];j<<1){ 15 m[flag++]=j; 16 a[0]-=j; 17 } 18 if(a[0])m[flag++]=a[0]; 19 for(int i=1;i<n;i++){ 20 scanf("%d",&a[i]); 21 sum+=a[i]; 22 for(int j=1;j<=a[i];j<<=1){ 23 m[flag++]=j*(i+1); 24 a[i]-=j; 25 } 26 if(a[i])m[flag++]=a[i]*(i+1); 27 } 28 29 memset(dp,0,sizeof(dp)); 30 if(sum==0)break; 31 sum=0; 32 for(int i=0;i<flag;i++)sum+=m[i]; 33 if(sum&1){ 34 printf("Collection #%d:\nCan't be divided.\n\n",kase++); 35 continue; 36 37 } 38 for(int i=0;i<flag;i++){ 39 for(int j=sum/2;j>=m[i];j--){ 40 dp[j]=max(dp[j],dp[j-m[i]]+m[i]); 41 } 42 } 43 if(dp[sum/2]==sum/2)printf("Collection #%d:\nCan be divided.",kase++); 44 else printf("Collection #%d:\nCan't be divided.",kase++); 45 puts(""); 46 puts(""); 47 48 } 49 return 0; 50 }
标签:int,dp,include,kase,sum,#%,Collection 来源: https://www.cnblogs.com/ttttttttrx/p/10260646.html