bzoj1911 [Apio2010]特别行动队commando
斜率优化
1 #include<cstdio> 2 #include<cstdlib> 3 #include<string> 4 #include<cstring> 5 #include<cmath> 6 #define re(i,l,r) for(int i=(l);i<=(r);i++) 7 using namespace std; 8 typedef long long LL; 9 template<typename Q> 10 void inin(Q &x) 11 { 12 x=0;int f=0;char ch=getchar(); 13 while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} 14 while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar(); 15 x=f?-x:x; 16 } 17 int n,a,b,c; 18 LL sum[1000010],x[1000010],y[1000010],aa[1000010],dp[1000010]; 19 int q[1000010]; 20 LL f(const LL &a){return a*a;} 21 int main() 22 { 23 inin(n);inin(a),inin(b),inin(c); 24 re(i,1,n)inin(aa[i]),sum[i]=sum[i-1]+aa[i]; 25 re(i,1,n)x[i]=1LL*a*sum[i]; 26 int l=0,r=0; 27 re(i,1,n) 28 { 29 while(l<r&&y[q[l+1]]-y[q[l]]>2LL*sum[i]*(x[q[l+1]]-x[q[l]]))l++; 30 int t=q[l]; 31 dp[i]=dp[t]+1LL*f(sum[i]-sum[t])*a+1LL*(sum[i]-sum[t])*b+c; 32 y[i]=dp[i]+1LL*a*f(sum[i])-1LL*b*sum[i]; 33 while(l<r&&(y[q[r]]-y[q[r-1]])*(x[i]-x[q[r]])>(y[i]-y[q[r]])*(x[q[r]]-x[q[r-1]]))r--; 34 q[++r]=i; 35 } 36 printf("%lld",dp[n]); 37 return 0; 38 }