还是斜率优化
f[i]=f[j]+a*(sum[i]-sum[j])^2+b*(sum[i]-sum[j])+c
当j>k时,j比k优
(f[j]-f[k]+a*sum[j]*sum[j]-a*sum[k]*sum[k]-b*sum[j]+b*sum[k])/(2*a*(sum[j]-sum[k]))
ac代码
#include<bits/stdc++.h> #define maxn 1000005 #define ll long long using namespace std; ll sum[maxn],a,b,c,n; ll f[maxn],q[maxn]; ll read(){ ll x=0,c,f=1;while(!isdigit(c=getchar()))if(c=='-')f=-1; while(x=x*10+c-'0',isdigit(c=getchar())); return x*f; } ll calc(int x) { return (ll)a*x*x+b*x+c; } double K(int j,int k) { return (double)(f[j]-f[k]+a*sum[j]*sum[j]-a*sum[k]*sum[k]-b*sum[j]+b*sum[k])/(2*a*(sum[j]-sum[k])); } int main() { n=read(); a=read();b=read();c=read(); for(int i=1;i<=n;i++)sum[i]=sum[i-1]+read(); int l=0,r=0; for(int i=1;i<=n;i++) { while(l<r&&K(q[l],q[l+1])<sum[i])l++; int t=q[l]; f[i]=f[t]+calc(sum[i]-sum[t]); while(l<r&&K(q[r-1],q[r])>K(q[r],i))r--; q[++r]=i; } printf("%lld\n",f[n]); return 0; }