1010: [HNOI2008] Toy boxing toy time limit:1 Sec Memory limit:162 MB
submit:7874 solved:3047
[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 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 Output1Hintsource
Ideas
Slope optimization DP.
First write slope optimization good tension =-=
The transfer equation is:
f[x]=min{f[i]+ (X-i-1+sum (x)-sum (i)-L) ^2}
i.e. f[x]=min{f[i]+ ((x+sum (x) -1-l)-(I+sum (i))) ^2}
Set A[x]=x+sum (x) -1-l, b[i]=i+sum (i) so there
F[x]=min{f[i]+b[i]^2-2*a[x]*b[i]}+a[x]^2, where x (i) =b[i],y (i) =f (i) +b[i]^2 is set, there is
F[x]=min{y (i) -2*a[x]*x (i)}, which is the straight line min P=y-2ax.
Because A[x] and X (i) are monotonically increasing, the convex hull can be maintained in a monotone queue and solved at O (n) time.
Code
1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 6 Const intMAXN =50000+Ten;7 8typedefLong LongLL;9 structPoint {LL x, y;};Ten Point NOW,D[MAXN]; OneLL c[maxn*Ten]; ALL Cross (Point a,point b,point c) {//vector BA and vector ca Fork Product <0 when CA is on the right side of Ba - return(b.x-a.x) * (C.Y-A.Y)-(B.Y-A.Y) * (c.x-a.x); - } the intn,w; - - intMain () { -scanf"%d%d",&n,&W); + for(intI=1; i<=n;i++) { -scanf"%d", &c[i]); c[i]+=c[i-1]; + } A intL=0, r=0; at for(intI=1; i<=n;i++) { - while(L<r && d[l].y-2* (i+c[i]-w-1) *d[l].x >= d[l+1].y-2* (i+c[i]-w-1) * (d[l+1].x)) l++;//Delete is not optimal for the current point -Now.x=i+c[i];//Calculate new Points -now.y=d[l].y-2* (i+c[i]-w-1) *d[l].x+ (i+c[i]-w-1) * (i+c[i]-w-1) + (I+c[i]) * (i+c[i]); - while(L<r && Cross (d[r-1],d[r],now) <=0) r--;//maintaining convex shells inserting new points -d[++r]=Now ; in } -printf"%lld\n", d[r].y-(N+c[n]) * (N+c[n]));//Calculate f[n]=y[r]-b[n]^2 to return 0; +}
Bzoj 1010 [HNOI2008] Toy packing toy (DP slope optimization)