传送门
题目分析:
设\(f[i]\)表示装前i个玩具的花费。
列出转移方程:\[f[i] = max\{f[j] + ((i - (j + 1)) + sum[i] - sum[j] - L))^2\}\]
令\(x[i] = sum[i] + i\), \(P = L + 1\),上式化为:
\[f[i] = max\{f[j] + (x[i] - x[j] - P)^2\}\]
列式并化为斜率形式:\(S(i, j) = \frac{(f[i] + x[i]^2 + 2x[i]P) - (f[j] + x[j]^2 + 2x[j]P)}{2(x[i] - x[j])}\)
然后就是斜率dp了。
code
#include<bits/stdc++.h>
using namespace std;
const int N = 50050;
typedef long long ll;
int n;
ll L, c[N];
typedef long long ll;
ll f[N], x[N], sum[N];
int que[N];inline ll calc(int i, int j){return f[j] + (x[i] - x[j] - L) * (x[i] - x[j] - L);
}inline bool slopeCheck(int i, int j, int k){return ((f[i] + x[i] * x[i] + 2 * x[i] * L) - (f[j] + x[j] * x[j] + 2 * x[j] * L)) * 2 * (x[j] - x[k]) <=((f[j] + x[j] * x[j] + 2 * x[j] * L) - (f[k] + x[k] * x[k] + 2 * x[k] * L)) * 2 * (x[i] - x[j]);
}int main(){freopen("h.in", "r", stdin);scanf("%lld%lld", &n, &L);for(int i = 1; i <= n; i++) scanf("%lld", &c[i]), sum[i] = sum[i - 1] + c[i], x[i] = sum[i] + i;L += 1;int head, tail;que[head = tail = 1] = 0;for(int i = 1; i <= n; i++){while(head + 1 <= tail && calc(i, que[head]) >= calc(i, que[head + 1])) head++;f[i] = calc(i, que[head]);while(head <= tail - 1 && slopeCheck(i, que[tail], que[tail - 1])) tail--;que[++tail] = i;}printf("%lld", f[n]);return 0;
}