hdu 1171 Dividing 단조로운 대기열 최적화 dp

4623 단어
/* *********************************************** 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:
"
,_++); if(sum%2==1) { printf("Can't be divided.

"
);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.

"
); else printf("Can't be divided.

"
); } return 0; }

좋은 웹페이지 즐겨찾기