[APIO2010]特别行动队
维护一个上凸壳就行,普通的斜率优化。
若想要具体过程请参考以前博客
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #define int long long using namespace std; inline int read(){ int f=1,ans=0;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();} return f*ans; } const int N=1000011; int x[N],a,b,c,f[N],n,que[N],X[N],Y[N],l,r; signed main(){ f[0]=0; n=read(),a=read(),b=read(),c=read(); for(int i=1;i<=n;i++) x[i]=x[i-1]+read(); l=r=1,que[1]=0,X[0]=x[0],Y[0]=f[0]+x[0]*x[0]*a-b*x[0]; for(int i=1;i<=n;i++){ while(l<r&&(Y[que[l+1]]-Y[que[l]])>=2*a*x[i]*(X[que[l+1]]-X[que[l]])) l++; f[i]=f[que[l]]+(x[i]-x[que[l]])*(x[i]-x[que[l]])*a+(x[i]-x[que[l]])*b+c; X[i]=x[i],Y[i]=f[i]+x[i]*x[i]*a-x[i]*b; while(l<r&&(Y[que[r]]-Y[que[r-1]])*(X[i]-X[que[r]])<=(Y[i]-Y[que[r]])*(X[que[r]]-X[que[r-1]])) r--; que[++r]=i; }cout<<f[n];return 0; }