BZOJ1010 [HNOI2008] Toy packing toy

Source: Internet
Author: User

This article is copyright ljh2000 and Blog Park is shared, welcome reprint, but must retain this statement, and give the original link, thank you for your cooperation.

this article ljh2000
Author Blog: http://www.cnblogs.com/ljh2000-jump/
Reprint please specify the source, infringement must investigate, retain the final interpretation right!
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

Positive solution: Decision monotonicity +dp+ Monotone queue

Problem Solving Report:

A long time ago, a question of examination, the problem directly with seniors:

  

First greedy is obviously wrong (although you can go through the example), want to find a counter-example of a random look at a test data bar.
because the number of toys in the same container must be continuous, it can be resolved with a dynamic plan if no effect is met. A relatively easy to think O (n^2) algorithm is, with F I for the minimum cost of loading the first toy, S I for the total length of the first toy, then the state
Transfer equation is:
F i = min {f j + (S I? S j + I? (j + 1)? L) 2}0≤j<i
But since n can reach 50000, the expected score is 10~20.
However, something really rather bull B came.
Make S i′= s i + I, l′= L + 1, let's re-observe the original equation:
F i = min {f j + (S i′)? S j′? l′) 2} 0≤j<i
V j = F J + (S i′?) S j′? l′) 2, then at x < y, V x≥v y can be deformed:
F x? F y≥ (S i′?) S y′? l′) 2? (S i′?) S x′? l′) 2
? F x? F y≥ (2S i′? S x′? S y′? 2l′) (S x′?) S y′)
? F x? F y≥ (2 (S i′?) l′)? (s x′+ s y′)) (S x′?) S y′)
? F x? F y≥2 (S i′?) l′) (S x′?) S y′)? (s x′+ s y′) (S x′?) S y′)
? F x? F y + S x′? S y′≥2 (S i′?) l′) (S x′?) S y′)
In this case, if for the current I, there are x < Y, V X≥v y, then for the later larger I, because of 2 (S i′? l′) monotonicity, V x≥v y
will be constant, that is, X can no longer be the optimal decision, we will never use it again.

Because decision I may be better than some decisions in the original queue, if you do not delete these definitely useless decisions, the non-optimal decisions in front of the queue may be "protected" by one of their decisions and not deleted, in fact, so that the value of function g is monotonically increasing in Q.
Because each element is enqueued at most one time, the queue is maintained with an O (n) complexity, the state transitions to O (1), and the F value is O (n), so the total time complexity is O (n) + O (1) xo (n) = O (n).

1 //It's made by jump~2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <algorithm>8#include <ctime>9#include <vector>Ten#include <queue> One#include <map> A#include <Set> - using namespacestd; -typedefLong LongLL; the ConstLL inf = (1ll<< -); - Const intMAXN =50011; - intN,a[maxn],dui[maxn],head,tail; - LL L,F[MAXN],SUM[MAXN],S[MAXN]; +  -InlineintGetint () + { A     intw=0, q=0;CharC=GetChar (); at      while((c<'0'|| C>'9') && c!='-') C=getchar ();if(c=='-') q=1, c=GetChar (); -      while(c>='0'&& c<='9') w=w*Ten+c-'0', C=getchar ();returnQ? -w:w; - } -  -InlineBOOLCheckintXintYinti) { -X=DUI[X]; y=Dui[y]; inLL ji=f[x]-f[y]+s[x]*s[x]-s[y]*S[y]; -LL comp=2* (S[I]-L); comp*=s[x]-S[y]; to     if(Ji>=comp)return true;return false; + } -  theInlineBOOLCheck2 (intXintYinti) { *X=DUI[X]; y=Dui[y]; $LL Ji1=f[x]-f[y]+s[x]*s[x]-s[y]*s[y]; Ji1/= (s[x]-s[y]);Panax NotoginsengLL Ji2=f[y]-f[i]+s[y]*s[y]-s[i]*s[i]; Ji2/= (s[y]-s[i]); -     if(JI1&GT;=JI2)return true;return false; the } +  AInlinevoidWork () { theN=getint (); L=getint (); for(intI=1; i<=n;i++) A[i]=getint (), sum[i]=sum[i-1]+a[i],s[i]=sum[i]+i; +Head=tail=0; dui[head]=0; l++; -      for(intI=1; i<=n;i++) { $      while(Head<tail && Check (head,head+1, i)) head++; $f[i]=f[dui[head]]+ (s[i]-s[dui[head]]-l) * (s[i]-s[dui[head]]-L); -      while(Head<tail && Check2 (tail-1, tail,i)) tail--; -dui[++tail]=i; the     } -printf"%lld", F[n]);Wuyi }  the  - intMain () Wu { - Work (); About     return 0; $}

BZOJ1010 [HNOI2008] Toy packing toy

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.