0%

ABC366F Maximum Composition

传送门

Solution

f1(x)=a1x+b1f_1(x)=a_1x+b_1f2(x)=a2x+b2f_2(x)=a_2x+b_2。若 f1(f2(x))<f2(f1(x))f_1(f_2(x))<f_2(f_1(x)),则 a1b2+a1<a2b1+b2a_1b_2+a_1<a_2b_1+b_2。又因为 b1,b2>0b_1,b_2>0,移项得a11b1<a21b2\dfrac{a_1-1}{b_1}<\dfrac{a_2-1}{b_2}

因此对于选定的 KKf(x)f(x),从内到外 a1b\dfrac{a-1}{b} 应升序排列。

我们对 NN 个函数以 a1b\dfrac{a-1}{b} 为关键字升序排序,用 01 背包求解即可。

Code

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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll N = 2e5 + 10;

ll n, k, f[N];
struct Node {
ll a, b;
friend bool operator < (const Node &a, const Node &b) {
return (a.a - 1) * b.b < (b.a - 1) * a.b;
}
} a[N];

int main()
{
scanf("%lld%lld", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%lld%lld", &a[i].a, &a[i].b);
sort(a + 1, a + 1 + n);
f[0] = 1;
for (int i = 1; i <= n; i++)
for (int j = k; j; j--)
f[j] = max(f[j], a[i].a * f[j - 1] + a[i].b);
printf("%lld", f[k]);
return 0;
}