比较基础的斜率优化DP详见以下2篇博客
http://www.cnblogs.com/proverbs/archive/2012/10/06/2713109.html
http://blog.163.com/myq_952/blog/static/863906320112711750378/
主要总结斜率优化时的步骤
1.证明较优决策点对后续状态影响的持续性
2.求斜率方程:一般化为左边是J,K,右边是I的形式
3.规定队列的维护规则
4.看决策是否又单调性,没有的话有应该怎样维护
本题的代码
1 #include<cstdio> 2 #define N 50100 3 #define LL long long 4 5 int n; 6 int q[N],h,t; 7 LL f[N],c[N],sum[N],g[N],l; 8 9 LL sqr(LL a){return a*a;} 10 LL X(int a,int b){return f[a]-f[b]+sqr(g[a]+l)-sqr(g[b]+l);} 11 LL Y(int a,int b){return 2*(g[a]-g[b]);} 12 13 int main() 14 { 15 scanf("%d%lld",&n,&l); 16 l++; 17 for (int i=1;i<=n;i++) scanf("%lld",c+i); 18 for (int i=1;i<=n;i++) sum[i]=sum[i-1]+c[i]; 19 for (int i=1;i<=n;i++) g[i]=sum[i]+(LL)i; 20 for (int i=1;i<=n;i++) 21 { 22 while (h<t&&X(q[h],q[h+1])>=Y(q[h],q[h+1])*g[i]) h++; 23 f[i]=f[q[h]]+sqr(g[i]-g[q[h]]-l); 24 while (h<t&&X(q[t-1],q[t])*Y(q[t],i)>=X(q[t],i)*Y(q[t-1],q[t])) t--; 25 q[++t]=i; 26 } 27 printf("%lld\n",f[n]); 28 return 0; 29 }
时间: 2024-12-21 06:14:00