On the interval Dynamic Programming

\(\mathcal{Before\ Writing}\)

In order to deepen the impression, which wrote the study notes.

If there is wrong with the paper, please also indicate, grateful.

\ (\ mathcal {PS:} \) limited level of the author chicken dish, I did not write, do not spray oh qwq

\(\mathcal{Come\ into\ subject}\)

We first start with a question.

\(\mathcal{Problem\ Link\ :P1090}\)

Solutions of the problem is \ (n-\) stack fruit any two piles combined merged into the final value of the minimum physical cost required pile. The greedy method is easy to think that each two piles combined minimum cost, can be achieved by maintaining a small heap root. Here we are not given in the code, the reader achieve their own.

Next, consider if you can only merge the title was changed to the adjacent two piles (ensure all fruit in a line, that is, the first stack and \ (n \) stack is not adjacent ) then obviously greedy the idea is not feasible. It is necessary to change an algorithm.

\(\mathcal{Solution}\)

We can think so, no matter how merge these fruits heap, are ultimately to be merged into a pile of.

For example: Suppose there are 5 stacks fruit (numbered 1-5) need to merge, then when they finally merge into a pile, it must be made ① - ②③④⑤ | ①② - ③④⑤ | ①②③ - ④⑤ | ①②③④ - ⑤ a merger for merging the four methods obtained ( \ (a \) - \ (B \) represents \ (a \) and \ (B \) combined).

That is, we just need to know which is the last bunch of piles from the merger, the problem will be solved. The final solution required for optimal, that is, from the merger of two piles will have to be optimal, which in line with the nature of optimal substructure of dynamic programming, and two piles merger situation is the only independent, there is no aftereffect , and therefore we can consider the use of dynamic programming to solve this problem.

Convention : \ (H (A, B) \) represents the number from \ (A \) to \ (B \) of all stack merged obtained a stack

Description Status

Order \ (dp [i] [j ] \) represents the number from \ (I \) to \ (J \) all fruit stack, are combined to get the smallest physical consumption values (i.e. \ (H (i, j) \) minimum). Since \ (H (i, j) \) by \ (H (i, k) \) and \ (H (k + 1, j) \) were combined, i.e. above example
\ (H (1, 5) = min \ begin {cases } H (1,1) + H (2,5) \\ H (1,2) + H (3,5) \\ H (1,3) + H (4, 5) \\ H (1,4) + H (5,5) \ end {cases} \)

Thus, we can get the state transition equation

\(dp[i][j]=min\{\ dp[i][k]+dp[k+1][j]\ \}+cost(i,j)\) \(\ \ \ \ \ (1\leq i \leq j \leq n,i\leq k \leq j-1)\)

So we went to enumerate \ (i, j, k \ ) is like, the final solution is the \ (dp [1] [the n-] \) . That is:

初始化 dp[i][i]=0;

for(int i=1;i<=N;++i)
  for(int j=i;j<=N;++j){
    for(int k=i;k<=j-1;++k)
      dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+cost(i,j));
    /* dp[i][j]+=cost(i,j);  状态转移方程中的 +cost(...) 操作也可以放到循环外面来做,对于此题没有影响 
       cost可以是一个计算花费的函数,也可以是一个数组,具体看题目而定,而在本题中表示 H(i,j) 的值,可以使用
       前缀和,即将函数cost(i,j)用来计算sum[j]-sum[i-1] 
    */
}

Res=dp[1][N];
    

Typically \ (DP \) of the first stage is dimensional array, the second dimension is the state. But if you're the concept of dynamic programming is very clear, you will find that doing so actually there is a problem, because there's \ (i \) and not as a stage .

Or take the previous example, for a sub-optimal structural properties of dynamic programming, we ask when do \ (dp [1] [5 ] \) , her sub-problems \ (dp [1] [1 ], \ ) \ (dp [1] [2] \) \ (... \) \ (dp [4] [5] \) must have been identified and are the best, but we wrote into circulation watching, do \ (dp [1] [5 ] \) , the need to use the \ (DP [2] [. 3] \) \ (, DP [2] [. 4] ... \) \ (DP [. 4 ] [5] \) , all has not been determined.

You can think, for the variable range of the state transition equation above \ (\ 1 \ leq i \
leq j \ leq n \, \ i \ leq k \ leq j-1 \ \ \\) Obviously, binding transfer equation view, \ (DP [K +. 1] [J] \) phase of the array \ (k + 1 \) will be greater than \ (I \) at this stage, i.e. stage \ (I \) all good status yet to be determined, it is used to determine the stage of waiting behind \ (k + 1 \) in the state, which is obviously wrong.

Here we will consider in the end as to what the stage.

We found that \ (H (l, 5) \) , she is a stack of five combined obtained, and her subproblems \ (H (1,2), \) \ (H (2,4) \) , etc., belong, i.e., to be determined by the combined stack 2 to 4 obtained by the (n-\) \ combined stack formed by a \ (H (a, a + n-1) \) , we is determined by the need to do to 2 \ (n-1 \) th stack merger \ (H (a, a-K +. 1) \) . Since \ (k \) must be smaller than \ (the n-\) , so we can merge several heap as a stage. The combined stack number found know, just know the initial position of the stack of combined operation, the stack can be calculated terminated position.

The following pseudo-code gives the correct

初始化 dp[i][i]=0; 

for(int L=2;L<=N;++L) //这里合并的堆数从2开始,也就是最少两个堆合并
  for(int i=1;i+L-1<=N;++i){  //枚举起点
    int j=i+L-1;  //由起点计算出终点
    for(int k=i;k<=j-1;++k)
      dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+cost(i,j));
}

Res=dp[1][N];

So far, the problem has been resolved.

\(end\)

Thought the above code range is based on dynamic programming, it is the basic routines 1. enumerated interval length Enumerate start point, the end point is calculated Enumerate transition points 4. The transfer state .

But now the majoritycancerThe question of course will not give you too naked models do, so they will describe in some states, the transition state, or performing \ (dp \) aspects of treatment before fuss. Wherein the annular zone most dynamic regulation classic, this paper as an example for analysis.

We can modify the previous topic again: the \ (n \) pile fruit circle , you can only merge adjacent to the piles, consistent with the original title of any other agreement, ask the least physical cost value.

\(\mathcal{Solution}\)

We look at a map

As can be seen, the annular distribution of the solution in fact equivalent to a length taken from the ring \ (n-\) of the chain and the front topic we do Solutions \ (dp [1] [N ] \) but this ring one of the cases taken chain.

The more simple approach is in the original cycle outermost layer of the cycle and then set the starting point enumeration chain, also invited readers to try on their own. This introduces the classic solution of an annular dynamic range of regulation.

Cutting ring

Obviously, since it is a ring, we can consider "cut" a knife in the ring, so that it becomes a chain. Such a chain transfer ring, ring segment is movable Regulation more commonly used method to solve the ring for illustration, we can be expressed as: 123451234i.e. the length \ (n-\) loop length becomes is \ (2n-1 \) chains. Then the optimal solution in the 12345 23451 34512 45123 51234middle.

You will find that the next operation and will be exactly the same as the previous wording

Pseudo code

初始化 dp[i][i]=0;

for(int i=1;i<=N;++i) a[i]=a[i+N]=read();  由环转链 

for(int L=2;L<=N;++L)
  for(int i=1;i+L-1<=2*N-1;++i){
    int j=i+L-1;
    for(int k=i;k<=j-1;++k)
      dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+cost(...));
}

Res=min{dp[i][i+N-1]}  1<=i<=N

理解了之后,就可以尝试做这两道环形区间动规的经典题目。(如果还有例题可以提出来哦)

\(\mathcal{Problem\ Link : P1880}\)

\(\mathcal{Problem\ Link : P1063}\)

\(end\)

\(\mathcal{After\ Writing}\)

最后,如果你认真看了这篇文章,或多或少一定会有点收获吧(dalao请忽略qwq)。如果你还有什么问题请发在讨论区或者私信我,我会不定时解疑的。

另外笔者文化课水平欠佳,有些抽象的意思实在不能解释的很清楚,还请你们自行画图列表帮助理解哦。

\({End}\)

Guess you like

Origin www.cnblogs.com/SuYii/p/10988769.html