1010: [HNOI2008] Toy packing toy
Description
Professor P was going to see the Olympics, but he couldn't give up his toys, so he decided to ship all the toys to Beijing. He uses his own compressor to press
It can turn any item into a pile and then into a special one-dimensional container. Professor P has the number 1 ... n Toys, item I Toys
After compression becomes a one-dimensional length of ci. For ease of finishing, Professor P requires that the number of toys in a one-dimensional container be continuous. And if a one-dimensional capacity
There are a number of toys in the device, then two pieces of toys to add a unit length of the filler, in the form of saying that if I toy to the J Toys put to a
container, the length of the container will be X=j-i+sigma (Ck) i<=k<=j the cost of making a container is related to the length of the container, according to the professor,
If the container length is X, its production cost is (X-L) ^2. Where L is a constant. Professor P does not care about the number of containers, he can make any length of the capacity
Even more than L. But he wants the cost to be minimal.
Input
The first line enters two integers n,l. Next N Lines Enter ci.1<=n<=50000,1<=l,ci<=10^7
Output
Minimum cost of output
Sample Input5 4
3
4
2
1
4Sample Output1Exercises
Slope optimization DP,
Setting Dp[i]
Dp[i] = Min{dp[j] + (i-j-1+s[i]-s[j]-l) ^2} j<i;
Observe the other p[i] = S[i]+i; l=l+1;
Then finishing get dp[i] = Min{dp[j] + (p[i]-p[j]-l) ^2};
There's no proof of monotonicity here.
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<queue>using namespacestd;Const intN = 5e4+ -, inf = 2e9, mod = 1e9+7; typedefLong Longll;ll n,l,r,q[n];ll dp[n],p[n],l,c[n];ll sqr (ll x) {returnx*x;}DoubleGetsum (intJintk) { return(Double) (DP[J]-DP[K]+SQR (P[j])-SQR (P[k]) +2*l* (P[j]-p[k])/(Double)(2.0* (p[j]-P[k])) ;}intMain () {scanf ("%lld%lld",&n,&l); L= L +1; for(intI=1; i<=n;i++) {scanf ("%lld",&C[i]); P[i]=p[i-1]+c[i]+1; } l=0; for(intI=1; i<=n;i++) { while(L<r&&getsum (q[l+1],Q[L] <p[i]) l++; intnow=Q[l]; Dp[i]= Dp[now] + SQR (p[i]-p[now]-m); while(L<r&&getsum (I,q[r]) <getsum (q[r],q[r-1])) r--; q[++r]=i; } printf ("%lld\n", Dp[n]); return 0;}
Bzoj 1010: [HNOI2008] Toy packing toy slope optimization DP