[APIO2010]特别行动队
题面很直白,就不放了。
太套路了,做起来没点感觉了。
\(dp(i)=dp(j)+a*(s(i)-s(j))^{2}+b*(s(i)-s(j))+c\)
直接推出一个斜率优化的式子上单调队列就好了
时间/空间复杂度:\(O(n)\)
#include<cstdio>
#define sid 1000500
#define ri register int
#define ll long long
#define dd double
using namespace std; #define getchar() *S ++
char RR[], *S = RR;
inline int read(){
int p = , w = ;
char c = getchar();
while(c > '' || c < '') {
if(c == '-') w = -;
c = getchar();
}
while(c >= '' && c <= '') {
p = p * + c - '';
c = getchar();
}
return p * w;
} ll dp[sid], sum[sid];
int q[sid], n, a, b, c; #define x(g) (sum[(g)])
#define y(g) (dp[(g)] + a * sum[(g)] * sum[(g)] - b * sum[(g)]) inline dd s(int i,int j){
return (dd)(y(i) - y(j)) / (dd)(x(i) - x(j));
} int main(){
fread(RR, , sizeof(RR), stdin);
n = read(); a = read();
b = read(); c = read();
for(ri i = ; i <= n; i ++) sum[i] = sum[i - ] + read();
ri fr = , to = ;
for(ri i = ; i <= n; i ++){
while(fr + <= to && s(q[fr],q[fr + ]) > * a * sum[i]) fr ++;
int p = q[fr];
ll pp = sum[i] - sum[p];
dp[i] = dp[p] + a * pp * pp + b * pp + c;
while(fr + <= to && s(q[to], q[to - ]) <= s(i, q[to - ])) to --;
q[++ to]=i;
}
printf("%lld\n", dp[n]);
return ;
}
特别行动队