"Slope optimization" "Hnoi 2008" "Bzoj 1010" Toy packing toy

Source: Internet
Author: User

1010: [HNOI2008] Toy packing toy
Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 7449  Solved: 2854

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 Input

5 434214

Sample Output

1

Exercises

First slope optimization.
I studied here--well, that's great.

Code:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <algorithm>using namespace Std;#define N 50100#define LL Long LongintN LL L,s1[n],s2[n],Q[n], F[n];intIn () {int x=0; Char Ch=getchar (); while(ch<' 0 '|| Ch>' 9 ') Ch=getchar (); while(ch>=' 0 '&& ch<=' 9 ')x=x*10+ch-' 0 ', Ch=getchar ();return x;} ll Lin () {llx=0; Char Ch=getchar (); while(ch<' 0 '|| Ch>' 9 ') Ch=getchar (); while(ch>=' 0 '&& ch<=' 9 ')x=x*10+ (LL) (ch-' 0 '), Ch=getchar ();return x;} LL Calc (int x,int y){returnf[x]-f[y]+ (s2[x]+L)*(s2[x]+L)-(s2[y]+L)*(s2[y]+L);} void DP () {intH=0, t=1;Q[h]=0; Memset (F,0, sizeof (f)); for(intI=1; i<=n; i++) { while(h<t-1&& Calc (Q[h+1],Q[h]) <= (2*(s2[Q[h+1]]-s2[Q[h]])*s2[i])) h++; f[i]=f[Q[h]]+ (s2[i]-s2[Q[h]]-L)*(s2[i]-s2[Q[h]]-L); while(h<t-1&& Calc (i,Q[t-1])*(2*(s2[Q[t-1]]-s2[Q[t-2]])) <=calc (Q[t-1],Q[t-2])*(2*(s2[i]-s2[Q[t-1]])) t--;q[t++]=i; }}intMain () {n=in (), L=lin () +1; s1[0]=0; for(intI=1; i<=n; i++) {S1[i]=lin () +s1[i-1];    s2[i]=s1[i]+ (LL) i; } DP ();printf("%lld\ n", F[n]);return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Slope optimization" "Hnoi 2008" "Bzoj 1010" 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.