ACM ICPC World Finals 2016

Problem C: Ceiling Function

Shortest judge solution: 829 bytes. Shortest team solution (during contest): 550 bytes.

This was clearly the easiest problem in the problemset. What we were asked to do was to build an unbalanced binary tree out of each sequence of numbers we were given, and then group the trees by shape (and report the number of groups).

Forming the trees is simply simulation of the process. To compare the shape of the two trees, the easiest thing is to do it recursively: if both trees are empty, they are of equal shape, if only one is empty, they are not of equal shape, and if they are both non-empty, they are equal if both the left and right subtrees are equal.

For the grouping, the limits are low enough we can afford to do it quadratically — for each tree, check whether it is the first of this shape in the sequence, and if yes, increment the answer by one.