BZOJ1911:[APIO2010]特别行动队(斜率优化DP)
Description
Input
Output
Sample Input
4
-1 10 -20
2 2 3 4
-1 10 -20
2 2 3 4
Sample Output
9
HINT
Solution
裸的斜率优化DP
Code
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #define LL long long 5 #define N (2000000+10) 6 using namespace std; 7 LL n,a,b,c,x; 8 LL s[N],f[N]; 9 LL q[N*2],head,tail; 10 11 LL K(LL j) { return -2*a*s[j]; } 12 LL B(LL j) { return f[j]+a*s[j]*s[j]-b*s[j]; } 13 LL Y(LL i,LL j) { return K(j)*s[i]+B(j); } 14 15 bool cover(LL x1,LL x2,LL x3) 16 { 17 LL w1=(K(x3)-K(x1))*(B(x1)-B(x2)); 18 LL w2=(K(x2)-K(x1))*(B(x1)-B(x3)); 19 return w1<=w2; 20 } 21 22 int main() 23 { 24 scanf("%lld%lld%lld%lld",&n,&a,&b,&c); 25 for (LL i=1;i<=n;++i) 26 scanf("%lld",&x),s[i]=s[i-1]+x; 27 head=1,tail=1; 28 for (LL i=1;i<=n;++i) 29 { 30 while (head<tail && Y(i,q[head])<=Y(i,q[head+1])) head++; 31 f[i]=Y(i,q[head])+a*s[i]*s[i]+b*s[i]+c; 32 while (head<tail && cover(i,q[tail],q[tail-1])) tail--; 33 q[++tail]=i; 34 } 35 printf("%lld",f[n]); 36 }