The patience of a senior one night to explain, so I understand the interval DP so high-level things, or very easy. That is, dynamic planning within a range.
Summary of the following use case questions.
Example: Stone merging.
Description n heap of stones in a row, each pile of stones have a certain number. Now we are going to heap n heaps of stones. The merging process can only pile up adjacent two piles of stones each time, and the new pile counts as the score of the merger.
Test data for a group
Input 4 means there are 4 piles of gravel.
4 4 5 9 indicates the number of stones per heap
Output 54 indicates the maximum score
Analysis: The main idea is an accumulation: 4 4 5 9 Look, I want to know the maximum value of dp[i][j], I is the starting position, J means the terminating position, so I'm sure I want to find out the maximum value between dp[1][4] but I am from 1 to 4 but this is the three interception method, so I'll start with the small one.
Dp[1][1]=4;dp[2][2]=4;dp[3][3]=5;dp[4][4]=9. Then I recorded the length of the 2, dp[1][2]=4+4=8,dp[2][3]=8+5=14;dp[3][4]=14+9=23; This adds up to the value of 8+14+23=45; but what if I turn around? dp[1][2]=5+9=14;dp[2][3]=14+4=18;dp[3][4]=18+4=22; This adds up to the value of 14+18+22=54. It is clear that 54 is the maximum value to be asked.
, if I were to ask for the three value of the circle, I could have the two partitions on the graph, and the split value is already known.
Dynamic planning idea: first 22 merges, merges in 33, and finally the NN merges, in the process of merging, The larger interval can be divided into the community between the calculation, saving time and effort. For example, I can split into one by one and two or three when I merge in 33. namely The current phase of the consolidation method is subdivided into the previous phase of the calculated method, select the optimal scheme.
Specifically, we should define an array dp[i,j] to represent the merge method, I means to start merging from the stone numbered I, J for the end of the desired interval, sum for the number of stones.
For the above example,
First stage: Dp[1][1],dp[2][2],dp[3][3],dp[4][4] because there is no merge at the beginning, these values should all be 0.
Phase II: 22 The merging process is as follows, where SUM (I,J) represents the number of stones, that is, the number of J numbers starting from I and
dp[1,2]=dp[1,1]+dp[2,2]+sum[1,2];
dp[2,3]=dp[2,2]+dp[3,3]+sum[2,3];
dp[3,4]=dp[3,3]+dp[4,4]+sum[4,4];
Phase three: 33 merge can be split into 22 merge, split method has two, the first two is a group or the latter two is a group of
dp[1,3]=dp[1,2]+dp[3,3]+sum[1,3] or dp[1,3]=dp[1,1]+dp[2,3]+sum[1,3];
dp[2,4]=dp[2,2]+dp[3,4]+sun[2,4] or dp[2,4]=dp[2,3]+dp[3,3]+sum[2,4];
Phase four: 44 combined split method with three kinds, the same way to find out the score of three different methods, take its best. After the fifth and sixth stages, and so on, finally find the best answer in the sixth stage.
The dynamic equation is dp[i][j]=dp[i][k]+dp[k+1][j]+sum[i][j];
Reference code.
1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 7 //#define MAX 9999998 9 intMain ()Ten { One intdp[ About][ About],sum[ About][ About],a[ About]; A intN; - while(~SCANF ("%d",&N)) - { the //memset (Dp,max,sizeof (DP)); - for(intI=1; i<=n; i++) -scanf"%d",&a[i]); - for(intI=1; i<=n; i++) + { -dp[i][i]=0;//initialized to 0 +Sum[i][i]=a[i];//assign the number of stones in each heap A } at for(intlen=1; len<n; len++)//Small to large enumeration by length - { - for(intI=1; i<=n&&i+len<=n; i++)//i = start position - { - intJ=len+i;//J represents the end position of a section of Len length - for(intK=i; k<j; k++)//use K to denote the dividing interval in { -sum[i][j]=sum[i][k]+sum[k+1][j]; to if(dp[i][j]<dp[i][k]+dp[k+1][j]+Sum[i][j]) +dp[i][j]=dp[i][k]+dp[k+1][j]+Sum[i][j]; - //cout<<i<< "<<j<<" "<<sum[i][j]<<" "<<k<<" "<<dp[i][j" <<endl; the } * } $ }Panax Notoginsengcout<<dp[1][n]<<Endl; - } the return 0; +}
http://acm.nyist.net/JudgeOnline/problem.php?pid=737
This is a similar topic, and the above code is a little different, the 7,15,31 line modification can oh ~
Interval DP (Summary)