hdu 1171 Dividing 单调队列优化dp

/* *********************************************** Author :xryz Email :[email protected] Created Time :2015/10/23 14:47:13 File Name :0001.cpp ************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
//#include <bits/stdc++.h>
using namespace std;
int a[120000+5],b[120000+5],f[120000+5];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
   int _,i,j,n,m,x[10];
   int h,t,v,c,k,d,now,sum;
   _=1;
   while(1)
   {
       memset(f,0,sizeof(f));
       sum=0;
       for(i=1;i<=6;i++)
       {
           scanf("%d",&x[i]);
           sum+=x[i]*i;
       }
        if(sum==0) break;
       printf("Collection #%d:\n",_++);
       if(sum%2==1)
       {
           printf("Can't be divided.\n\n");continue;
       }
       for(i=1;i<=6;i++)
       {
           v=x[i];
           if(v==0) continue;
           if(v>sum/i) v=sum/i;
            for(d=0;d<i;d++)
            {
                h=t=0;
                for(j=0;j<=(sum-d)/i;j++)
                {
                    now=f[j*i+d]-j*i;
                    while(h<t&&b[t-1]<now)
                        t--;
                    a[t]=j;
                    b[t++]=now;
                    while(a[h]<j-v) h=h+1;
                    f[j*i+d]=max(f[j*i+d],b[h]+j*i);
                }
            }
       }
       if(2*f[sum/2]==sum)
            printf("Can be divided.\n\n");
       else
            printf("Can't be divided.\n\n");
   }
    return 0;
}

你可能感兴趣的:(hdu 1171 Dividing 单调队列优化dp)