CH5E07 division marble (dp + backpack binary split)

    Portal

    main idea:

  Are valuable marble 1..6 each A [1..6] block, they now want to split into two parts, so that the value equal to the sum of two parts, can be asked if implemented. The total number of marble which does not exceed 20,000.

 Problem-solving ideas:

  Multiple properly properly backpack + binary split, split mainly write about binary save a file (a child's nightmare).

  As we know, 2 0 , 2 . 1 , 2 2 , 2 ...... K. 1- pick and choose the number obtained by adding 2 ~ 0 K any number of -1. Then a binary split s number, the first step is to find the maximum k satisfies 2 k -1 <= s , set C = 2 s- k + 1'd. Apparent to 2 0 , 2 . 1 , ......, 2 K-. 1 , a plurality of C can pick and choose the number obtained by adding an arbitrary number of 0 ~ s. This optimization problem is to do in this idea, take the value of 1, for example marble, has a value of marble t1 1, t1 may be split into 2 0 , 2 1 , 2 2 , ......, 2 k -1 , C, and can be multiple to solve the backpack into the backpack 01, each number s can dissolve the log (s), the total complexity o (nlogn)

  

#include<bits/stdc++.h>
using namespace std;
int num[7][10086];
bool bk[100086];
int main()
{
    int t[10];
    while(1){
        int sum=0;
        for(int i=1;i<=6;i++){
            scanf("%d",&t[i]);
            sum+=t[i]*i;
        }
        if(sum==0)    break;
        if(sum&1){
            cout<<"Can't"<<endl;continue;
        }
        sum/=2;for(int i=1;i<=sum;i++)    bk[i]=false;bk[0]=true;
        for(int i=1;i<=6;i++){
            int t1=0,t2=0;
            while(t[i]>=(1<<t1)){
                num[i][t1]=(1<<t1);t1++;
            }
            if(t[i]-(1<<t1))    num[i][t1++]=t[i]-(1<<t1);
            for(int j=0;j<t1;j++){
                for(int h=sum;h>=i*num[i][j];h--){
                    bk[h]|=bk[h-i*num[i][j]];
                }
            }
        }
        if(bk[sum])    cout<<"Can"<<endl;
        else        cout<<"Can't"<<endl;
    }
}
View Code

 

Guess you like

Origin www.cnblogs.com/r138155/p/12663900.html