"Problem description"
The mid-order traversal of a two-ary tree with an n node is (l,2,3,..., N), where the number,..., n is a node number.
Each node has a fraction (all positive integers), the fractional I node has a fraction of di,tree and each of its subtrees
There is an extra point, and the addition of any subtrees tree subtree (which also contains the tree itself) is calculated as follows:
Subtree The xsubtree of the right subtree of the left sub-tree and the score of the root of the +subtree.
If a subtree is empty, it is specified to be divided into 1, and the addition of the leaves is the fraction of the leaf node itself. Without regard to its empty trees.
Try to find a tree of two trees that matches the middle order traversal (,..., N) and has the highest added score. Required output;
(1) Maximum bonus points for tree
(2) The tree's pre-sequence traversal
"Input Format"
Line 1th: An integer n (n<30), which is the number of nodes.
Line 2nd: n spaces separated by integers, for each node's score (fractional <100).
"Output Format"
Line 1th: An integer that is the highest plus score (the result will not exceed 4,000,000,000).
Line 2nd: N A space-separated integer, for the tree's pre-order traversal.
"Input Sample"
5
5 7 1) 2 10
"Output Example"
145
3 1 2) 4 5
- Why is this solution right?
F[I][J] represents the maximum score in any subtree composed of node I to Node J, which can be obtained by f[i][k-1] and F[k+1][j]. And so on, we discussed the composition score for all two-fork trees, f[i][j] is the maximum number in all of these fractions, so it's right.
- Why is that kind of solution wrong?
Empty.
- Why is this solution not optimal?
Empty
- Prove why there is no better solution.
All node numbers on the left subtree of the root node are larger than the root node number (opposite the right subtree).
First of all, think F[1][n] How to get the maximum score, we will 1 to n each node as the root node (there are n cases), for some cases I is the root node is, easy to know, the left subtree maximum score * Right subtree maximum fraction +score[i]= i is the root node of the tree maximum score, This recursion down, each possible combination is discussed only once, so is the optimal solution.
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace STD;intf[ -][ -];inttree[ -][ -];intnode[ -][2];voidInit () {memset(Tree,0,sizeof(tree));memset(F,0,sizeof(f));intSolveintLintR) {if(F[l][r])returnF[L][R]; for(inti = l; I <= R; i++) {intx = Solve (l, I-1);inty = Solve (i +1, R);if(x = =0) x =1;if(Y = =0) y =1;if(x * y + f[i][i] > F[l][r]) {F[l][r] = x*y + f[i][i]; Tree[l][r] = i; } }returnF[L][R];}voidPrintintNumintLintR) {if(Tree[l][num-1]) {printf("%d", Tree[l][num-1]); Print (tree[l][num-1], L, num-1); }if(Tree[num +1][r]) {printf("%d", Tree[num +1][r]); Print (Tree[num +1][R], num +1, R); }}intMain () {intN Freopen ("In.txt","R", stdin); while(~scanf("%d", &n)) {init (); for(inti =1; I <= N; i++) {scanf("%d", &f[i][i]); Tree[i][i] = i; };cout<< Solve (1, n) << Endl;printf("%d", tree[1][n]); Print (tree[1][n],1, n);printf("\ n"); }return 0;}
Add two forks to the tree (it feels like an interval DP)