Bzoj 1010: [hnoi2008] toy packing

Source: Internet
Author: User

Question address: http: // www. lydsy. com/judgeonline/problem. php? Id = 1010

For more information, see the original question.

Algorithm analysis:

If s [I] is set to the prefix and f [I] of C [I], it indicates the minimum cost from 1st items to I items.

The easy-to-obtain DP equation is f [I] = min (F [J] + (s [I]-s [J] + I-j + 1-L) ^ 2 ).

If t [I] = s [I] + I is set, F [I] = min (F [J] + (T [I]-T [J] + 1-L) ^ 2.

If M = T [I]-L-1, F [I] = min (F [J] + (m-T [J]) ^ 2 ).

When making a decision on I, set J and K to two general decision points. Set j <K.

If K is better than J

F [k] + (m-T [k]) ^ 2 <F [J] + (m-T [J]) ^ 2

Expand, get

F [k] + m ^ 2-2 * m * t [k] + T [k] ^ 2 <F [J] + m ^ 2-2 * m * t [j] + T [J] ^ 2

Simplified

(F [k] + T [k] ^ 2)-(F [J] + T [J] ^ 2 )) /(t [k]-T [J]) <2 * m = 2 * (s [I] + i-l-1)

Therefore, we can use monotonous queues to optimize the DP.

Pseudo code of the monotonous queue-optimized DP:

for (int i=1;i<=n;++i){while (h+1<t && slope(q[h],q[h+1])<...) h++;f[i]=...;while (h+1<t && slope(q[t-2],q[t-1])>slope(q[t-1],i)) t--;q[t++]=i;}

Two important conclusions of slope optimization (for this question ):

1) j <K, if slope (j, k) <2 * (s [I] + i-l-1), k is better than J. (PS: The left side of the inequality is the formula child related only to J and K, and the right side of the inequality is the formula child related only to I)

2) j <k <L. If slope (j, k)> slope (K, L), K can be removed.

Code: [to be added]

By Charlie pan

Jul 18,2014

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.