f[i]表示a[1]+…+a[i]+i。为了方便处理。。。dp[i]表示把前i件压缩需要的最少费用。则
dp[i]=min{dp[j]+(f[i]−f[j]−c)2|1≤j<i}
若k1< k2且k1优于k2,则:
(dp[k2]+(f[k2]+c)2−dp[k1]−(f[k1]+c)2)/(2∗(f[k2]−f[k1]))>f[i]
。依然是维护一个下凸曲线,保证队列中的斜率是单增的。
#include <cstdio>
#include <cstring>
#define ll long long
int const N=50000+10;
ll dp[N],f[N];
int n,l,q[N],h=0,t=0,c=0;
inline int read(){
int x=0;char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x;
}
inline ll sqr(ll x){return x*x;}
inline double slope(int k1,int k2){
return (dp[k2]-dp[k1]+sqr(f[k2]+c)-sqr(f[k1]+c))*1.0/(2.0*(f[k2]-f[k1]));
}
int main(){
// freopen("a.in","r",stdin);
n=read();l=read();c=l+1;
for(int i=1;i<=n;++i) f[i]=read(),f[i]+=f[i-1]+1;
for(int i=1;i<=n;++i){
while(h<t&&slope(q[h],q[h+1])<f[i]) ++h;
dp[i]=dp[q[h]]+sqr((f[i]-f[q[h]]-c));
while(h<t&&slope(q[t],i)<slope(q[t-1],q[t])) --t;
q[++t]=i;
}
printf("%lld",dp[n]);
return 0;
}