可行性多个背包。
题意是说,有 1~6 一种石头,价格分别为值1~6 。再有就是不同的数。问:你能不能给一个平均的两个人。
这时候可以刮胡子价格值作为成本,达 总价值的一半。即讲背包的容量设为 总价值的一半。是否能装满。
据说有个非常强大的“剪树” 1~6的最小公倍数是60 。
个数超过60……if(n&1)n=61; else n=60;
ORZ……没想到,也没用这个。
#include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<queue> #include<map> #include<stack> #include<iostream> #include<list> #include<set> #include<cmath> #define INF 0x7fffffff #define eps 1e-6 #define LL long long using namespace std; int n,m; int dp[7][150001]; int Cot[7]; int Cost[7]; int main() { int cc=1; while(1) { bool flag=1; m=0; for(int i=1; i<7; i++) { int tmp; scanf("%d",&tmp); if(tmp)flag=0; Cost[i]=i,Cot[i]=tmp; m+=Cost[i]*Cot[i]; } if(flag)return 0; memset(dp,-1,sizeof(dp)); printf("Collection #%d: ",cc++); if(m&1) { puts("Can't be divided. "); continue; } else m/=2; dp[0][0]=0; for(int i=1; i<7; i++) { for(int j=0; j<=m; j++) { if(dp[i-1][j]>=0) dp[i][j]=Cot[i]; else dp[i][j]=-1; } for(int j=0; j<=m-Cost[i]; j++) { if(dp[i][j]>0) dp[i][j+Cost[i]]=max(dp[i][j+Cost[i]],dp[i][j]-1); } } if(dp[6][m]==-1) puts("Can't be divided. "); else puts("Can be divided. "); } }