Toy Boxing Toy Time limit:1 Sec Memory limit:162 MB
submit:8218 solved:3233
[Submit] 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 compress, which can turn any item into a pile and put it into a special one-dimensional container. Professor P has the number 1 ... N Toys, Part I toys after compression into 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. At the same time if a one-dimensional container has more than one toy, then two pieces of toys to add a unit-length filler, in the form of saying that if I toy to the J toy into a container, then the length of the container will be X=j-i+sigma (Ck) i<=k<=j The cost of making the container is related to the length of the container, according to the professor's study, if the container length is X, the cost of production is (X-l) ^2. Where L is a constant. Professor P does not care about the number of containers, he can make containers of any length, 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 Output1This problem directly pushes the formula, using slope optimization. Note to open a long long.
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intmaxn=50010;6 Long LongS[MAXN],F[MAXN];7 intST,ED,Q[MAXN];8 intMain ()9 {Ten intn,l; Onescanf"%d%d",&n,&L); A for(intI=1; i<=n;i++) -scanf"%d",&s[i]); - the for(intI=1; i<=n;i++) -s[i]+=s[i-1]; - - for(intI=1; i<=n;i++) +s[i]+=i; - +q[st=1]=0; ed=2; A for(intI=1; i<=n;i++){ at Long Longm=s[i]-l-1; - while(st<ed-1&&f[q[st+1]]-f[q[st]]+s[q[st+1]]*s[q[st+1]]-s[q[st]]*s[q[st]]<=2*m* (s[q[st+1]]-S[Q[ST]]) st++; -f[i]=f[q[st]]+ (M-s[q[st]) * (M-S[q[st]]); - while(st<ed-1&& (f[i]-f[q[ed-1]]+s[i]*s[i]-s[q[ed-1]]*s[q[ed-1]]) * (s[q[ed-1]]-s[q[ed-2]]) <= (f[q[ed-1]]-f[q[ed-2]]+s[q[ed-1]]*s[q[ed-1]]-s[q[ed-2]]*s[q[ed-2]]) * (s[i]-s[q[ed-1]]) ed--; -q[ed++]=i; - } inprintf"%lld\n", F[n]); -}
Dynamic planning (Slope optimization): Bzoj 1010 "HNOI2008" Toy packing