标签:
题目链接:http://acm.swust.edu.cn/problem/360/
5
5 7 1 2 10
|
145
3 1 2 4 5
|
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 7 const int maxn = 50, inf = -0x7fffffff; 8 int n,ptr, vi[maxn], dp[maxn][maxn], root[maxn][maxn]; 9 //若根节点的下标是k,则左端点的是k-1,右端点是k+1; 10 void PreOrder(int vi, int y){ 11 if (root[vi][y]){ 12 if (ptr++) cout << ‘ ‘; 13 cout << root[vi][y]; 14 PreOrder(vi, root[vi][y] - 1); 15 PreOrder(root[vi][y] + 1, y); 16 } 17 } 18 19 int main(){ 20 //freopen("360-加分二叉树.in","r",stdin); 21 //freopen("360-加分二叉树.out", "w", stdout); 22 while (cin >> n){ 23 for (int i = 0; i <= n; i++) 24 for (int j = 0; j <= n; j++) 25 dp[i][j] = 1; 26 for (int i = 1; i <= n; i++){ 27 cin >> vi[i]; 28 dp[i][i] = vi[i]; 29 root[i][i] = i; 30 } 31 for (int r = 1; r <= n; r++){ 32 for (int i = 1; i <= n - r; i++){ 33 int j = i + r, tmp = inf; 34 for (int k = i; k < j; k++){ 35 if (tmp < (dp[i][k - 1] * dp[k + 1][j] + vi[k])){ 36 tmp = dp[i][k - 1] * dp[k + 1][j] + vi[k]; 37 root[i][j] = k; 38 } 39 } 40 dp[i][j] = tmp; 41 } 42 } 43 cout << dp[1][n] << endl; 44 ptr = 0; 45 PreOrder(1, n); 46 cout << endl; 47 } 48 //fclose(stdin); fclose(stdout); 49 return 0; 50 }
标签:
原文地址:http://www.cnblogs.com/zyxStar/p/4606495.html