斜率优化实现初步(4) [BZOJ][1911][Apio2010]特别行动队
#include<bits/stdc++.h> using namespace std; #define int long long const int MAXN=1e6+2333; int n,a,b,c; int x[MAXN],sum[MAXN]; int f[MAXN],g[MAXN]; int head=1,tail=1,q[MAXN]; char readc(){ static char buf[100000], *l=buf,*r=buf; if (l==r) r=(l=buf)+fread(buf,1,100000,stdin); if (l==r) return EOF;else return *l++; } int read(){ int x=0,f=1;char ch=readc(); while (!isdigit(ch)) { if (ch=='-') f=-1; ch=readc();} while (isdigit(ch)) x=x*10+ch-48,ch=readc(); return x*f; } int slope(int j,int k){ return (g[j]-g[k])/(sum[j]-sum[k]); } int pow2(int x){ return x*x; } signed main(){ n=read(); a=read(),b=read(),c=read(); for (int i=1;i<=n;i++) x[i]=read(),sum[i]=sum[i-1]+x[i]; for (int i=1;i<=n;i++){ while (head<tail&&slope(q[head+1],q[head])>2*a*sum[i]+b) head++; f[i]=f[q[head]]+a*pow2(sum[i]-sum[q[head]])+b*(sum[i]-sum[q[head]])+c; g[i]=f[i]+a*pow2(sum[i]); while (head<tail&&slope(i,q[tail])>slope(q[tail],q[tail-1])) tail--; q[++tail]=i; } printf("%lld\n",f[n]); return 0; }