Merge dynamic planning, merge stones, and merge stone solution reports

Source: Internet
Author: User
Stone merge
1: Any Version
There are n piles of stones, and now we want to combine them into a pile of stones in an orderly manner. The rule is as follows: we can only move any two piles of stones to merge each time, and the merge cost is the number of piles of stones. Design an algorithm that combines the N stones into a pile with the minimum (or maximum) total cost ).
This type of problem is simple, that is, the deformation of the Harman code. The greedy algorithm can be used to obtain the optimal solution. That is, the minimum two stacks are selected each time, and the new stacks are merged until only one pile is left. For the proof process, refer to the proof process of Homan.
2. Chain merge

Problem description

N piles of sand are arranged in a row numbered 1, 2, 3 ,..., N (n <= 100 ). Each pile of sand has a certain amount. Now we need to add n piles of sand into a pile. The process of merging can only be heap into a pile of adjacent two piles of sand each time, so that after the N-1 is merged into a pile. Find a reasonable merge method to minimize the total cost.

For example, there are three piles of sand, with the numbers of 13, 7, and 8 respectively. There are two merge schemes. The first one is to merge 1 and 2, and the number of sand in the merged pile is 20, the merge cost is 20, and then the new heap and the 3rd heap sand are merged. The number of sand after the merge is 28, and the merge cost is 28, the total cost of merging three piles of sand into one is the first merge price 20 plus the second merge price 28, that is, 48. The second solution is to merge heap 2 and 3 first, the number of new piles of sand after merging is 15, and the price of this merge is 15. Merge the new pile with the 1st piles of sand. The number of sand after merging is 28, and the price of this merge is 28, the total cost of merging three piles of sand is the first merge price 15 plus the second merge price 28, that is, 43. The second solution can achieve the minimum total cost, with a value of 43.

[Input format]

The input consists of several rows. The first row has an integer N (1 ≤ n ≤ 100), indicating the number of sand heaps. The rows from 2nd to m + 1 are the amount of sand per pile.

[Output format]

An integer at the minimum cost of merging.

[Input example]

Input File Name: shizi. In

7
13
7
8
16
21
4
18

[Output example]

Output file name: shizi. Out

239

Matrix concatenation is very similar to this type of problem. Matrix concatenation merges adjacent two matrices (only in Different Calculation Modes ). Then, the problem of stone merge can be solved by matrix concatenation.
So what is the optimal sub-structure? If there are n heap, the first operation is certainly from n-1 to select a pair to merge, the second from The N-2 to select a pair to merge, and so on ......

Obtain the W prefix and S, s [0] is set to 0, s [I] indicates W [1] + W [2] +... + W [I]. In this case, W [I] + W [I + 1] +... + W [J] is s [J]-s [I-1].

F [I] [J] indicates the minimum cost of merging heap I to heap J, then f [I] [J] = min (F [I] [k] + F [k + 1] [J] + s [J]-s [I-1]) (I <= k <j), f [I] [J] is initially set

Int_max.

Three Layers of loops, the outermost layer enumerative of a string of merged lengths Len, minimum is 2 piles of stones merged, maximum is n heap.

Then enumerate I, then J is the I + len-1, enumeration between I and j break K, comparison.

DP process:

Stage: Taking the length of the stones as the stage, there are a total of N-1 stages.
Status: the number of stones in each stage to be merged. If the length of Angelica is 2, there are n-1 states;
Angelica and length is 3, there is a state of N-2;
When the length is N, there is 1 state.
Decision-making: when the length of Angelica is 2, there is one decision; when the length of Angelica is 3, there are two decisions;
N-1 decision is made when the length of Angelica is N.

# Include <iostream> using namespace STD; # define M 101 # define INF 000000000int N, F [m] [m], sum [m] [m], stone [m]; int main () {int I, J, K, T; CIN> N; for (I = 1; I <= N; I ++) scanf ("% d", & stone [I]); for (I = 1; I <= N; I ++) {f [I] [I] = 0; sum [I] [I] = stone [I]; for (j = I + 1; j <= N; j ++) sum [I] [J] = sum [I] [J-1] + stone [J];} For (INT Len = 2; Len <= N; Len ++) // The length of the merged stone {for (I = 1; I <= N-len + 1; I ++) // I is the starting point, J is the end {J = I + len-1; // due to Len, it indicates that there are Len stones to merge, starting from I, because only adjacent stones can be merged, so the end position J = I + len-1 f [I] [J] = inf; // The initial values are set to infinity for (k = I; k <= J-1; k ++) // locate the center disconnection and find the position where the breakpoint K is set in [I, j, f [I] [J] obtain the optimal value {If (F [I] [J]> F [I] [k] + F [k + 1] [J] + sum [i] [J]) f [I] [J] = f [I] [k] + F [k + 1] [J] + sum [I] [J] ;}} printf ("% d/N", F [1] [N]); Return 0 ;}

3. What is the optimal value if the stones are arranged in a circle and the remaining conditions remain unchanged?

[Description]

Place n piles of stones around a circular playground, and merge the stones into a pile in sequence. it is specified that only two adjacent piles can be selected at a time to merge into a new pile, and the number of stones in the new pile is recorded as the score of the merge. An algorithm is designed to calculate the minimum score and the maximum score for merging n stones into 1 heap.

[Input format]

The positive integer N, 1 ≤ n ≤ 1st in the first row of the data indicates that there are n piles of stones. N in the second row indicates the number of stones in each pile.

[Output format]

2 rows in total output, 1st minimum behavior score, 2nd maximum behavior score

[Example input]

4

4 4 5 9

[Sample output]

43

54

[Source]

Hzoi


Because the circle is connected at the beginning and end, the first thought seems to be totally different from the line arrangement. Because after each merge, we have to consider the last merging relationship with the first one. The optimal sub-structure of the linear version is gone. F (I, j) indicates that the optimal value of I-> J merging does not seem feasible, because the first step we can get the optimal value is to merge the first and last values. Then f (I, j) does not indicate this relationship.
Modify it. f (I, j) indicates that the optimal value is obtained by merging the J values starting from the I. Sum (I, j) indicates the sum of the numbers from the I to the I + J. Then the problem is solved. Consider it as a ring, that is, merging within a finite field.

The recursive formula is as follows:




# Include <cstdlib> # include <cstdio> # include <cmath> # include <algorithm> using namespace STD; # define maxn 100int sum [maxn]; int mins [maxn] [maxn], Maxs [maxn] [maxn]; int int_max = 999999999; int N, stone [maxn]; int sums (int I, Int J) {if (I + j> = N) {return sums (I, n-I-1) + sums (0, (I + J) % N );} else {return sum [I + J]-(I> 0? Sum [I-1]: 0) ;}} void getbest (Int & minnum, Int & maxnum) {// initialization, no merging, cost: 0for (INT I = 0; I <n; ++ I) {mins [I] [0] = Maxs [I] [0] = 0 ;} // The number of merging times for (Int J = 1; j <n; ++ J) {for (INT I = 0; I <n; ++ I) {mins [I] [J] = int_max; Maxs [I] [J] = 0; For (int K = 0; k <j; ++ K) {mins [I] [J] = min (mins [I] [k] + mins [(I + k + 1) % N] [J-k-1] + sums (I, j), Mins [I] [J]); maxs [I] [J] = max (Maxs [I] [k] + Maxs [(I + k + 1) % N] [J-k-1] + sums (I, j), Maxs [I] [J]) ;}} minnum = mins [0] [n-1]; maxnum = Maxs [0] [n-1]; for (INT I = 0; I <n; ++ I) {minnum = min (minnum, Mins [I] [n-1]); maxnum = max (maxnum, Maxs [I] [n-1]);} int main () {scanf ("% d", & N); For (INT I = 0; I <n; ++ I) scanf ("% d ", & stone [I]); sum [0] = stone [0]; for (INT I = 1; I <n; ++ I) {sum [I] = sum [I-1] + stone [I];} int minnum, maxnum; getbest (minnum, maxnum ); printf ("% d/n % d/N", minnum, maxnum); Return 0 ;}


Solution 2:

For the linear merge stone problem, the DP model is similar to the type of DP question of "brackets", and f (I, j) is the optimal solution obtained by merging item I to item j.
The key is that this question is circular. The ring structure often uses double-length linearity. That is to say, the ring structure is treated as a linear structure with twice the length of the ring.
The length of the ring is N, so the question is equivalent to a row of stones 1... n + 1... n, and then we can use linear stones to merge the problem.
Note that f (I, j) is always equal to F (n + I, n + J), so unnecessary computation can be reduced.

The key to this question is to turn the ring into a linear structure, which is enclosed by N numbers. The maximum sum of consecutive numbers is the same.

Convert the linear table with N structure to a linear table with 2n structure with double length. Then, in the 2N length table, extract the part with N length.

# Include <iostream> # include <stdio. h> # include <string. h> # include <string> # include <limits. h> using namespace STD; int DPX [1100] [110], p [1100] [1100], s [1100], DP [1100] [1100]; int anx, any; int main () {int N; while (CIN> N) {memset (p, 0, sizeof (p); For (INT I = 1; I <= N; I ++) {CIN> S [I]; s [n + I] = s [I] ;}for (INT I = 1; I <= 2 * n; I ++) {P [I] [I] = s [I]; for (Int J = I + 1; j <= 2 * n; j ++) P [I] [J] = P [I] [J-1] + s [J];} memset (DP, 0, sizeof (DP); For (INT r = 1; r <n; r ++) // R indicates the number of stones to be merged {for (INT I = 1; I <= 2 * n-R; I ++) // I indicates merging stones. The starting position is {Int J = R + I; // J indicates merging R stones, position of the merge sequence ending DPX [I] [J] = int_max; // initialize for (int K = I; k <j; k ++) {DP [I] [J] = max (DP [I] [J], DP [I] [k] + dp [k + 1] [J] + P [I] [J]); DPX [I] [J] = min (DPX [I] [J], DPX [I] [k] + DPX [k + 1] [J] + P [I] [J]) ;}} anx = 0; Any = int_max; for (INT I = 1; I <= N; I ++) // IN THE 2n heap, the maximum number of consecutive sequences whose lengths are N is obtained, min {anx = max (anx, DP [I] [n + I-1]); Any = min (any, DPX [I] [n + I-1]);} printf ("% d \ n", any); printf ("% d \ n", anx);} return 0 ;}




Merge dynamic planning, merge stones, and merge stone solution reports

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.