Detailed problem-solving Report of stone merge (Dynamic Planning)

Source: Internet
Author: User

I. Questions
Place n piles of stones (n ≤ 100) around a garden-shaped playground, and combine the stones into a pile in order. Rules
Each time, only two adjacent heaps can be selected and merged into a new heap, and the number of stones in the new heap is recorded as the score of the merge.
Compile a program to read n heap and 20 stones into the file ),
(1) Select a method to merge stones, so that the N-1 merge, the sum of the score is the smallest;
② Select a combination of stone program, so that the N-1 merge, the total score is the largest.
For example, the number of 4 stones in each heap (starting from the top heap, clockwise) is determined
The number of requests is 4594. The total score of three merge operations is the smallest: 8 + 13 + 22 = 43
The maximum score is 14 + 18 + 22 = 54.
Input data:
The file name is input by the keyboard. The file content is:
The first behavior is the number of stone heaps N;
The second act is the number of stones in each heap, separated by a space character.
Output Data:
Output file name: output.txt
A Merge plan with the minimum score from 1st to n. Line n + 1 is empty. From row n + 2 to row 2n + 1
The maximum merge plan.
Each merge scheme is represented by N rows, where line I (1 ≤ I ≤ n) represents the number of stones in each heap before the I merge (according
Clockwise output ). The numbers of the two stones to be merged must be expressed as negative numbers for identification.

Input and Output examples:
Input File Content:
4
4 59 4
Output file content:
-4 5 9-4
-8-5 9
-13-9
22

4-5-9 4
4-14-4
-4-18
22

Ii. Algorithm Analysis
In the competition, most contestants used the greedy method of approaching the target as much as possible to merge one by one: from the top
The beginning of a stack, arranged in a sequence clockwise. Merge the two adjacent stacks with the minimum (maximum) score for the first time,
Form a new heap; next, select the smallest (largest) score in the N-1 heap of the adjacent two heap merge ......, And so on,
Until all stones are merged by the N-1 to form a pile.

For example, there are 6 piles of stones, and the number of each pile of stones (starting from the top pile, clockwise) is 3 46 5 4 2 in turn.
It is required to select a method to merge stones so that the total score is the minimum for five merge operations.
According to the greedy method, the merge process is as follows:
Score for each merge
Merge 3 4 6 5 4 2-> 5 for the first time
Merge for the second time 5 4 6 5 4-> 9
Merge for the third time 9 6 5 4-> 9
Merge for the fourth time 9 6 9-> 15
Merging for the fifth time: 15 9-> 24
24
Total score = 5 + 9 + 9 + 15 + 24 = 62

But after careful consideration, we can come up with another plan to merge stones:
Score for each merge
First merge 3 4 6 5 4 2-> 7
Merge for the second time 7 6 5 4 2-> 13
Merge for the third time 13 5 4 2-> 6
Fourth merge 13 5 6-> 11
Fifth merge 13 11-> 24
24
Total score = 7 + 6 + 11 + 13 + 24 = 61
Obviously, the latter is better than the merge scheme obtained by greedy method. The example in the question intentionally causes a greedy method to solve the problem.
Fake images trick readers into "traps ". To help readers get out of this "trap", let's clarify the following question:

1. The best merge process conforms to the best principle.
The greedy method may cause errors,
It is because the merge of the two adjacent stacks with the minimum (maximum) score is not necessarily guaranteed that the remaining merge process can lead to the optimal solution. Intelligent readers will immediately think of an ideal assumption: if the global optimal solution of a N-1 merge contains the optimal solution of each merge subproblem, therefore, the sum of the scores after such a N-1 merge is inevitably the best.
For example, in the above example, the fifth merge of stones is the adjacent two piles of 13 and 11 respectively.
These two piles of stones were first piled up from 1, 2, 3 (3, 4, 6) and 4, 5, and 6 (5, 4, 2) formed after four mergers. So the question comes down to how to make the N-2 of these two subsequences
The sum of sub-merge scores is optimal. To achieve this goal, we split 1st sequences into two more parts: 1st and 2 heap to form subsequence 1,
The 3rd heap is subsequence 2. Merge two stacks in subsequence 1 for the first time, with a score of 7;
The second merge with a heap of sub-Sequence 2 with a score of 13. Obviously, this merge scheme is optimal for 1st subsequences. Similarly, we split the 2nd sub-sequences into two parts. The 4th sub-sequences are subsequences 1, and the sub-sequences constitute subsequences 2. For the third merge, 2 heap in subsequence 2 has a score of 6. For the fourth merge with a heap in subsequence 1, with a score of 13. Obviously, this merge scheme is optimal for the second subsequence.
From this, we can draw a conclusion-6 piles of stone Sutra
After such a merge, the total score is the smallest. We divide each merge into stages. The score and status calculated in the current phase are as follows,
On the basis of the previous merge, how to define a merge plan that maximizes the current total score as a decision. Obviously, after the status of a stage is given, the decision-making of each stage in the future is not affected by the status of each section before this stage.
Therefore, the dynamic programming algorithm can be used to solve this problem.

2. Set the direction and initial value of dynamic planning
The key to solving the problem through dynamic planning is to determine the optimal merge scheme for all the stone heap subsequences. The subsequences of these stone heaps include:
{1st heap, 2nd heap}, {2nd heap, 3rd heap },...... , {Nth heap, 1st heap };
{1st heap, 2nd heap, 3rd heap}, {2nd heap, 3rd heap, 4th heap },...... , {Nth heap, 1st heap, 2nd heap };......
{1st heap ,...... , N heap} {1st heap ,...... , N heap, 1st heap }...... {N heap, 1st heap ,...... , N-1 heap}

For ease of operation, we use [I, j] to represent a number starting from the I heap, subsequence for counting J heap clockwise {I heap, I + 1 heap ,...... , The (I + J-1) mod n heap}
Its Optimal Merge plan includes two information:
① The sum of the scores of each merge in the process of merging the heap stones of the subsequence into a heap;
② Form the subsequence 1 and subsequence 2 with the optimal score and sum. Because two subsequences are adjacent, you only need to remember the number of heaps of subsequence 1;
Set
F [I, j] ── combine the J heap stones in the subsequence [I, j] into the best scores and values of a heap;
C [I, j] ── split [I, j] into two parts, and the number of heap of Its Neutron sequence 1;
(1 ≤ I ≤ n, 1 ≤ j ≤ n)
Apparently, for every pile of stones, its
F [I, 1] = 0
C [I, 1] = 0 (1 ≤ I ≤ n)
For subsequences [I, j], if the minimum score sum is obtained, the initial value of F [I, j] Is ∞. If the maximum score sum is obtained, F [I, j] The initial value is 0. (1 ≤ I ≤ n, 2 ≤ j ≤ n ).
Dynamic Planning goes smoothly (from top to bottom ). Consider n subsequences containing 2 heaps of stones (each subsequence ranges from 1st heaps, 2nd heaps ,...... , Number of N-heap, number of 2 heap clockwise) Merge Scheme
F [1, 2], F [2, 2 〕,......, F [N, 2 〕
C [1, 2], C [2, 2 〕,......, C [N, 2 〕
Then consider the n sub-sequences containing three piles of stones (each sub-sequence is from 1st, 2nd ,...... , The number of N-heap, 3 heap clockwise) Merge Scheme
F [1, 3], F [2, 3 〕,......, F [N, 3 〕
C [1, 3], C [2, 3 〕,......, C [N, 3 〕
......
And so on until n subsequences Containing N heap stones are considered (each subsequence ranges from 1st heap, 2nd heap ,...... , The number of N heap, the number of N heap clockwise) Merge Scheme
F [1, N], F [2, N 〕,......, F [N, N 〕
C [1, N], C [2, N 〕,......, C [N, N 〕
Finally, in the subsequence [1, N], [2, N 〕,......, In [N, N], select a subsequence [I, n] (1 ≤ I ≤ n) with the smallest (or largest) score sum (F value ), this starts the merge process.

3. Dynamic Planning equations and inverted merge process
For the last merge of subsequences [I, j], the score is from the I heap count, and the total number of stones in the J heap count clockwise T. The merged two heaps of stones are composed of subsequences [I, K] and [(I + k-1) mod
N + 1, J-k) (1 ≤ k ≤ J-1) formed by a limited combination. To find the K value in the Optimal Merge scheme, we define a dynamic planning equation:
When the sum of the maximum scores is obtained
F [I, j] = max {f [I, K] + F [x, J-K] + t}
1 ≤ k ≤ J-1
C [I, j] = K │ f [I, j] = f [I, K] + F [x, J-K] + T
(2 ≤ j ≤ n, 1 ≤ I ≤ n)

When the sum of the minimum scores is obtained
F [I, j] = min {f [I, K] + F [x, J-K] + t}
1 ≤ k ≤ J-1
C [I, j] = K │ f [I, j] = f [I, K] + F [x, J-K] + T
(2 ≤ j ≤ n, 1 ≤ I ≤ n)
Where X = (I + k-1) modn + 1, that is, the number of I heap, clockwise k + 1 heap number.

For example, for the 6 (3 4 6 5 4 2) Heap stones in the above example, the minimum score and the Dynamic Programming equation are used. The merge scheme of six sub-sequences containing two piles of stones is obtained in sequence.
F [1, 2] = 7 f [2, 2] = 10 F [3, 2] = 11
C [1, 2] = 1 C [2, 2] = 1 C [3, 2] = 1
F [] = 9 F [] = 6 F [6, 2] = 5
C [4, 2] = 1 C [5, 2] = 1 C [6, 2] = 1

Merging scheme of 6 (3 4 6 5 4) subsequences containing three piles of stones
F [1, 3] = 20 F [2, 3] = 25 f [3, 3] = 24
C [1, 3] = 2 C [2, 3] = 2 C [3, 3] = 1
F [4, 3] = 17 F [5, 3] = 14 F [6, 3] = 14
C [4, 3] = 1 C [5, 3] = 1 C [6, 3] = 2

Merging scheme of 6 (3 4 6 5 4) subsequences containing four piles of stones
F [] = 36 f [] = 38 f [] = 34
C [1, 4] = 2 C [2, 4] = 2 C [3, 4] = 1
F [4, 4] = 28 F [5, 4] = 26 F [6, 4] = 29
C [4, 4] = 1 C [5, 4] = 2 C [6, 4] = 3

Merging scheme of 6 (3 4 6 5 4) subsequences containing five piles of stones
F [] = 51 f [] = 48 f [] = 45
C [] = 3 C [] = 2 C [] = 2
F [4, 5] = 41 F [5, 5] = 43 f [6, 5] = 45
C [4, 5] = 2 C [5, 5] = 3 C [6, 5] = 3

Merging scheme of 6 (3 4 6 5 4) subsequences containing six piles of stones
F [] = 61 F [] = 62 f [] = 61
C [] = 3 C [] = 2 C [] = 2
F [] = 61 F [] = 61 F [6, 6] = 62
C [4, 6] = 3 C [5, 6] = 4 C [6, 6] = 3

F [] is f [], F 〕,...... The minimum value in F [6, 6] indicates that the sum of the minimum score is obtained by merging the sequence [1, 6] five times. Starting from this sequence,
The merge process is reversed as follows:
It can be seen from C [5th] = 3 that the two stones merged for the first time are obtained by merging the subsequence [1, 3] and subsequence [4, 3] four times. C [1, 3] = 2 indicates that a pile of stones merged from subsequence [1, 3] is merged from subsequence [1, 2] and the third stack. C [1st] = 1, to show that the merging scheme of sub-sequence [2nd] is to merge heaps.
From this, we can push it back to get a 1st-2nd merge plan, with a score for each merge.
Merge 3 4 6 for the first time ...... -> 7
The second merge of 7 6 ...... -> 13
13 ......
Subsequences [1, 3] are merged into 1 heap after 2 times, and the scores and = 7 + 13 = 20 for 2 times are merged.
C [4th] = 1. We can see that a pile of stones merged from subsequences [] is composed of heaps and subsequences [5,
2) merged. C [5, 2] = 1, also shows that the merging scheme of sub-sequence [5, 2] is to merge 5th heap and 6th heap. So we can push it back to get a 3rd or 4th merge plan.
Score for each merge:
The third merge ...... 54 2-> 6
The fourth merge ...... 5 6-> 11
...... 11
Subsequences [4, 3] are merged into 1 heap after two merge operations, and the scores and = 6 + 11 = 17 for the two merge operations.
The fifth merge is to merge the last two heaps into one heap, and the score for this merge is 24.
Obviously, the sum of the preceding five merge scores is the minimum
20 + 17 + 24 = 61

The above push-down process can be described by a print ([subsequence]) recursive algorithm.
Procedure print ([I, j 〕)
Begin
If j <> 1 then {continue the merge process
Begin
Print ([I, C [I, j]; {merge process of inverted subsequence 1}
Print ([I + C [I, j]-1] mod n + 1, J-C [I, j 〕)
{Merge process of reverse-pushing subsequence 2}
For K: = 1 to n do {output the current merged two stones}
If (the K heap stone is not removed from the circle)
Then begin
If (k = I) or (k = x) then sets the K heap stone to be merged
The K heap stones of else are not merged;
End; {then}
The number of I-heap stones + the number of X-heap stones;
Remove the X heap from the circle;
End; {then}
End; {print}
For example, after print ([]) is called, the result is as follows:
Print ([1, 6]) ⑤
┌ ── ─ ┴ ── ─
Print ([1, 3]) ② print ([4, 3]) ④
── ─ ┴ ── ─ ┐ ┌ ── ─ ┴ ──
Print ([1, 2]) ① print ([3, 1]) print ([4, 1]) print ([5, 2]) ③
┌ ── ─ ┴ ── ─ ┐ ┌ ── ─ ┴ ──
Print ([]) print ([]) print 〕)
Print ([6, 1 〕)
(Figure 6.2-5)
It goes back
① Display 3 46 5 4
② Display 7 65 4 2
③ Show 13 54 2
④ Display 135 6
⑤ Show 13 11
Note: After the print process is called, the total number of 6 piles of stones should be displayed as the score of the 5th merge.

Program stones;
Type
Node = record {merge plan of the current sequence}
C: longint; {score and}
D: byte {Number of heaps in subsequence 1}
End;
Sumtype = array [1 .. 100, 1 .. 100] of longint;
{Sumtype [I, j]-Total number of stones in subsequence [I, j}
VaR
List: array [1 .. 100, 1 .. 100] of node;
{List [I, j]-merging scheme of subsequences [I, j}
Date, DT: array [1 .. 100] of integer;
{Date [I]-the number of I heap stones, DT-temporary storage date}
Sum: ^ sumtype; {sum ^ [I, j]-pointer to the total number of stones in the subsequence [I, j}
F: text; {file variable}
FN: string; {file name string}
N, I, j: integer; {n-Number of stone heaps, I, j-cyclic variable}

Procedure print (I, j: byte); {recursively print the merging process of subsequences [I, j}
VaR
K, X: Random int; {k-cyclic variable; sequence number of the first heap in X-subsequence 2}
Begin
If j <> 1 then begin {continue the reverse merge process}
Print (I, list [I, j]. d); {merge process of inverted subsequence 1}
X: = (I + list [I, j]. d-1) mod n + 1; {sequence number of the first heap stone in subsequence 2}
Print (x, J-list [I, j]. d); {merge process of inverted subsequence 2}
For K: = 1 to n do {output current merge I heap, X heap stone solution}
If date [k]> 0 then begin
If (I = K) or (x = k) then write (F,-date [K], '')
Else write (F, date [K], '')
End; {then}
Writeln (f); {output line break}
Date [I]: = date [I] + date [X]; {the original I heap and X heap are merged into I heap}
Date [x]: =-date [x] {remove the original X heap from the circle}
End {then}
End; {print}

Procedure main (S: Transaction INT );
VaR
I, J, K: integer;
T, X: longint;
Begin
For I: = 1 to n do begin {the sequence containing only one pile of stones does not exist}
List [I, 1]. C: = 0;
List [I, 1]. D: = 0
End; {}
For J: = 2 to n do {2 heap, 3 heap ...... Merging scheme of sub-sequences Containing N heap stones}
For I: = 1 to n do begin {starting from the number of heap I, The subsequence of the J heap is counted clockwise}
If S = 1 then list [I, j]. C: = maxlongint {merge [I, j] subsequence score and initialization}
Else list [I, j]. C: = 0;
T: = sum ^ [I, j]; {the total number of stones in the last merge is [I, j] subsequence}
For K: = 1 to J-1 do begin {the number of stone heaps in sub-sequence 1 is regarded as 1 heap ...... J-1 heap}
X: = (I + k-1) mod n + 1; {subsequence 2 first heap number}
If (S = 1) and (list [I, K]. C + list [x, J-K]. C + T <list [I, j]. c)
Or (S = 2) and (list [I, K]. C + list [x, J-K]. C + T> list [I, j]. c)
{If this merge scheme is currently the best, write it down}
Then begin
List [I, j]. C: = list [I, K]. C + list [x, J-K]. C + T;
List [I, j]. D: = K
End {then}
End {}
End; {}
{In subsequences [1, N], [2, N],…, In [N, N], select a subsequence with the smallest (or largest) sum of scores}
K: = 1; X: = list [1, N]. C;
For I: = 2 to n do
If (S = 1) and (list [I, n]. C <X) or (S = 2) and
(List [I, n]. c> X) then begin
K: = I; X: = list [I, n]. c
End; {then}
Print (k, n); {starting from this, the merge process is reversed}
Writeln (F, sum ^ [1, N]); {output the total number of stones merged into a heap at the last time}
Writeln (f );
Writeln (list [k, n]. c)
End; {main}

Begin
Write ('file name = '); {input file name string}
Readln (FN );
Assign (F, FN); {connection between the file name string and the file variable}
Reset (f); {File Read preparation}
Readln (F, n); {Number of stones read}
For I: = 1 to n do read (F, date [I]); {Number of stones read per heap}
New (SUM); {calculate the number of stones in each subsequence sum}
For I: = 1 to n do sum ^ [I, 1]: = date [I];
For J: = 2 to n do
For I: = 1 to n do
Sum ^ [I, j]: = date [I] + sum ^ [I mod n + 1, J-1];
DT: = date; {save each heap stone before merging. variables with the same structure can be assigned values to each other}
Close (f); {close the input file}
Assign (F, 'output. txt '); {connection between file variables and output file name strings}
Rewrite (f); {file write preparation}
Main (1); {merge plan with minimum score}
Date: = DT; {restore all stones before merging}
Main (2); {merge plan with the greatest score}
Close (f) {close the output file}
 

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.