Bzoj 1010 Toy packing Slope optimization DP

Source: Internet
Author: User

See http://www.cnblogs.com/proverbs/archive/2013/02/01/2713109.html for details (I think this is enough detail, I will not repeat it)

Let's recall the course of doing this problem! At first it was a bit of a mistake, but it came back immediately and clearly test instructions. Wrote a n^2 algorithm. Obviously, for the data of N <= 50000, it must be tle. (quoting me to read the blog process to see a sentence to describe is "the provincial selection of data is hard.") No way, only the internet to find Baidu (too weak). At first it was a bit dazed, but immediately decided to deduce it yourself, and the idea was clear. But there are doubts about the correctness of the algorithm, so I think about it all afternoon (the class is stupefied). Then I got a rough idea.

In the beginning there are doubts, why can directly in the queue after the first maintenance to take the first place? The reason is that I < J if I am better at first J than I will be kicked out of the queue. So why not set I < J < K in J Although I'm worse than I but K is better than I! The reason is that if I < J < K in this position, we will be in the end of the maintenance process to remove this situation. So you can take the first place directly in the queue after the first maintenance of the team.

Slope optimization is the fact that each state sees a discrete point in a Cartesian coordinate system that abstracts the X, y representation of the slope (y2-y1)/(X2-X1) in relation to a relationship state I function, and then maintains the point to see the convex or convex nature of the slope. The specific situation depends on the monotonicity of the functions of I.

To say some things to note:

Note that the slope as far as possible multiplication, not the real number (indeed, the error is very large), if necessary to use a long long, so as not to overflow. Note that x2-x1 may be less than 0 inequalities to change the negative sign ;

The question starts with int, WA.

1#include <cstdio>2#include <iostream>3 #defineRep (i,j,k) for (int i = j; I <= K; i++)4 #defineDown (i,j,k) for (int i = j; I >= K; i--)5 #defineSqr (i) (i) * (i)6 #defineMAXN 500107 #defineLL Long Long8 using namespacestd;9  Ten LL SUM[MAXN], C[MAXN], DEP[MAXN], DP[MAXN]; One DoubleSlopeintIintj) { A     return  1.0* (DP[I]+SQR (sum[i])-DP[J]-SQR (Sum[j])/(sum[i]-sum[j]); - } -   the intMain () - { - LL N, l, X; -scanf"%lld%lld", &n,&l); +c[0] =0; -Rep (I,1, N) { +scanf"%lld",&x); AC[i] = c[i-1] +x; atSum[i] = C[i] +i; -     } -LL s =0, t =0; -dp[0] =0, dep[0] =0; -Rep (I,1, N) { -LL m = sum[i]-L-1; in          while(S < T && Slope (dep[s+1],dep[s]) <=2* m) s++; -LL j =Dep[s]; toDp[i] = Dp[j] + sqr (M-Sum[j]);  +          while(S < T && Slope (dep[t],dep[t-1]) >= slope (i,dep[t)) t--; -DEP[++T] =i; the     } *cout<<dp[n]<<Endl; $     return 0;Panax Notoginseng}
1010: [HNOI2008] Toy boxing toy time limit:1 Sec Memory limit:162 MB
submit:7823 solved:3015
[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

Attached POJ 3709

This problem is similar to the above, this is more than a group should have at least the number of K limit. On this quote from the blog to see

"Well, the following considerations each group is not less than the K element of this limit.

To resolve this limitation, just delay the time to join.

If a delay of K-1 rounds is added, it is possible to make the previous group less than K.

This is not the case if you delay the 2*k-1 of a round join. But the number to be added at this time should be i-k+1 (assuming the first round) "//Focus

As well as delay in the timing, the tail maintenance of the code will also have to be modified (check the wrong half a day).

See http://blog.sina.com.cn/s/blog_5f5353cc0100jxxo.html for details.

1#include <cstdio>2#include <iostream>3#include <cstring>4 #defineRep (i,j,k) for (int i = j; I <= K; i++)5 #defineMAXN 5000106 #defineLL Long Long7 using namespacestd;8 9 LL NUM[MAXN], SUM[MAXN], F[MAXN], Q[MAXN];Ten inthead, tail; One  ALL G (intKintj) { -        returnf[j]-f[k]-sum[j]+sum[k]+j*num[j+1]-k*num[k+1]; - } the   -LL S (intKintj) { -        returnnum[j+1]-num[k+1]; - } +  - intRead () + { A     ints =0, t =1;Charc =GetChar (); at      while( !IsDigit (c)) { -         if(c = ='-') T =-1; c =GetChar (); -     } -      while(IsDigit (c)) { -s = S *Ten+ C-'0'; c =GetChar (); -     } in     returnS *T; - } to  + intMain () - { the     intt =read (); *      while(t-- ){ $         intn = Read (), k =read ();Panax Notoginsengsum[0] = f[0] = q[0] = head = Tail =0; -Rep (I,1, N) { theNum[i] =read (); +Sum[i] = sum[i-1] +Num[i]; A         } theRep (I,1, N) { +              while(Head < tail && G (q[head+1],q[head]) >=i*s (q[head+1],q[head]) head++; -             intj =Q[head]; $F[i] = F[j] + sum[i]-sum[j]-num[j+1] * (I-j); $             if(i>=2*k-1 ) { -q[++tail]=i-k+1; -             } the              while(Head < tail && G (q[tail-1],q[tail-2]) * S (q[tail],q[tail-1]) >= G (q[tail],q[tail-1]) * S (q[tail-1],q[tail-2] ) ) { -q[tail-1] =Q[tail];Wuyitail--; the             } -         } Wucout<<f[n]<<Endl; -     } About     return 0; $}

Convert the title to: a sequence of ascending, n elements, grouped. Requires that each group of elements not less than K, calculate the difference between the elements of the group and the smallest element of the sum, the value of each group is added together, and to the minimum.

It is easy to draw a conclusion that continuous fetching is better than discrete results (it is easy to pass, so it is not proven).

Bzoj 1010 Toy packing Slope optimization DP

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.