Black Book DP album

Source: Internet
Author: User

Original article in the black book: All content in this section is very important, especially for examples. readers who are determined to master DP should grasp all the time. The difficulty in this section is the understanding of the model, including some difficult questions and some problems in the dynamic planning theory. After learning this section, you should be able to complete your ideas for each question in a short time. For most questions, you should be able to write programs quickly.

Here are several conventions: 1. Add the address for submitting each question. 2. Only the transfer equation and IDEA are written for each question, and no code is posted, because I will repeat it later. 3. I am a blind man.

 

 

 

1.5.1 bracket sequence poj 1141

The classic example in the black book. DP [I] [J] indicates the minimum number of parentheses added from I to J.

DP [I] [J] = min {

DP [I + 1] [J-1] s [I] s [J] = "()" or s [I] [J] = "[]" ...... 1

DP [I] [k] + dp [k + 1] [J] divides s into Si... Sk, SK + 1... SJ ............ 2

} DP [I] [I] = 1;

Here we have simplified the Writing Method in the black book and found that (s ', [s', s '), s'] these situations are in Si... SK, SK + 1... it is included in SJ.

This makes the output of the matching sequence... So it's tangled... Refer to here

The idea is to use POS [I] [J] to represent the split position of (I, j) segments. Initially, the case is-1. When DP [I] [J] = DP [I] [k] + dp [k + 1] [J] appears, the split position is POS [I] [J] = K.

Output the sequence of parentheses according to POS [] [] DFS.

 

 

1.5.1 Board division poj 1191

A rare Chinese question.

The mean variance is reduced to σ = (1/n) Σ xi2)-(△x) 2. Writing on paper is simple, and the key is to think of it... It is known that △x is a fixed value. To ensure that σ is the smallest, you only need to ensure that xi2 is the smallest. S (x1, Y1, X2, Y2) indicates the sum of all values in the range (x1, Y1) (X2, Y2. DP (K, X1, Y1, X2, Y2) indicates (x1, Y1) (X2, Y2) the minimum Σ xi2 value obtained by dividing k + 1 segments by adding K cut lines. Of course, there are two cases: horizontal cut and vertical cut. Transfer equation:

DP (K, X1, Y1, X2, Y2) = {

Min (dp (K-1, X1, Y1, I, Y2) + S (I + 1, Y1, X2, Y2) 2, DP (K-1, I + 1, Y1, x2, Y2) + S (x1, Y1, I, Y2) 2); // cross cut

Min (dp (K-1, X1, Y1, X2, j) + S (x1, J + 1, X2, Y2) 2, DP (K-1, X1, J + 1, x2, Y2) + S (x1, Y1, X2, j) 2); // Vertical Cut

}

Then the memory-based search...

PS: for the first time, I did not know where the error was pushed or what was going on. The sample was just a bit difficult and I knocked it out again.

 

1.5.1 Example 3 duel spoj 196. Musketeers

You can also submit

O (N ^ 3), split X into X, X + N, DP [I] [J] = 1 to indicate that I and J can meet each other, to ensure that a person can survive is to ensure that I and I + n can meet each other. Initialize DP [I] [I + 1] = 1;

DP [I] [J] = {

1 If (DP [I] [k] & DP [k] [J] & (MP [I % N] [K % N] | MP [J % N] [K % N]) there is a K so that I-> K, J-> K, and I can beat K, or J can beat K.

0 else

}

Note that counterclockwise must be followed, so ensure that j <= I + N, I + 1 <= k <J;

1.5.1 Example 4 "dancer" Mr. White

Mr. White is a big fat man. He really likes to play DDR. The main content of DDR is to pedal the board with four directions.
Arrows (for example) are represented by 1, 2, 3, and 4, respectively. Each song has an arrow sequence, which must be used by the gamer once.
A certain foot is stepping on the corresponding pedal. At any time, neither foot can be on the same pedal, but can stay in the center at the same time.


Moving from the center to any arrow consumes 2 units of physical strength, moving from any arrow to an adjacent arrow consumes 3 units of physical strength, and moving to a relative arrow consumes 4 units of physical strength, you only need one unit of physical strength to stay on the same pedal, but you can stay on the Center board at the same time.

Conclusion: multi-stage decision making helps you understand the definition of No-efficiency. Note the following points for this question: case1 and the start position are both 0; case2, 1, 2, and 3 cannot be directly applied to two feet.

First, we need to consider what is the basis for Mr. White to make the next decision? This question is more than half done .... Amazing DP, ym.

According to the current position of Mr. White's feet, if DP [x] [y] [k] is set, the left foot is at the position of X when the K dance steps are to be performed, the minimum physical strength required for the right foot at the Y position. There are only the following types of mobile:

A) move one foot from the center to the target position.

B) move to an adjacent location

C) Move to the relative location

D) Click here.

DP [x] [y] [k] = min (DP [x] [A [k] [k + 1] + sum (Y, a [k]), DP [A [k] [y] [k] + sum (X, a [k]); // This write has taken into account case2, and think about why. Pai_^

Sum (x, y) indicates the physical strength X to Y.

Finally, the memory-based search...

 

1.5.1 Example 5 block game

DP (I, A, B, k) indicates heap I. A has been processed. The top is B, and the top is K. To determine a valid situation, you can draw a picture.

, It's a bit tricky to write... Various if... T_t

PS: It is worth noting that the problem is the sum of M heap. That is to say, even if I heap a heap for him and the height is the largest, the heap is the solution. (Because this pile can be split.) At the beginning, it was entangled .. I also asked how to ensure M heap splitting ....

 

1.5.2 Example 1 box Elimination

Question: N squares. Each time, all adjacent squares of the same color can be eliminated. Assume that l is eliminated at one time, score L ^ 2 and obtain the maximum score. (Remove the square in the middle and connect the two sides)

Train of Thought: first pre-process the square. Col [I] indicates the color of the whole consecutive square block I, and Len [I] indicates the length. DP (I, j, k) indicates that the color of the K block behind the cell is the same as col [J], and is merged (COL [I], Len [I]), (COL [I + 1], Len [J + 1]), (COL [I + 2], Len [I + 2])... (COL [J], Len [J] + k) (LEN [J] + k indicates that there are K behind and are eliminated together on Len [J .);

Case1 and K squares have exactly the same color as Len [J] and can be directly eliminated. DP (I, j, k) = dp (I, j-1, 0) + (LEN [J] + k) ^ 2;

The color of the case2 and K quick squares is the same as that of Len [J] and same as that of I... if the entire P color of J is the same, the DP (p + 1, J-1, 0) can be eliminated first, and then the DP (I, P, len [J] + k); this is a recursive process.

DP (I, j, k) = max (case1, case2 );

Draw images on paper, and the implementation is clear. At the beginning, I was confused about what K represents... T_t

Because Col [p] = Col [J] In case2, you can record that the front and side of J appear in the same position as the whole part of J color pre [J]. This is also a small optimization.

 

 

1.5.2 Example 7 frog's troubles

Frog's troubles... I am also worried! Admire the black book... In some cases, it can be written more intuitively. I don't know why the book is like that...

The distance after the transformation is shorter than the distance before the transformation. Therefore, we can draw a conclusion that the first step from 1 can only be n or 2, but only from 2. Similarly, if the first step is 2, the first step from 2 can only be n or 3. In other words, the first step starting from J must be J-1 or I, and the first step starting from I must be I + 1 or J. Recursion is obvious here...

DP (I, j, k) k = 0 indicates the minimum distance obtained from point I, k = 1 indicates the minimum distance obtained when a point is accessed in [I, j] Starting from J.

K = 0: dp (I, j, k) = min (DIS (I, I + 1) + dp (I + 1, J, 0), DIS (I, j) + dp (I + 1, J, 1 ));

K = 1: dp (I, j, k) = min (DIS (J, J-1) + dp (I, j-1, 1), DIS (J, i) + dp (I, j-1, 0 ));

Ans = min (dp (0, n-1, 0), DP (0, n-1, 1 ));

 

 

 

 

 

 

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.