Multi-Element Binary Search Tree
Time limit1sMemory limit1024 MB
Given sorted search probabilities and level-dependent node capacities, find the minimum expected number of comparisons for a multi-element BST.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Tree, Binary search
- Solved
- No attempts yet
Problem
A multi-element binary search tree differs from an ordinary binary search tree in that each node may hold several elements. The core binary-search-tree condition still holds in a generalized form: for any node, every element in that node's left subtree must be smaller, and every element in its right subtree larger, than every element the node itself contains.
Therefore an element can be looked up in a multi-element binary search tree in exactly the same way as in an ordinary one. At each node we decide whether the sought element must be in this node, or whether to descend into the left or the right subtree. Counting each such decision as one operation, the number of operations needed to reach the node that holds a given element equals exactly the number of levels from the root down to that node (the root is at level ).
Because of how node memory is managed, the maximum number of elements a single node may hold differs from level to level. With the root at level , the maximum number of elements in a node at level is defined, for given integers , , and , by
That is, the value subtracted when descending one level cycles as at level , at level , , at level , then again at level , and so on. For example, if , , , , then the root may hold at most elements, each of its direct children at most , each of their children , and every deeper node at most element.
Moreover, different elements are searched for with different probabilities, so a balanced tree does not necessarily minimize the average lookup time. For instance, if the smallest element is searched for far more often than any other, it is advantageous to place it at the root, in which case the entire left subtree stays empty.
For the given set of elements and their search probabilities, find the minimum average number of operations per lookup over all optimally shaped multi-element binary search trees.
Input
The first line contains three integers , , and , where is the number of elements (), , and . Each of the next lines contains one integer: the value on line is (). The last line contains real numbers ( and ), the search probability of each element. The probabilities are listed in order of element value (the first probability belongs to the smallest element). The actual element values do not matter for the solution; you may assume they are all distinct.
Output
Print, on a single line, the minimum average number of operations per lookup in an optimally shaped tree, rounded to exactly digits after the decimal point.
Note
The optimal tree shape for the second test case is shown below.
