标签:size 多重背包 思维 cst log mem ons can png
题目思维难度不大,二进制优化多重背包+完全背包组合。
但是我居然傻逼了WA了7次。
#include <cstdio> #include <algorithm> #include <cstring> using std::min; const int INF=0x3f3f3f3f; int dp[113333]; int shop[113333]; int n,t,Count,Min=INF; int coin[105],has; int V[23333],Size[23333]; int main(){ memset(dp,INF,sizeof(dp)); memset(shop,INF,sizeof(shop)); dp[0]=0; shop[0]=0; scanf("%d%d",&n,&t); for(int i=1;i<=n;i++){ scanf("%d",&coin[i]); for(int j=coin[i];j<=t*3;++j){ shop[j] = min(shop[j],shop[j-coin[i]]+1); } } for(int i=1;i<=n;++i){ scanf("%d",&has); for(int j=1;j<=has;j<<=1){ V[Count] = j*coin[i]; Size[Count++] = j; has-=j; } if(has>0){ V[Count] = has*coin[i]; Size[Count++] = has; } } for(int i=0;i<=Count;++i){ for(int j=3*t;j>=V[i];--j){ dp[j] = min(dp[j],dp[j-V[i]]+Size[i]); } } //for(int i=0;i<=3*t;i++) printf("dp[%d]:%d\n",i,dp[i]); for(int j=t;j<=3*t;++j){ if(dp[j]!=INF && shop[j-t]!=INF){ Min = min(Min,dp[j]+shop[j-t]); } } if(Min==INF) printf("-1"); else printf("%d\n",Min); return 0; }
BZOJ 1716 [Usaco2006 Dec]The Fewest Coins 找零钱
标签:size 多重背包 思维 cst log mem ons can png
原文地址:http://www.cnblogs.com/OIerLYF/p/7519957.html