洛谷 2647 最大收益 题解

题意简述

给定个物品,每个物品有收益,以及一个减损值。当你选择了物品之后,珂以获得的收益,但是以后的所有物品的收益值都会减少。减少是珂以叠加的(甚至变成负的)。求最大收益。

思路框架

首先是无序的,先排个序。然后就。设表示前个物品选个的最大收益。然后,其中被作为结构体按逆序排序。

具体思路

显然要排序。但是我们如何排序呢?

我们想来想方程。显然,我们有这个转移,这是不选第个的情况。当然,也要考虑选第个的情况,即。然后我们发现我们不会转移了。

如果我们把第个当成是最后选的当然没法转移。

但是,我们珂以把它当成第一个选的

然后就出来了方程

但是这样就一定对么?

我们要保证是最小的才对,不然最优的选项应该在更前面。所以我们把这些物品按逆序排序排序即珂。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
using namespace std;
namespace Flandre_Scarlet
{
#define N 3333
#define int long long
#define F(i,l,r) for(int i=l;i<=r;++i)
#define D(i,r,l) for(int i=r;i>=l;--i)
#define Fs(i,l,r,c) for(int i=l;i<=r;c)
#define Ds(i,r,l,c) for(int i=r;i>=l;c)
#define Tra(i,u) for(int i=G.Start(u),__v=G.To(i);~i;i=G.Next(i),__v=G.To(i))
#define MEM(x,a) memset(x,a,sizeof(x))
#define FK(x) MEM(x,0)

struct node{int w,r;}a[N];
bool operator<(node a,node b){return a.r>b.r;}
int n;
void R1(int &x)
{
x=0;char c=getchar();int f=1;
while(c<'0' or c>'9') f=(c=='-')?-1:1,c=getchar();
while(c>='0' and c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
x=(f==1)?x:-x;
}
void Input()
{
R1(n);
F(i,1,n) R1(a[i].w),R1(a[i].r);
}

int dp[N][N];
void Soviet()
{
sort(a+1,a+n+1);
dp[1][1]=a[1].w;
F(i,1,n) F(j,1,i)
{
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+a[i].w-a[i].r*(j-1));
}
int ans=0;
F(i,1,n) ans=max(ans,dp[n][i]);
printf("%lld\n",ans);
}

#define Flan void
Flan IsMyWife()
{
Input();
Soviet();
}
#undef int //long long
}
int main()
{
Flandre_Scarlet::IsMyWife();
getchar();getchar();
return 0;
}

w