Title Description
Input
Output
Sample input
4
-1 10-20
2 2 3 4
Sample output
9
Exercises
dp+ slope optimization
Set F[i] represents the maximum value of the sum of the combat effectiveness of the first I soldier.
Then there is f[i]=f[j]+a* (Sum[i]-sum[j]) ^2+b* (Sum[i]-sum[j]) +c,
Where sum is the prefix and.
Expand the square and tidy up the f[j]+a*sum[j]^2-b*sum[j]=2*a*sum[i]*sum[j]+f[i]-a*sum[i]^2-b*sum[i]-c.
This gives the Y=kx+b form, and the maximum value of B is required.
So the maintenance of a convex hull, and the difference between the convex hull is the slope comparison of ' > ' and ' < ' difference.
#include <cstdio> #define Y (i) (F[i] + A * sum[i] * Sum[i]-b * Sum[i]) #define X (i) Sum[i]long long sum[1000010], F [1000010];int q[1000010], L, R;int main () {int n, I;long long A, B, C, x;scanf ("%d%lld%lld%lld", &n, &a, & Amp;b, &c); for (i = 1; I <= n; i + +) scanf ("%lld", &x), sum[i] = sum[i-1] + x;for (i = 1; I <= n; i + +) {while (L < R && Y (q[l + 1])-y (Q[l]) > (x (q[l + 1])-X (Q[l])) * 2 * A * Sum[i]) L + +; F[i] = F[q[l]] + A * (Sum[i]-sum[q[l]) * (Sum[i]-sum[q[l]) + b * (Sum[i]-sum[q[l]]) + c;while (L < R && (y (i)-y (Q[r])) * (x (Q[r])-X (Q[r-1]) > (x (i)-X (Q[r]) * (Y (q[r])-y (Q[r-1]))) R--; q[++r] = i;} printf ("%lld\n", F[n]); return 0;}
"bzoj1911" [Apio2010] Special Operations team