BZOJ4247 : 挂饰
首先将挂饰按照挂钩个数从大到小排序,然后DP
设f[i][j]处理完前i个挂饰,还有j个多余挂钩的最大喜悦值,则
f[0][1]=0
f[i][j]=max(f[i-1][max(j-a[i],0)+1]+b[i],f[i-1][j])
时间复杂度$O(n^2)$。
#include<cstdio> #include<algorithm> #define N 2010 using namespace std; int n,i,j,f[N][N],ans; struct P{int x,y;}a[N]; inline bool cmp(const P&a,const P&b){return a.x>b.x;} int main(){ for(scanf("%d",&n),i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y); sort(a+1,a+n+1,cmp); for(i=0;i<=n;i++)f[0][i]=f[i][n+1]=-2000000000; for(f[0][1]=0,i=1;i<=n;i++)for(j=0;j<=n;j++)f[i][j]=max(f[i-1][max(j-a[i].x,0)+1]+a[i].y,f[i-1][j]); for(i=0;i<=n;i++)ans=max(ans,f[n][i]); return printf("%d",ans),0; }