[BZOJ4247]挂饰(DP)
当最终挂饰集合确定了,一定是先挂挂钩多的在挂挂钩少的。
于是按挂钩从大到小排序,然后就是简单的01背包。
1 #include<cstdio> 2 #include<algorithm> 3 #define rep(i,l,r) for (int i=(l); i<=(r); i++) 4 using namespace std; 5 6 const int N=2010,inf=1e9; 7 int f[N][N]; 8 int n,ans=-inf,v,c; 9 struct P{ int v,c; }s[N]; 10 bool operator <(const P &a,const P &b){ return a.v>b.v; } 11 12 int main(){ 13 freopen("bzoj4247.in","r",stdin); 14 freopen("bzoj4247.out","w",stdout); 15 scanf("%d",&n); 16 rep(i,1,n) scanf("%d%d",&s[i].v,&s[i].c); 17 rep(i,0,n) f[0][i]=f[i][n+1]=-inf; 18 f[0][1]=0; sort(s+1,s+n+1); 19 rep(i,1,n) rep(j,0,n) f[i][j]=max(f[i-1][max(j-s[i].v,0)+1]+s[i].c,f[i-1][j]); 20 rep(i,0,n) ans=max(ans,f[n][i]); 21 printf("%d\n",ans); 22 return 0; 23 }