• 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
    ";
    #else
        const char LL[]="%lld
    ";
    #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;
    }
  • 相关阅读:
    122. Best Time to Buy and Sell Stock II
    121. Best Time to Buy and Sell Stock
    72. Edit Distance
    583. Delete Operation for Two Strings
    582. Kill Process
    indexDB基本用法
    浏览器的渲染原理
    js实现txt/excel文件下载
    git 常用命令
    nginx进入 配置目录时
  • 原文地址:https://www.cnblogs.com/Gloid/p/9834021.html
Copyright © 2020-2023  润新知