Find out the length of the heap path
[Problem description]
We know that the two positive integers are input, and the two positive integers are separated by space bars. Create a user tree and use the input numbers as leaf nodes to find the length of the weighted path of the user tree.
[Input form]
First, enter the number of positive integers in the first row, and then a positive integer in the next row, representing the leaf node. The number of positive integers cannot exceed 1000
[Output form]
Output the corresponding weight
[Example input]
5
4 5 6 7 8
[Sample output]
69
About the user tree --
1. Path Length
The branches between one node in the tree and the other constitute the path between two nodes. The number of branches in the path is called the path length.
Figure 1 path length from the root node to the D node is 4
1. Tree path length
The path length is the sum of the path lengths from the root to each node.
Figure 2 the path length of the tree is 1 + 1 + 2 + 2 + 3 + 3 + 4 + 4 = 20
1. User tree:
The binary tree with the minimum Length of the Weighted Path WPL (Weighted Path Length) is also called the optimal binary tree.
For example, WPL = 1*5 + 2*15 + 3*40 + 4*30 + 4*10 = 315
First, let's take a look at the previous steps and we can get a description of the algorithm used to construct the Harman tree.
1. Based on the given n weights {w [1], w [2],…, W [n]}: the set of n Binary Trees F = {T [1], T [2],… T [n]}, where each binary tree T [I]; has only one root node with the weight of w [I], and its left and right Subtrees are empty.
2. In F, select the tree with the smallest weight of the two root nodes as the left and right subtree to construct a new one. binary Tree, and the weight of the root node of the new binary tree is the sum of the weights of the root node of the left and right Subtrees,
3. Delete the two trees in F and add the new binary tree to F.
4. Repeat steps 2 and 3 until F contains only one tree. This tree is the husky tree.
Use examples to illustrate this algorithm
Figure 3 Construction Process of the Harman tree
Figure 4 Final Result
The shortest result shows the minimum length of the weighted path.
WPL = 1*9 + 2*5 + 3*2 + 4*1 + 4*2 = 37
In addition, we can use another method, which is combined with algorithm descriptions to carefully observe and findThe minimum length of the weighted path is the sum of non-leaf nodes, that is
WPL = 19 + 10 + 5 + 3 = 37
As for the correctness of the algorithm, I can't think of any good way to prove it at once, but it should be logically deduced.
To implement this program, we already know about the above algorithm description, which is divided into three steps:
1. sort and exit until there is only one number in the array.
2. The minimum two numbers add up, that is, non-leaf nodes are accumulated into the accumulators.
3. Add the minimum two values and save them as a new value in the array. Remove the minimum two values and jump back to the first step.
# Include
# Include
// Qsort (); # define N 1010int rising (const void * a, const void * B) {return * (int *) a-* (int *) B ;} int main () {int leaf [N] = {0}, n, I, sum = 0; scanf ("% d", & n); for (I = 0; I