From Evernote: |
uva 711 |
這題我覺得滿難的DP,參考網路
[sourcecode language="cpp"]
//============================================================================
// Name : Dividing up.cpp
// Date : 2013 2013/2/5 下午4:09:06
// Author : GCA
//============================================================================
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cctype>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
#define Set(a,s) memset(a,s,sizeof(a))
#define Write(w) freopen(w,"w",stdout)
#define Read(r) freopen(r,"r",stdin)
#define Pln() printf("\n")
#define I_de(x,n)for(int i=0;i<n;i++)printf("%d ",x[i]);Pln()
#define De(x)printf(#x"%d\n",x)
#define For(i,x)for(int i=0;i<x;i++)
#define CON(x,y) x##y
#define Pmz(dp,nx,ny)for(int hty=0;hty<ny;hty++){for(int htx=0;htx<nx;htx++){\
printf("%d ",dp[htx][hty]);}Pln();}
#define M 220005
#define PII pair<int,int>
#define PB push_back
#define oo INT_MAX
#define Set_oo 0x3f
#define Is_debug true
#define debug(...) if(Is_debug)printf("DEBUG: "),printf(__VA_ARGS__)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define eps 1e-6
bool xdy(double x,double y){return x>y+eps;}
bool xddy(double x,double y){return x>y-eps;}
bool xcy(double x,double y){return x<y-eps;}
bool xcdy(double x,double y){return x<y+eps;}
int min3(int x,int y,int z){
int tmp=min(x,y);
return min(tmp,z);
}
int max3(int x,int y,int z){
int tmp=max(x,y);
return max(tmp,z);
}
int dp[M],dp2[M];
int main() {
ios_base::sync_with_stdio(0);
int a[7];
int ff=0;
while(1){
int sum=0;
Set(dp,0);
Set(dp2,0);
for(int i=1;i<=6;i++){
scanf("%d",&a[i]);
sum+=a[i]*i;
}
if(!sum)break;
printf("Collection #%d:\n",++ff);
if(!(sum&1)){
int half=sum/2;
for(int i=0;i<=half;i++){
int t=i;
if(t/4>=a[4])t-=a[4]*4;
else t%=4;
if(t/2>=a[2])t-=a[2]*2;
else t%=2;
if(t/1>=a[1])t-=a[1];
else t%=1;
if(t==0)dp[i]=1;
}
for(int i=0;i<=half;i++){
int t=i;
if(t/6>=a[6])t-=a[6]*6;
else t%=6;
if(t/3>=a[3])t-=a[3]*3;
else t%=3;
if(t==0)dp2[i]=1;
}
if(a[5]){
int tmp=a[5];
for(int i=half;i>=0;i--){
if(dp[i])continue;
for(int a=0,b=min(tmp*5,(i/5)*5);a<=b;a+=5,b-=5){
// debug("%d %d%d\n",i,a,b);
if(dp[i])break;
if(dp[i-a]||dp[i-b])dp[i]=1;
}
}
}
int fg=0;
for(int i=0;i<=half;i++){
if(dp[i]&&dp2[half-i]){
fg=1;break;
}
}
if(fg==1)printf("Can be divided.\n");
else printf("Can't be divided.\n");
}
else printf("Can't be divided.\n");
Pln();
}
}
[/sourcecode]
留言
張貼留言