POJ 1014 Dividing(DP)

简直弱到不行,想用单调队列优化秀操作居然还给写错了WA了好几次,怎一个弱字了得。。。。

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

int a[7];
bool dp[120010];

int main(){
    int i,j,cas=1;
    while(1){
        memset(dp,0,sizeof(dp));
        int flag=1;
        int sum=0;
        for(i=1;i<7;i++){
            cin>>a[i];
            if(a[i]%2) flag=0;
            sum+=i*a[i];
        }
        if(!sum) break;
        cout<<"Collection #"<<cas++<<":"<<endl;
        if(flag){
            cout<<"Can be divided."<<endl<<endl;
            continue;
        }
        if(sum%2){
            cout<<"Can't be divided."<<endl<<endl;
            continue;
        }
        int n=sum/2;
        dp[0]=1;
        for(i=1;i<7;i++){
            if(a[i]==0) continue;
            if(dp[n]) break;
            if(a[i]==1){
                for(j=n-i;j>=0;j--){
                    if(dp[j]){
                        dp[j+i]=1;
                        //cout<<i+j<<" ";
                    }
                }
            }
            else if(a[i]*i>=n){
                for(j=0;j<=n;j++){
                    if(dp[j]){
                        dp[i+j]=1;
                        //cout<<i+j<<" ";
                    }
                }
            }
            else{
                //cout<<"dandiao"<<i<<endl;
                for(j=0;j<i;j++){
                    int que[60010],sum=0,s=0,e=-1;
                    for(int k=j;k<=n;k+=i){
                        if(e-s==a[i]){
                            sum-=que[s++];
                        }
                        sum+=dp[k];
                        que[++e]=dp[k];
                        if(sum){
                            dp[k]=1;
                        }
                    }
                }
            }
        }
        if(dp[n]) cout<<"Can be divided."<<endl;
        else cout<<"Can't be divided."<<endl;
        cout<<endl;
    }
    return 0;
}


你可能感兴趣的:(dp)