Question:
Extra Binary Tree
[Problem description]
Set the ordinal traversal of a tree with N nodes to (l, 2, 3 ,..., N), where the number is 1, 2, 3 ,..., N is the node number. Each node has a score (all positive integers). Note that the score of node I is Di. The tree and each of its Subtrees have a plus score, the method for calculating the extra points of any subtree (also including the tree itself) is as follows:
Bonus points for the left subtree of the subtree x bonus points for the right subtree of the subtree + score for the root of the subtree
If a subtree is empty, set it to 1. The leaf score is the score of the leaf node. Ignore its empty subtree.
Try to find a tree that matches the ordinal traversal (, 3 ,..., N) the tree with the highest bonus points. Output required;
(1) Top bonus points for tree
(2) tree pre-order traversal
[Input format]
Row 1st: an integer n (n <30), indicating the number of nodes.
Row 2nd: N integers separated by spaces, which are the scores of each node (score <100 ).
[Output format]
Row 1st: an integer that is the maximum value (the result cannot exceed 4,000,000,000 ).
Row 2nd: N integers separated by spaces, traversing the tree in the forward order.
[Input example]
5
5 7 1 2 10
[Output example]
145
3 1 2 4 5
Analysis:
I have done this question a long time ago. Today, when I sorted out the tree-like DP, I saw that the difference was that he asked to output the pre-order traversal of the maximum bonus tree. At that time, it seemed that I only needed to output the maximum bonus points. This is also a difficult issue.
① According to the method at the time, indicator d [L, root, R] indicates setting the maximum bonus points for root Root in the [L, R] In the middle order. Transition equation: d [I, K, J] = max {d [I] d [m] d [k-1] | M = [I, K )} * max {d [k + 1] d [m] d [J] | M = (k, J]}. three-dimensional indicators are generally used, which is somewhat affected by binary classification. Solution: for I = [0, n) max {dp (0, I, n-1 )}.
② Indicator: d [L, R] indicates the maximum bonus points that can be obtained in the middle sequence [L, R. Transition equation: d [I, j] = max {d [I, k-1] * d [k + 1, J] + A [k] | K = [I, j]}. stop condition: I> J, d [I, j] = 0; I = J, d [I, j] = A [I]; solution: dp (0, n-1 ).
Obviously, the second one looks pretty cool. Why is the difference between 2D and 3D? We can see that the second indicator is more "dynamic. To be honest, I still cannot fully understand it. The inspiration is to boldly set indicators with high levels of dynamics.
Top-order traversal of the maximum bonus points:
MSO-ascii-font-family: Arial; MSO-Hansi-font-family: Arial; MSO-bidi-font-family:
Arial; color: black; MSO-ANSI-language: En-US; MSO-Fareast-language: ZH-CN;
MSO-bidi-language: AR-SA "> with a tree group of color: black; MSO-ANSI-language: En-US; MSO-Fareast-language: ZH-CN; MSO-bidi-language:
AR-SA "lang =" En-us "> G, color: black; MSO-ANSI-language: En-US; MSO-Fareast-language: ZH-CN; MSO-bidi-language:
AR-SA "lang =" En-us "> G [I, R] Arial; MSO-Hansi-font-family: Arial; MSO-bidi-font-family: Arial; color: black;
MSO-ANSI-language: En-US; MSO-Fareast-language: ZH-CN; MSO-bidi-language: AR-SA "> Representation
Arial; MSO-Hansi-font-family: Arial; MSO-bidi-font-family: Arial; color: black;
MSO-ANSI-language: En-US; MSO-Fareast-language: ZH-CN; MSO-bidi-language: AR-SA "> gets the number of the root node for the maximum addition. ; Color: black; MSO-ANSI-language: En-US; MSO-Fareast-language: ZH-CN; MSO-bidi-language:
AR-SA "lang =" En-us ">
G [I, R] = karial; MSO-Hansi-font-family: Arial; MSO-bidi-font-family: Arial; color: black;
MSO-ANSI-language: En-US; MSO-Fareast-language: ZH-CN; MSO-bidi-language: AR-SA "> (meet; color: black; MSO-ANSI-language: En-US; MSO-Fareast-language: ZH-CN; MSO-bidi-language:
AR-SA "lang =" En-us "> d [I, k-1] * d [k + 1, J] + A [k] = d [I, j] font-family:; MSO-ascii-font-family: Arial; MSO-Hansi-font-family: Arial;
MSO-bidi-font-family: Arial; color: black; MSO-ANSI-language: En-US; MSO-Fareast-language:
ZH-CN; MSO-bidi-language: AR-SA "> ). It is actually 10.5pt; font-family: Arial; MSO-Fareast-font-family:; color: black; MSO-ANSI-language:
En-US; MSO-Fareast-languages: ZH-CN; MSO-bidi-language: AR-SA "lang =" En-us "> d [I, R] Arial; MSO-bidi-font-family: Arial; color: black; MSO-ANSI-language: En-us;
MSO-Fareast-language: ZH-CN; MSO-bidi-language: AR-SA ">; color: black; MSO-ANSI-language: En-us; MSO-Fareast-language: ZH-CN; MSO-bidi-language:
AR-SA "lang =" En-us "> karial; MSO-Hansi-font-family: Arial; MSO-bidi-font-family: Arial; color: black;
The value of MSO-ANSI-language: En-US; MSO-Fareast-language: ZH-CN; MSO-bidi-language: AR-SA ">. Then the MSO-Hansi-font-family: Arial; MSO-bidi-font-family: Arial; color: black; MSO-ANSI-language:
En-US; MSO-Fareast-language: ZH-CN; MSO-bidi-language: AR-SA "> construct the optimal solution.
MSO-Hansi-font-family: Arial; MSO-bidi-font-family: Arial; color: black; MSO-ANSI-language:
En-US; MSO-Fareast-language: ZH-CN; MSO-bidi-language: AR-SA ">