[DP topic-Quadrilateral inequality optimization]51nod 1022

Source: Internet
Author: User

?

1021? Stone Merge ? V1

n Heap of stones into a line. It is now necessary to merge the stones into a pile in order. It is stipulated that each of the next 2 stones can be merged into a new pile each time, and a new pile of stones is counted as the cost of the merger. Calculates the minimum cost of merging n heap stones into a heap.

?

For example: 1 2 3 4, there are a number of consolidation methods

1 2 3 4 = 3 3 4 (3) = 6 4 (9) = 10 (19)

1 2 3 4 = 1 5 4 (5) = 1 9 (+) + 10 (24)

1 2 3 4 = 1 2 7 (7) = 3 7 (Ten) + 10 (20)

?

The total cost in parentheses shows that the first method has the lowest cost and now gives the number of n heap stones, calculating the minimum combined cost.

Input

Line 1th: N (2 <= n <= 100)

Number of 2-n + 1:n heap stones (1 <= a[i] <= 10000)

Output

Minimum combined cost of output

Input example

4

1

2

3

4

Output example

19

?

?

1022? Stone Merge ? V2

N The heap of stones was put into a ring. It is now necessary to merge the stones into a pile in order. it is stipulated that each of the next 2 stones can be merged into a new pile each time, and a new pile of stones is counted as the cost of the merger. Calculates The minimum cost of merging N heap stones into a heap.

?

For example: 1 2 3 4 , there are a number of consolidation methods

1 2 3 4 = 3 3 4 (3) = 6 4 (9) = 10 (19)

1 2 3 4 = 1 5 4 (5) = 1 9 (+) + 10 (24)

1 2 3 4 = 1 2 7 (7) = 3 7 (Ten) + 10 (20)

?

The total cost in parentheses shows that the first method has the lowest cost and now gives N Count the number of stones, calculate the minimum combined cost.

Input

Section 1 Line: N ( 2?<=? n?<=?1000)

Section 2?-? N?+?1: number of stones in N heap (1?<=? a[i]?<=?10000)

Output

Minimum combined cost of output

Input Example

4

1

2

3

4

Output Example

19

?

The first problem is better written, the complexity is N3, but the second problem needs to use the Quadrilateral inequality optimization, the complexity reduced to N2.

"Algorithmic Competition Introduction Classic Training Guide-Rujia" P170 (old version)

In the book, we refer to the optimization method of quadrilateral inequalities when we discuss the optimal ordered binary tree. In general, for a similar state transfer equation:

???? D[I,J] = max{d[i,k?1] + d[k+1,j]} + w[i,j]

The status has O (N2), each decision has O (n), so the time complexity is O (N3).

The following optimizes it to O (N2).

quadrilateral Inequalities (Monge Condition/quadrangle inequality) if for i≤i ' < j≤j ' There is always w[i,j] + w[i ', J ']≤w[i ', j] + W[i, J ' ], called W satisfies the quadrilateral inequality, or w satisfies convexity.

the monotony of the interval containing lattice if for i≤i ' < j≤j ' There is always w[i ', j]≤w[I, J '], called W satisfies monotonicity, i.e. for two intervals q1,q2 (Q2 contains Q1), W[Q1]≤W[Q2].

theorem (F.yao): If W satisfies the quadrilateral inequalities, D also satisfies the quadrilateral inequalities, i.e.

D[I,J] + d[i ', j ']≤d[I ', j] + d[I, J '], I≤i ' ≤j≤j '

Proof slightly.

Further, the convexity of D can introduce the monotonic of decision making . K[I,J] for the decision to take the minimum value for D[i,j

theorem (F.yao):k[i,j]≤k[i,j + 1]≤k[i + 1,j + 1], i≤j, that is, K is incremented in the same column as the peers.

Proof: by symmetry, just prove k[i,j]≤k[i,j + 1]. Kee Dk[i,j] = d[i,k? 1] + d[k,j] +w[i,j], then simply prove to all I < k≤k ' ≤j, if DK '[I,J]≤DK [I,j], then DK ' [I,j + 1] ≤DK [I,j + 1], that is: After the interval is extended a unit, the previous better decision is still good now . In fact, a stronger conclusion can be proved: Dk [i,j]?d k '[i,j]≤d k [i,j +1]?dk ' [i,j +1] (i <k≤k ' ≤j), because when K ' is better in [I,j] , left ≥0, by inequality know right ≥0, so K ' is still better.

For example, set K ' is the optimal value of [i,j], then for its left of any k,k ' on [i,j] better means that K ' is still optimal on [i,j+1], so K ' left of any k on [i,j+1] is still not the maximum value, that is K[i,j + 1]≥k[i,j].

to certify DK [i,j]–dk ' [I,J]≤DK [I,j + 1]–dk ' [I,j + 1] (i < k≤k ' ≤j),

Shift term: DK [i,j] +dk ' [i,j + 1]≤dk [i,j + 1] + Dk ' [i,j].

Expand by definition, both sides eliminate W[I,J] + w[i,j + 1] + d[i,k? 1] + d[i,k 0? 1]:

???????? D[K,J] + d[k ', j + 1]≤d[k,j + 1] + d[k ', j]

This is the convexity of D.

With decision Monotonicity, you can change the program a little, and make decisions from k[i,j?1] to k[i+1,j].

Does this reduce the complexity of the time? When L = j? 1 when fixed,

D[1,l + 1] The decision is k[1,l] ~ k[2,l + 1]

D[2,l + 2] Decision is K[2,L + 1] ~ k[3,l + 2]

D[3,l + 3] Decision is K[3,L + 2] ~ k[4,l + 3]

D[4,l + 4] Decision is K[4,L + 3] ~ k[5,l + 4]

...

Combined, when L fixed the total decision was k[1,l] ~ k[n? L+1,n], a total of O (n). Because L has O (n), the total time complexity is reduced to O (n 2).

?

Extended

The state transfer equation given above is only a relatively general one, in fact, many state transfer equations satisfy the conditions of optimization of quadrilateral inequalities.

The approximate steps to solve this type of problem are:

  1. prove W satisfies the quadrilateral inequalities, here W is a m the number of attachments, shaped like M[i,j]=opt{m[i,k]+m[k,j]+w[i,j]} , most of them must first prove W meet the conditions to further prove m Meet the conditions
  2. prove m satisfying quadrilateral inequalities
  3. prove S[i,j-1]≤s[i,j]≤s[i+1,j]

    ?

Whether opt is min or Max , regardless of M[i,j]=opt{m[i,k]+m[k,j]+w[i,j]} , each step of the decision is enumerated from k[i,j1] to k[i+1,j]. This will reduce the complexity. As far as 51nod is concerned, so does its steps.

[DP topic-Quadrilateral inequality optimization]51nod 1022

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.