首页 > 代码库 > 【bzoj 1911】[Apio2010]特别行动队

【bzoj 1911】[Apio2010]特别行动队

Description

技术分享

Input

技术分享

Output

技术分享

Sample Input

4
-1 10 -20
2 2 3 4

Sample Output

9

HINT

技术分享

 

 

技术分享
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 using namespace std;
 5 const int N=1000050;
 6 int n,a,b,c,x[N],q[N];
 7 long long s[N],f[N];
 8 long long read()
 9 {
10     long long x=0,k=1;char c=getchar();
11     while(c<0||c>9){if(c==-)k=-1;c=getchar();}
12     while(c>=0&&c<=9){x=x*10+c-0;c=getchar();}
13     return x*k;
14 }
15 long long sum(long long x){return x*x;}
16 double slope(int k,int j){return (f[j]-f[k]+a*(sum(s[j])-sum(s[k]))+b*(s[k]-s[j]))/(2.0*a*(s[j]-s[k]));}
17 int main()
18 {
19     int l=0,r=0;
20     n=read();a=read();b=read();c=read();
21     for(int i=1;i<=n;i++)x[i]=read(),s[i]=s[i-1]+x[i];
22     for(int i=1;i<=n;i++)
23     {
24         while(l<r&&slope(q[l],q[l+1])<s[i])l++;
25         int t=q[l];
26         f[i]=f[t]+a*sum(s[i]-s[t])+b*(s[i]-s[t])+c;
27         while(l<r&&slope(q[r],i)<slope(q[r-1],q[r]))r--;
28         q[++r]=i;
29     }
30     printf("%lld",f[n]);
31     return 0;
32 }
View Code

 

【bzoj 1911】[Apio2010]特别行动队