Editorial for Bubble Cup V9 I R3D3's summer adventure


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Basically the problem can be formulated in the following way: Let's build a binary tree with edges labeled '0' and '1' with N leaves so that the sum of costs of paths to all leaves is minimal. This tree is also called 'Varn code tree'. Varn code tree is generated as follows. Start with a tree consisting of a root node from which descend 2 leaf nodes, the costs associated with the corresponding code symbols. Select the lowest cost node, let c be its cost, and let descend from it 2 leaf nodes c+c(0) and c+c(1). Continue by selecting the lowest cost node from the new tree until N leaf nodes have been created.

This greedy approach can be done in \mathcal O(N \log N) if we actually construct a tree. Basically we do the following thing N times: out of all nodes, we select the lowest, delete it, and create 2 new nodes by adding '0' and '1' to the one we have deleted. If all of this was done with some standard data structure, this is \mathcal O(N \log N).

If we actually don't need the tree and the coding itself, just the final cost, we can improve to \mathcal O(\log^2 N). Let's define an array F to represent this tree, where F[i] will be the number of paths to the leaves in this tree with cost equal to i. While the sum of all values in F is lower than N, we do the following procedure: find an element with the lowest 'i' where F[i] > 0. Then, F[i+c(0)] \mathbin{\text{+=}} F[i]; F[i+c(1)] \mathbin{\text{+=}} F[i]; F[i] = 0. Basically we do the same thing as in the previous algorithm, just instead of adding 2 edges to the leaf with the lowest cost, we do that to all of the leaves that have the lowest cost in the tree. Numbers in this array rise at least as fast as Fibonacci numbers, just the array will be sparse. So instead of an array, we use some data structure like a map, and we get \mathcal O(\log^2 N) complexity.

DMOJ curator's note: This solution is not \mathcal O(\log^2 N). Consider C_0 = N and C_1 = 3.


Comments

There are no comments at the moment.