传送门
Solution
设 f1(x)=a1x+b1,f2(x)=a2x+b2。若 f1(f2(x))<f2(f1(x)),则 a1b2+a1<a2b1+b2。又因为 b1,b2>0,移项得b1a1−1<b2a2−1。
因此对于选定的 K 个 f(x),从内到外 ba−1 应升序排列。
我们对 N 个函数以 ba−1 为关键字升序排序,用 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; }
|