标签:
斜率优化dp。
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn = 50000 + 10; int n; long long L; int c[maxn],q[maxn]; long long s[maxn],f[maxn]; double slop(int j,int k) { return (f[k]-f[j]+(s[k]+L)*(s[k]+L)-(s[j]+L)*(s[j]+L))/(2.0*(s[k]-s[j])); } void dp() { int l=0,r=0; q[r++]=0; for(int i=1;i<=n;i++) { while(l<r-1 && slop(q[l],q[l+1]) <= s[i]) l++; int t=q[l]; f[i]=f[t]+(s[i]-s[t]-L)*(s[i]-s[t]-L); while(l<r-1 && slop(q[r-1],i) < slop(q[r-2],q[r-1])) r--; q[r++]=i; } } int main() { scanf("%d%lld\n",&n,&L); L++; for(int i=1;i<=n;i++) { scanf("%d",&c[i]); s[i]=(long long)s[i-1]+c[i]+1; } dp(); printf("%lld\n",f[n]); return 0; }
标签:
原文地址:http://www.cnblogs.com/invoid/p/5482842.html