标签:pen typedef nbsp log int algorithm down gis lld
多重背包问题,二进制拆分后做~
#include <cstdio> #include <algorithm> #include <cstring> #define up(a,b,c) for(register int c=a;c<=b;++c) #define down(a,b,c) for(register int c=a;c>=b;--c) typedef long long ll; ll n,x; ll num[205],value[205],Aim,v[200005],Count=1,dp[2000005],Size[200005]; int main(){ // freopen("in.in","r",stdin); // freopen("myout.out","w",stdout); scanf("%lld",&n); up(1,n,i) scanf("%lld",&value[i]); up(1,n,i){ scanf("%lld",&x); for(int j=1;j<=x;j<<=1){ v[Count] = j*value[i]; Size[Count++]=j; x-=j; } if(x>0){ v[Count]=x*value[i]; Size[Count++]=x; } } scanf("%lld",&Aim); memset(dp,0x3f3f3f3f,sizeof(dp)); dp[0]=0; //printf("%d\n",Count); /*for(int i=1;i<Count;i++){ printf("v[%d]:%d Size[%d]:%d\n",i,v[i],i,Size[i]); }*/ for(int i=1;i<Count;i++){ for(int j=Aim;j>=v[i];j--){ dp[j] = std::min(dp[j],dp[j-v[i]]+Size[i]); //printf("dp[%d]:%d\n",j,dp[j]); } } printf("%lld\n",dp[Aim]); return 0; }
标签:pen typedef nbsp log int algorithm down gis lld
原文地址:http://www.cnblogs.com/OIerLYF/p/7496143.html