BZOJ4247 挂饰(动态规划)

  相当于一个有负体积的背包。显然如果确定了选哪些,应该先把体积小的挂上去。于是按体积从小到大排序,就是一个裸的背包了。

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
#define N 2010
#define inf 2000000000
int n,f[N][N];
struct data{int x,y;
}a[N];
bool cmp(const data&a,const data&b)
{
    return a.x>b.x;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("bzoj4247.in","r",stdin);
    freopen("bzoj4247.out","w",stdout);
    const char LL[]="%I64d\n";
#else
    const char LL[]="%lld\n";
#endif
    n=read();
    for (int i=1;i<=n;i++) a[i].x=read(),a[i].y=read();
    sort(a+1,a+n+1,cmp);
    for (int i=0;i<=n;i++)
        for (int j=1;j<=n+1;j++) f[i][j]=-inf;
    f[0][1]=0;
    for (int i=1;i<=n;i++)
        for (int j=0;j<=n;j++)
        f[i][j]=max(f[i-1][j],f[i-1][max(j-a[i].x,0)+1]+a[i].y);
    for (int i=1;i<=n;i++) f[n][0]=max(f[n][0],f[n][i]);
    cout<<f[n][0];
    return 0;
}

 

posted @ 2018-10-23 00:58  Gloid  阅读(184)  评论(0编辑  收藏  举报