Compare the base slope optimization DP See the following 2 blog posts
Http://www.cnblogs.com/proverbs/archive/2012/10/06/2713109.html
http://blog.163.com/myq_952/blog/static/863906320112711750378/
Main summary of slope optimization steps
1. Demonstrate the persistence of better decision points for subsequent state impact
2. Slope equation: Generalized to the left is the j,k, the right is the form of I
3. Rules governing the maintenance of queues
4. See if the decision is monotonous, if not, how to maintain
The Code of the subject
1#include <cstdio>2 #defineN 501003 #defineLL Long Long4 5 intN;6 intq[n],h,t;7 LL f[n],c[n],sum[n],g[n],l;8 9LL Sqr (ll a) {returnA *A;}TenLL X (intAintb) {returnF[A]-F[B]+SQR (g[a]+l)-SQR (g[b]+l);} OneLL Y (intAintb) {return 2* (g[a]-g[b]);} A - intMain () - { thescanf"%d%lld",&n,&l); -l++; - for(intI=1; i<=n;i++) scanf ("%lld", c+i); - for(intI=1; i<=n;i++) sum[i]=sum[i-1]+C[i]; + for(intI=1; i<=n;i++) g[i]=sum[i]+(LL) i; - for(intI=1; i<=n;i++) + { A while(H<t&&x (q[h],q[h+1]) >=y (q[h],q[h+1]) *g[i]) h++; atF[I]=F[Q[H]]+SQR (g[i]-g[q[h]]-l); - while(H<t&&x (q[t-1],q[t]) *y (q[t],i) >=x (q[t],i) *y (q[t-1],q[t]) t--; -q[++t]=i; - } -printf"%lld\n", F[n]); - return 0; in}
[Bzoj 1010] [Hnoi 2008] Toy packing toy