Luo Gu P2647 maximum benefit solution to a problem

Face questions

For the "n th choose any number of items," we can think of A recursive method, set and f [i] [j] represent the i-th article before the election of the j-th maximum benefit

We found a positive significant shift transfer is not good, we can shift backwards, the currently selected item No. i for the first item, so we found this article to contribute answer do becomes a [i] .w-a [i] .r * (j-1), then write the transfer equation:

f[i][j]=max(f[i−1][j],f[i−1][j−1]+a[i].w−a[i].r∗(j−1))

In order to obtain, for the entire equation, we want to make the biggest gains, as Ri greedy sort descending backwards in the case of the transfer, rather than from small to large beginning of thought.

 

#include <bits/stdc++.h>
using namespace std;
int n;
struct haha{
    int a;
    int b;
}lala[3010];
long long  f[3010][3010];
bool cmp(haha x,haha y)
{
    return x.b<y.b;
}
int main ()
{
    cin>>n;
    for(register int i=1;i<=n;i++){
        scanf("%d%d",&lala[i].a,&lala[i].b);
    }
    sort(lala+1,lala+1+n,cmp);
    f[n][1]=lala[n].a;
    for(register int i=n-1;i>=1;i--){
        for(register int j=1;j<=n;j++){
            f[i][j]=max(f[i+1][j],f[i+1][j-1]+lala[i].a-lala[i].b*(j-1));
        }
    }
    long long maxn=0;
    for(int i=0;i<=n;i++){
        maxn=max(maxn,f[1][i]);
    }
    cout<<maxn;
}

 

Guess you like

Origin www.cnblogs.com/kamimxr/p/11351131.html