First we need to know how the interval is done with DP, so let's look at the template.
1 for(inti =1; I <= N; i++) {//number of enumeration intervals2 for(intj =1; J <= can enumerate to the largest POS; J + +){3 intp = i + J-1;//represents the coordinates of the maximum value that can be reached at the moment4 if(P > N) Break;5 for(intK = J; K <= P; k++){6Dp[j][p] = min or Max (dp[j][p], dp[j][k] + dp[k +][p] +J to P Val);7 }8 }9}View Code
① Stone Problem
http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=737
This is a very obvious template problem.
You can use sum[] in advance to maintain intervals, or you can maintain them in a tree-like array.
#include <cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intMAXN = $+5;Const intINF =0x3f3f3f3f;intDP[MAXN][MAXN];intA[MAXN];intSUM[MAXN];intN;intMain () { while(SCANF ("%d", &n) = =1){ for(inti =1; I <= N; i++) scanf ("%d", A +i); Memset (DP,0,sizeof(DP)); memset (SUM,0,sizeof(sum)); for(inti =1; I <= N; i++) {Sum[i]= Sum[i-1] +A[i]; } for(inti =2; I <= N; i++){ for(intj =1; J <= N-i +1; J + +){ intp = j + I-1; if(P > N) Break; DP[J][P]=inf; for(intK = J; K <= P; k++) {Dp[j][p]= Min (Dp[j][p], dp[j][k] + dp[k +1][P] + sum[p]-sum[j-1]); }}} printf ("%d\n", dp[1][n]); } return 0;}View Code
Ii
Interval DP Primer