题意
给出 A A A,把它分成若干段,段 [ l , r ] [l,r] [l,r]的贡献为 f ( m i n { a l . . r } ) = f ( x ) = A x 3 + B x 2 + C x + D f(min\{a_{l..r}\})=f(x)=Ax^3+Bx^2+Cx+D f(min{al..r})=f(x)=Ax3+Bx2+Cx+D,求贡献最大。
思路
设
f
i
f_i
fi为前
i
i
i个已划分的贡献,得:
f
i
=
f
j
+
F
(
m
i
n
{
a
j
+
1..
i
}
)
f_i=f_{j}+F(min\{a{j+1..i}\})
fi=fj+F(min{aj+1..i})
然后是
O
(
n
2
)
O(n^2)
O(n2)的,我们考虑优化。
用单调递增的栈维护前面的最小值转折点,在这个区间内, F ( x ) F(x) F(x)的值相同,所以维护一个最大的 f f f进行转移。(糊
#include <cstdio>
#include <algorithm>
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
long long n, A, B, C, D;
long long a[200001], f[200001];
struct node {
long long id, maxx, ans;
}stack[200001];
long long F(long long x) {
return A * x * x * x + B * x * x + C * x + D;
}
int main() {
file(min);
scanf("%lld %lld %lld %lld %lld", &n, &A, &B, &C, &D);
for (int i = 1; i <= n; i++)
scanf("%lld", a + i);
int top = 0;
for (int i = 1; i <= n; i++) {
long long w = f[i - 1];
while (top && a[stack[top].id] >= a[i]) w = std::max(w, stack[top--].maxx);
stack[++top].maxx = w;
stack[top].id = i;
if (top > 1) stack[top].ans = std::max(stack[top - 1].ans, w + F(a[i]));
else stack[top].ans = w + F(a[i]);
f[i] = stack[top].ans;
}
printf("%lld", f[n]);
}