DP acquaintance

EDITORIAL words:

In fact, in the winter Olympiad training camp time last year, it has been in contact with DP, but he is really very silent on their own time, and will not, can not figure out, can not remember on the matter, also thought to be certain to thoroughly understand it - - but the total still want to come.

So now DP fun to play it.

DP Category:

First, the simple basis dp

Such are some of the main state dp easier said comparison really want to transfer equation, the basic problem is rather common. Including recursive, backpacks, the LIS (longest increasing sequence), the LCS (Longest Common sequence)

Second, the interval dp

Third, the tree dp

Fourth, the digital dp

Fifth, the state of compression dp

 

Well, start with soft persimmon pinch

Dp first understand the basic idea of ​​it.

In real life, some processes can be divided into several interrelated stages, each stage in its decisions must be made, so that the entire process of activities to achieve the best results. Among them, select all stages of the decision-making depends both on the state currently facing, but also affect future development, after each stage when a decision is determined, constitutes a sequence of decisions.

Dynamic programming problem satisfies three important properties

  • Optimal substructure property: If the solution of the problem of sub-optimal solution to the problem is also included in the best, we say that the problem has structural sub-optimal (ie, meet the optimization principle). Sub-optimal structural properties of problem-solving dynamic programming algorithm provides an important clue.
  • Nature overlapping subproblems: overlap problem refers to proton not always new problems subproblems recursive algorithm with respect to a top-down problem solving, generated each time that some sub-problem will be repeated a plurality of times calculated. Dynamic programming algorithm is the use of the overlapping nature of this sub-problems, the calculation of each sub-problem only once, and then save the results in a table, when you need to calculate the sub-problem has been calculated again, just in a table simply look at the results, so as to obtain higher efficiency.
  • No after-effect : after the various stages lined up according to a certain order, for a given stage of a state, its previous status of each stage can not directly influence its future decisions, but only through this current state. In other words, each state is a complete summary of past history. This is no after-directional, also known as no after-effect.

 OK, now you start to see the first simple model of it - digital pyramid.

               
                 7
               3 8
              8 1 0
             2 7 4 4
            4 5 2 6 5

The general idea:

Method 1: extrapolation cis

Determining path starting, intermediate and end points relative uncertainty, define f [x] [y] is from (1,1) departure and arrival (x, y) and the maximum path weight.

Because make the (x, y) to the end of the maximum value, it is necessary that the (1,1) to (x, y) maximum value, and arriving at (x, y) of only two paths, a top left, a right, of course, both sides of the point, do not worry, because the value of its top left or top right of zero, the state transition equation is still valid.

 f[x][y]=max{f[i-1][y-1],f[i-1][y]}+a[x][y]。

Finally, ans is f [n] [1 ~ n] a maximum

Method 2: Inverse Inference (novel brain circuit)

Analysis by a top-down, bottom-up calculation.

f[x][y]=max{f[x+1][y+1]+f[x+1][y]}+a[x][y]。

for(int i=1;i<=n;i++)

f[n][i]=a[n][i];

Recommended Bowen; https://www.cnblogs.com/Renyi-Fan/p/9285495.html

and then  

The LIS (rising longest sequence) 

#include<iostream>
#include<cstdio>
#include<algorithm>
#define MAX 110000
using namespace std;
int a[MAX],f[MAX];
int main()
{
 int n;
 cin>>n;
 for(int i=1 ; i<=n ; i++)
   cin>>a[i];
 for(int i=1 ; i<=n ; i++)
 {
   f[i]=1;
   for(int j=1;j<=n-1;j++)
     if(a[i]>a[j]) f[i]=max(f[j]+1,f[i]);
 }
 sort(f+1,f+1+n);
 cout<<f[n];
 return 0;
 } 

The above is 0 (n ^ 2)

My own understanding:

f [i] expressed in a [i] at the end of the longest length of the sequence, the sequence still has the shortest one (itself), SO initialized to f [i] = 1;

If there is more than behind a [i] small, for example, let's set a [k] it, then we can choose will be a [i] that number at the end of a string of connected behind a [k] at this time to a [k] is the end of the sequence length is f [i] +1;

Of course, this select a [i] is not necessarily connected to the back of the longest, so we want to known f [i] and the selection value a [i] is connected to the front to give value comparison,

f[i]=max(f[j]+1,f[i])。

Sequentially traversing a [1] ~ a [n], then the sort what can be obtained lis.

There are 0 (nlongn) wording, but, well, I will not ah . . .

LCS (longest common subsequence):

 1 //T:最长公共子序列 
 2 #include <cstdio>
 3 #include <algorithm>
 4 
 5 #define MAXN 2111
 6 
 7 using namespace std;
 8 
 9 int n, m;
10 int a[MAXN], b[MAXN];
11 int f[MAXN][MAXN];
12 int main() {
13     scanf("%d%d", &n, &m);
14     for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
15     for(int i = 1; i <= m; i++) scanf("%d", &b[i]);
16     
17     for(int i = 1; i <= n; i++) {
18         for(int j = 1; j <= m; j++) {
19             
20             f[i][j] = max(f[i - 1][j], f[i][j - 1]);
21             
22             if(a[i] == b[j]) f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1);
23             
24             else f[i][j] = max(f[i][j], f[i - 1][j - 1]);
25             
26         }
27     }
28     printf("%d\n", f[n][m]);
29     return 0;
30 }

 

OK, now we look at some of the topics :( To be continued)

 

 

Guess you like

Origin www.cnblogs.com/becase/p/11809013.html