题解:裸的斜率优化
少了一个括号WA了几发QWQ
总结:以后不能写这么长的式子
问题:我还不会决策单调性QWQ
#include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long Lint; const int maxn=1000009; int n; Lint A,B,C; Lint s[maxn]; Lint f[maxn]; Lint Getk(int x){ return f[x]+A*s[x]*s[x]-B*s[x]; } int q[maxn],h,t; int main(){ scanf("%d",&n); scanf("%lld%lld%lld",&A,&B,&C); for(int i=1;i<=n;++i){ int x;scanf("%d",&x); s[i]=s[i-1]+x; } q[h=t=1]=0; for(int i=1;i<=n;++i){ while((h<t)&&((Getk(q[h+1])-Getk(q[h]))>2*A*(s[q[h+1]]-s[q[h]])*s[i]))++h; int j=q[h]; f[i]=f[j]+A*(s[i]-s[j])*(s[i]-s[j])+B*(s[i]-s[j])+C; while((h<t)&&((Getk(q[t])-Getk(q[t-1]))*(s[i]-s[q[t]])<(Getk(i)-Getk(q[t]))*(s[q[t]]-s[q[t-1]])))--t; q[++t]=i; } printf("%lld\n",f[n]); return 0; }