1010: [HNOI2008] Toy boxing toy time limit:1 Sec Memory limit:162 MB
submit:9812 solved:3978
[Submit] [Status] [Discuss] 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 Output1 ready to be sortedc handles prefixes andF[i]=min{1<=j<i}f (j) + (si-sj+i-j-1-l) ^2I together, J together, unfold two times, and get the form of slope optimization(s[i]+i) <= ......min is the lower convex shellThe x-coordinate and the slope are monocytogenes, and the monotone queue is all right.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;Const intn=5e4+5; typedefLong LongLl;inlineintRead () {CharC=getchar ();intx=0, f=1; while(c<'0'|| C>'9'){if(c=='-') f=-1; C=GetChar ();} while(c>='0'&&c<='9') {x=x*Ten+c-'0'; C=GetChar ();} returnx*F;}intn,l;ll s[n],f[n];ll F (inti) {returnf[i]+ (s[i]+1+L) * (s[i]+1+L);}DoubleSlopeintAintb) {return(f (b)-F (a))/(s[b]-s[a]);}intHead,tail,q[n];voiddp () {head=tail=1; Q[head]=0; for(intI=1; i<=n;i++){ while(Head<tail&&slope (q[head],q[head+1]) <=2*s[i]) head++; intj=Q[head]; F[i]=f[j]+ (s[i]-s[j]-1-L) * (s[i]-s[j]-1-m); while(Head<tail&&slope (Q[tail],i) <=slope (q[tail-1],q[tail]) tail--; q[++tail]=i; }}intMain () {n=read (); L=read (); for(intI=1; i<=n;i++) s[i]=s[i-1]+read (); for(intI=1; i<=n;i++) s[i]+=i; DP (); printf ("%lld", F[n]);}
Bzoj 1010: [HNOI2008] Toy packing toy [DP slope optimization]