QBXT 2018 Spring DP & Graph Theory Class 2018.5.3 --- Interval DP topic

The title of this article comes from the PPT of Zhang Haowei, a senior at Peking University.

1. Interval DP: Solve problems regarding the merging or deletion of two or more intervals (maximum/minimum times/values, total number of options, feasibility, etc.).

2. Stone merge:

Status: dp[l][r] represents the minimum cost of combining only the stones in the interval l~r.

State transition: dp[l][r]=min{dp[l][r],dp[l][k]+dp[k+1][r]+s[r]-s[l-1] }

The pile of stones l~r can be divided into two piles of stones l~k and k+1~r. Only the minimum cost sum of the two piles of stones is considered plus the sum of the cost of merging the two piles of stones. This can be prefixed with the sum optimization.

Boundary: dp[i][i]=0 When there is only one pile of stones, there is no need to merge, and the cost is 0.

Code:

for ( int len= 1 ;len<n;len++) // enumerate the length of the interval, pay attention to the range of values 
  ​​for ( int l= 1 ;l<=n-len;l++) // enumerate the left endpoint 
  {  
    int r= l+len; // Right endpoint 
    dp[l][r]= INF;
    for ( int k=l;k<r;k++ ) {
      dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]+s[r]-s[l-1]);
    }
  }

There is also a method of enumerating the left endpoint in reverse order and enumerating the right endpoint in positive order, which can also ensure that the state transition is legal, but it is not very intuitive.

Stone merging This problem can be optimized with quadrilateral inequality, but it is said that it is difficult to understand, here is the code of zhw, and the proof is omitted.

for (len= 1 ; len<n; len++ ) difference between the left and right endpoints of len
   for (l= 1 ; l<=n-len; l++ )
  {
    r = 1 + len;
    dp[l][r]=INF;
    for (k=f[l][r-1]; k<=f[l+1][r]; k++)
      if (dp[l][k]+dp[k+1][r]+s[r]-s[l-1]<dp[l][r])
      {
        dp[l][r]=dp[l][k]+dp[k+1][r]+s[r]-s[l-1];
        f[l][r]=k;
      }
  }

3. Stone Merge+

There are n piles of stones arranged in a row, and the number of stones in the i-th pile is ai.
You can merge 2~3 adjacent piles into one pile at a time.
The cost of the merger is the sum of the stones in the 2/3 piles of stones.
The design scheme requires the minimum sum of costs.
The same as the above question, enumerate two breakpoints, it is time to transfer the equation, and then for a for is A.

for (l= 1 ; l<=n; l++) dp[l][l]= 0 ;
 for (len= 1 ; len<n; len++ ) The difference between the left and right endpoints of len
   for (l= 1 ; l<=n-len; l++ )
  {
    r = 1 + len;
    dp[l][r]=INF;
    for (k=l; k<r; k++)
      dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]+s[r]-s[l-1]);
    for (k=l; k<r; k++)
      for (t=k+1; t<r; t++)
        dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][t]+dp[t+1][r]+s[r]-s[l-1]);
  }

4. Stone Merge++(?)

There are n piles of stones arranged in a row, and the number of stones in the i-th pile is ai.
Adjacent 2~n piles can be merged into one pile at a time.
The cost of merging is the sum of the number of stones in the 2/3/.../n piles of stones.
The design scheme requires the minimum sum of costs.

thinking question. . . You can directly combine n piles into a pile at one time.

5. Addition of binary tree

6. hihocoder1110 regular expressions

Given a string, check if it is a valid regular expression.
 A regular expression is defined as:
 1:0 is a regular expression, 1 is also a regular expression.
 2: P and Q are both regular expressions, then PQ is a regular expression.
 3: P is a regular expression, then (P) is a regular expression
 4: P is a regular expression, then P* is also a regular expression
 5: P and Q are both regular expressions, then P|Q is a regular expression .
010101101*
(11|0*)* are all regular expressions.
|S|<=100.


The interval DP judges whether the regular expression is legal .
For 1: enumerate each character of the string, if(s[i]=='0'||s[i]=='1') dp[i][i]=1;else dp[i][ i]=0;

2:枚举断点,if(dp[i][k]&&dp[k+1][r]) dp[l][r]=1;

3:if(s[l]==‘(’&&s[r]==')'&&dp[l+1][r-1]) dp[l][r]=1;

4:if(s[r]=='*'&&dp[l][r-1]) dp[l][r]=1;

5: Enumerate the position of '|' if(s[k]=='|'&&dp[l][k]&&dp[k+1][r]) dp[l][r]=1;

最终if(dp[1][n]) cout<<yes ;else cout<<no;

 

 

 

 

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325340301&siteId=291194637