多重背包的二进制优化

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int v[200005],s[200005],val[200005];
int main()
{
    int a[6]={1,2,3,4,5,6},b[10],k,sum,h,f,g=0;
    while(1)
    {
        memset(val,0,sizeof(val));
        memset(v,0,sizeof(v));
        memset(s,0,sizeof(s));
        g++;
        k=0;
        sum=0;
        for(int i=0;i<6;i++)
        {
            cin>>b[i];
            sum=sum+b[i]*a[i];
            if(b[i]==0)
            k++; 
        }
        if(k==6)
        break;
        printf("Collection #%d:\n",g);
        if(sum%2!=0||k==5)
        {
            cout<<"Can't be divided."<<endl;
            cout<<endl;
            continue;
        }
        h=0;
        f=0;
        for(int i=0;i<6;i++)
        {
            if(b[i]==0)
            continue;
            for(int j=1;j<=b[i];j<<=1)
            {
                v[h++]=j*a[i];
                s[f++]=j*a[i];
                b[i]=b[i]-j;
            }
            if(b[i]>0)
            {
                v[h++]=b[i]*a[i];
                s[f++]=b[i]*a[i];
            }
        }
        for(int i=0;i<h;i++)
            for(int j=sum/2;j>=s[i];j--)
            {
                val[j]=max(val[j],val[j-s[i]]+v[i]);
            } 
            if(val[sum/2]==sum/2)
            {
            cout<<"Can be divided."<<endl;
            }
            else
            {
            cout<<"Can't be divided."<<endl;
            }
            cout<<endl;
    }
}

 

posted @ 2019-05-08 22:17  Leozi  阅读(215)  评论(0编辑  收藏  举报