# 【BZOJ 1911】 [Apio2010]特别行动队

4
-1 10 -20
2 2 3 4

9

## HINT

f[i]=max(f[j]+a*(h[i]-h[j])^2+b*(h[i]-h[j])+c)
//h数组为前缀和

f[j]+a*(h[i]-h[j])^2+b*(h[i]-h[j])+c>f[k]+a*(h[i]-h[k])^2+b*(h[i]-h[k])+c

f[j]-f[k]+a*h[j]^2-a*h[k]^2-b*h[j]+b*h[k]>2*a*(h[j]-h[k])*h[i]

 1 #include<cstdio>
2 #define ll long long
3 const int N=1000005;
4 int a,b,c,n,l,r;
5 int x[N],q[N];
6 ll f[N],h[N];
7 ll sqr(ll x) {return x*x;}
8 double slop(int k,int j)
9 {return (double)(f[j]-f[k]+a*sqr(h[j])-a*sqr(h[k])-b*h[j]+b*h[k])/(double)(2*a*(h[j]-h[k]));}
10
11 int main(){
12     scanf("%d",&n);
13     scanf("%d%d%d",&a,&b,&c);
14     for (int i=1;i<=n;i++) {
15         scanf("%d",&x[i]);
16         h[i]=h[i-1]+x[i];
17     }
18     for (int i=1;i<=n;i++){
19         while(l<r&&slop(q[l],q[l+1])<h[i])l++;
20         int now=q[l];
21         f[i]=f[now]+a*sqr((h[i]-h[now]))+b*(h[i]-h[now])+c;
22         while(l<r&&slop(q[r],i)<slop(q[r-1],q[r])) r--;
23         q[++r]=i;
24     }
25     printf("%lld",f[n]);
26 } 

posted @ 2016-02-16 06:50  Alisahhh  阅读(181)  评论(0编辑  收藏  举报