DP-01背包

题目传送门

题目类似01背包,但存在一个选取先后不同价值会有损耗,所有对物品按易损耗的程度从大到小排个序来顺序选取。

#include<bits/stdc++.h>
using namespace std;
#define LL long long
const LL maxn=1e5+10;
struct note
{
    LL a,b,c;
} w[maxn];
LL f[maxn];
bool cmp(note a,note b)
{
    return a.b*b.c>b.b*a.c;
}
int main()
{
    LL t,n;
    cin>>t>>n;
    for(LL i=1; i<=n; i++)
        cin>>w[i].a;
    for(LL i=1; i<=n; i++)
        cin>>w[i].b;
    for(LL i=1; i<=n; i++)
        cin>>w[i].c;
    sort(w+1,w+1+n,cmp);
    for(LL i=1; i<=n; i++)
        for(LL j=t; j>=w[i].c; j--)
        {
            f[j]=max(f[j],f[j-w[i].c]+w[i].a-j*w[i].b);
        }
    LL ans=-0x3f3f3f3f;
    for(LL i=1; i<=t; i++)
        ans=max(ans,f[i]);
    cout<<ans;
}
原文地址:https://www.cnblogs.com/dongdong25800/p/10713056.html