Title: 01 Backpack, which weight<=2^30, but each weight can be written in a*2^b form, of which a<=10,b<=30
Direct backpack sure Tle+mle
Considering that every weight can be written in a*2^b form, obviously we're going to do the backpack in B-tiered.
F[I][J] represents the maximum value for space with j*2^i+ (w& (1<<i)-1)
First, make a 01 backpack inside each layer.
Then transfer between layers and layers
from large to small enumeration J transfer equation is F[i][j]=max{f[i][j],f[i][j-k]+f[i-1][min (k*2+ ((w>>i-1) &1), 1000)]}
How did this come about? We first enumerate J, and then enumerate the space j-k of the same layer, then we can choose k*2+ ((w>>i-1) &1) on the previous level.
It's hard to understand? Give me a chestnut.
w= (1000100010) 2,i=6,j= (111) 2,k=1
So what we're going to update is (111100010) 2, to update this value is (110000000) 2
K=1, representing the difference between the layer 1, the difference between the previous layer 2
((w>>i-1) &1=1, on behalf of the previous layer can also choose 1 (6th position on the 1)
We will choose (1100010) 2 of the space at the next level, and this value is recorded on f[5][3]
Or understand not can only see a few more times this paragraph = = The problem is so damn rational and pleasant
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;int N,w;long Long F[40][1010],ans;int Main () {int I,j,k,x;while (cin>>n>>w,n>0) {memset (f,0,sizeof F ); Ans=0;for (i=1;i<=n;i++) {int a=0,b=0;scanf ("%d%d", &a,&x), while (~a&1) a>>=1,++b;for (j=1000; j>=a;j--) F[b][j]=max (f[b][j],f[b][j-a]+x);} for (i=0;i<=30;i++) for (j=1;j<=1000;j++) F[i][j]=max (F[i][j],f[i][j-1]), for (I=1;i<=min (1000,w); i++) ans= Max (Ans,f[0][i]); for (i=1;i<=30&& (1<<i) <=w;i++) for (J=min (1000,w>>i), ~j;j--) {for (k=0;k <=j;k++) F[i][j]=max (F[i][j],f[i][j-k]+f[i-1][min (k+k+ (w>>i-1) &1)]); Ans=max (Ans,f[i][j]);} Cout<<ans<<endl;} return 0;}
Bzoj 1190 HNOI2007 Fantasy Island orb Dynamic planning