1319 Toy packing
time limit: 1 sspace limit: 128000 KBtitle level: Diamonds DiamondTitle Description
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.
Enter a description
Input Description
The first line enters two integers n,l. Next N Lines Enter ci.1<=n<=50000,1<=l,ci<=10^7
Output description
Output Description
Minimum cost of output
Sample input
Sample Input
5 4
3
4
2
1
4
Sample output
Sample Output
1
Data range and Tips
Data Size & HintCode:
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 #defineINF 10000000006 #definell Long Long7 using namespacestd;8 9 intN,l,l,r;Ten intc[50005],q[50005]; Onell s[50005],f[50005],c; A DoubleSlopintJintk) - { - return(f[k]-f[j]+ (s[k]+c) * (s[k]+c)-(s[j]+c) * (S[J]+C))/(2.0* (s[k]-s[j])); the } - voidDP () - { -L=1; r=0; q[++r]=0; + for(intI=1; i<=n;i++) - { + while(L<r&&slop (q[l],q[l+1]) <=s[i]) l++; A intt=Q[l]; atf[i]=f[t]+ (s[i]-s[t]-c) * (s[i]-s[t]-C); - while(L<r&&slop (Q[r],i) <slop (q[r-1],Q[R]) r--; -q[++r]=i; - } - } - intMain () in { -scanf"%d%d", &n,&l); c=l+1; to for(intI=1; i<=n;i++) scanf ("%d",&c[i]); + for(intI=1; i<=n;i++) s[i]=s[i-1]+C[i]; - for(intI=1; i<=n;i++) s[i]+=i; the DP (); *printf"%lld\n", F[n]); $ return 0;Panax Notoginseng}
C + + Path advanced--Queue optimization DP (Toy boxing)