Dynamic Planning Summary

Source: Internet
Author: User
Tags x2 y2

Several DP Statements were made over the past two days. Let's review them.

1, http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1024

DP [I] [J] indicates that the first J numbers include the maximum values divided by the J-selected I segments.

DP [I] [J] = max (DP [I] [J-1], max (DP [I-1] [k]) + num [J]; 1 <= k <= J-1;

In programming, you can use a rolling array to implement a DP array, and use pre [k] to represent the maximum values of the first K numbers in the previous stage (K can be excluded) that is, Max (DP [I-1] [k]).

for(i=1;i<=m;i++){     mm=-inf;     int j;     for(j=i;j<=n;j++)     {           dp[j]=max(dp[j-1],pre[j-1])+num[j];           pre[j-1]=mm;           if(dp[j]>mm)               mm=dp[j];      }}

2, http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1025

The Analysis of valid lines cannot overlap

Then, if I> J, for the Yingcheng xi> XJ, it is essentially the longest ascending subsequence.

A dynamic planning algorithm is highly complex, and a binary algorithm is used.

3, http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1080

This question is similar to the longest common subsequence.

DP [I] [J] indicates the maximum values of I before str1 and J before str2.

DP [I] [J] = max (DP [I-1] [J-1] + num [[str1 [I] [str2 [J], DP [I-1] [J] + num [str1 [I] ['-'], DP [I] [J-1] + num ['-'] [str2 [J]);

4, http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1081

This question is the two-dimensional condition of the maximum continuous subsegment and

Use DP [k] [I] [J] to represent the maximum value from column I-j of row 0th to column I-j of row K.

DP [k] [I] [J] = max (DP [k-1] [I] [J] + sum [k] [I] [J], sum [k] [I] [J]) sum [k] [I] [J] indicates the sum of the number of column I --- J in the K row.

In actual programming, you can use DP [I] [J] to implement scrolling, while sum [k] [I] [J] only requires a temporary computation.

This question has a clear division of stages. Many papers have mentioned multi-stage decision-making. However, the division of stages of many questions is not clear, but the state transition equation can be listed.

Multi-stage decision-making is the content of Dynamic Planning in operations research. I personally think that dynamic planning in algorithms can have obvious stages, however, it is also possible to have no obvious Stage Division (such as the longest common subsequence), but it is easier to write State Equations with obvious stage division.

5, http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3905

Analysis:

From http://hi.baidu.com/mbxiluhqzjgktue/item/b325bf5616a4d401abf6d731

It is obviously a DP, but it is a little flexible and needs to be expressed in the active state.

Note that DP [I] [J] is the first I minute, the most score you can get when you sleep for J minutes

If such a state is used directly, the written transfer equation will find that the complexity is three power.

At this time, you can set another status for optimization.

Note that dp0 [I] [J] is awake for the consecutive l minutes before I start, and the maximum value that can be obtained after J minutes of sleep

So the transfer of DP [I] [J] can be as follows:

I-minute sleep DP [I] [J] = DP [I-1] [J-1]

Wake up at I minute DP [I] [J] = max {DP [I] [J], dp0 [I] [J]}

 

The transfer of dp0 [] [] is

Dp0 [I] [J] = max {dp0 [I-1] [J] + A [I], DP [I-l] [J] + sum [I]-sum [I-l];}

Dp0 [] [] can be implemented using a rolling array to remove the I dimension.

 

The answer is DP [N] [M].

In this case, the complexity is O (n * n)

6. http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2517

Board partitioning

To simplify the formula, we need to calculate the square value and the minimum value of each part.

I used the memo method.

Stages are clearly divided by the number of knives

DP [k] [X1] [Y1] [X2] [y2] indicates the maximum value of the rectangle between X1, Y1, and X2 Y2 in the upper left corner after the K-knife is cut,

The value is the minimum value of the next knife in all States.

Sum [X1] [Y1] [X2] [y2] sum of the square values of each rectangle

For (I=X1;I<X2;I++ ){Minxx=Min(DP(K+1,X1,Y1,I,Y2) +Sum(I+1,Y1,X2,Y2),Minxx); // Select the aboveMinxx=Min(DP(K+1,I+1,Y1,X2,Y2) +Sum(X1,Y1,I,Y2),Minxx); // Select the following for cross-cutting}
For (I=Y1;I<Y2;I++ ){Minxx=Min(DP(K+1,X1,Y1,X2,I) +Sum(X1,I+1,X2,Y2),Minxx); // Select the leftMinxx=Min(DP(K+1,X1,I+1,X2,Y2) +Sum(X1,Y1,X2,I),Minxx); // Select the right of vertical slice}
d[k][x1][y1][x2][y2]=minxx

7. http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3651

This stage can be divided by the order of the target string.

Process 0 to 10 for convenience.

DP [I] [l] [r] indicates the shortest time of pressing the I-th digit, left at L, and right at R. Because it is the shortest time, obviously, l or R must have a position in the I number.

In the next phase, DP [I + 1] [POS] [r] is left-hand based on the target location.

DP [I + 1] [POS] [r] = DP[I] [X] [Y] +Dis // Dis = ABS (X-Pos) + 1 the position where 1 R is added can be changed because the press takes another second. R can be changed but must be within the DIS range and be on the right of the left hand side.

DP [I + 1] [l] [POS] Right-hand position based on the target.

DP [I + 1] [l] [POS] = DP[I] [X] [Y] +Dis // Dis = ABS (Y-Pos) + 1 l can be changed but must be within the DIS range and on the left of the right hand side

When implemented, the DP array can use a similar scrolling array to reduce space. The left-hand code is provided:

void lefthand(int pos,int x,int y){    if(pos==10)        return ;    int dis=abs(pos-x)+1;    int i;    for(i=0;i<=dis&&y+i<=10;i++)    {        if(y+i>pos)            dp[cur][pos][y+i]=min(dp[cur][pos][y+i],dp[pre][x][y]+dis);    }    for(i=0;i<=dis&&y-i>=2;i++)    {        if(y-i>pos)            dp[cur][pos][y-i]=min(dp[cur][pos][y-i],dp[pre][x][y]+dis);    }}

Complete code:

Http://blog.csdn.net/juststeps/article/details/9202101

8, http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1494

This question stage is obvious and the status is obvious,

Divided by various stages of the road, the state of each stage is the state of nitrogen, the digitization of nitrogen, 5 represents a nitrogen, 14 represents 2 nitrogen, and 80% of the energy

DP [J] [k] indicates that there is still K energy after the J stage is finished.

If (K=0) // If K is 0, it must be accelerated.DP[J] [K] =DP[J-1] [K+5] +B[J]; Else if (K=10) // It may be 14 overflow of one to 10, or 9-> 10DP[J] [K] =Min(DP[J-1] [14] +A[J],DP[J-1] [9] +A[J]); Else if (K>10&&K<=14) // It can only be run normally.DP[J] [K] =DP[J-1] [K-1] +A[J]; Else // acceleration or no accelerationDP[J] [K] =Min(DP[J-1] [K-1] +A[J],DP[J-1] [K+5] +B[J]);

9 http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1913

I found that I had a feeling of looking at the previous problem based on theory.

DP [I] [J] indicates the optimal value of the I computer used at the end of day J. Of course, I <= J;

If (J>I)DP[I] [J] =Min(DP[I] [J],DP[I] [J-1] +M[I] [J]); Else // J = I {intK; (K=1;K<=I-1;K++)DP[I] [J] =Min(DP[I] [J],DP[K] [J-1] +M[I] [J] +C);}

10,

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.