http://acm.nyist.net/JudgeOnline/problem.php?pid=737
Data is small, suitable for the introduction of interval DP
For the [I, j] heap, no matter how you merge, whichever two stacks you choose First, when you combine [I, j] into a bunch of that step, the cost is SUM[I....J]
Can be simulated with paper.
So we set DP[I][J] to represent the minimum cost of crafting a heap of I...J.
such as dp[1][1] = 0. DP[1][2] = a[1] + a[2];
Then ask Dp[i][j], then it can be dp[i][k] + dp[k + 1][j] + cost
Note the order of DP, as required DP[1][N], then need to use dp[1][k] and dp[k][n]
You need to consider how for, in order to make the sub-problem has been calculated, it is recommended to start with DFS + memory to do.
Here the DP order should be calculated first 2 sets, 3, 4 、......
is to calculate first dp[1][2], dp[2][3], which makes it possible to seek dp[1][3].
All Dp[i][i] = 0
#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<assert.h>#defineIOS Ios::sync_with_stdio (False)using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>Const intMAXN = $+ -;intN;intA[MAXN];intDP[MAXN][MAXN];intSUM[MAXN];intDfsintBe,inten) { if(Be > en)return 0; if(BE = =en) { returnDp[be][en] =0; } if(Dp[be][en]! = INF)returnDp[be][en]; for(intK = be; K <= en; ++k) {Dp[be][k]=Dfs (be, k); Dp[k+1][en] = DFS (k +1, en); ASSERT (Dp[be][k]>=0); ASSERT (Dp[k+1][en] >=0); Dp[be][en]= Min (Dp[be][k] + dp[k +1][en] + sum[en]-sum[be-1], Dp[be][en]);//cout << dp[2][3] << Endl; } returnDp[be][en];}voidWork () { for(inti =1; I <= N; ++i) {scanf ("%d", &A[i]); Sum[i]= Sum[i-1] +A[i]; } memset (DP,0,sizeofDP);//cout << dfs (1, n) << Endl;//cout << dp[2][3] << Endl; for(intK =1; K <= N-1; ++k) { for(inti =1; I <= N-1; ++i) {intbe =i; intEn = i +K; if(En > N) Break; Dp[be][en]=inf; for(inth = be; H <= en-1; ++h) {Dp[be][en]= Min (Dp[be][en], dp[be][h] + dp[h +1][en] + sum[en]-sum[be-1]); }}} printf ("%d\n", dp[1][n]);}intMain () {#ifdef local freopen ("Data.txt","R", stdin);//freopen ("Data.txt", "w", stdout);#endif while(SCANF ("%d", &n)! =EOF) work (); return 0;}View Code
Nyoj 737 stone merge (i). Interval DP