"Slope dp" Bzoj 1010: Toy packing

Source: Internet
Author: User

1010: [HNOI2008] Toy boxing toy time limit:1 Sec Memory limit:162 MB
submit:7537 solved:2888
[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 Output1 Long time no blog has been written to say.   Recently learning a variety of DP series, more boring so to write a few.    First, the problem of transformation.   We found that all the toys selected for J+1-i had a box (I-j-1+sigma (Ck)-L) ^2 equivalent to (sum[i]-sum[j]+i-j-1-l) ^2.    So we preprocess prefixes and what, and then set s[i]=sum[i]+i,m[i]=s[i]-1-l.   Then the answer is (M[i]-s[j]) ^2.    And the transfer should be simple: F[i]=min (f[j]+ (m[i]-s[j)) ^2).  But this transfer still requires O (n^2).   We need to optimize.  Let's first assume that the decision J is better than K.    Then you can then use the queue to maintain a convex function. O (1) transfer.
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cmath>5#include <queue>6 7 #defineMAXN 500018 9 using namespacestd;Ten  One Long LongSUM[MAXN],M[MAXN],F[MAXN]; A  - intn,l,que[50001],head=1, tail=1; -  the DoubleKintXintY) {return((Double)/(F[x]+sum[x]*sum[x]-f[y]-sum[y]*sum[y])/(Double) (Sum[x]-sum[y]) *1.0;} -  - voidDP () - { +      for(intI=1; i<=n;i++) -     { +          while(Head<tail&&k (que[head],que[head+1]) <=2*m[i]) head++; A         intsb=Que[head]; atf[i]=f[sb]+ (M[I]-SUM[SB]) * (m[i]-SUM[SB]); -          while(Head<tail&&k (Que[tail],i) <=k (que[tail-1],que[tail]) tail--; -que[++tail]=i; -     } -printf"%lld", F[n]); - } in  - intMain () to { +Freopen ("1010.in","R", stdin); -scanf"%d%d",&n,&L); the      for(intI=1; i<=n;i++) *scanf"%lld", &sum[i]), sum[i]+=sum[i-1]+1, m[i]=sum[i]-l-1; $ DP ();Panax Notoginseng     return 0; -}
View Code

"Slope dp" Bzoj 1010: Toy packing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.