Editorial for Baltic OI '12 P3 - Peaks
Submitting an official solution before solving the problem yourself is a bannable offence.
We sort the cells according to descending elevation and go through the cells only once. If a cell has no adjacent cells that have been previously processed, then it is a peak. Otherwise it is assigned to the same peak as its adjacent, previously processed cells. If there are several such peaks, we have now found the maximum lowest point on a path connecting them. Thus, if some of these peaks are higher than other, we immediately know the answer for the lower peak(s). Moreover, all of the cells assigned to any of the peaks can now be assigned to the highest peak and the procedure can be continued. Care must be taken if there are several highest peaks in the merge; these peaks will always obtain the same answer in a later round, but the representative peak should preferably keep a list of them for easy reference. To make the algorithm efficient, one can use a disjoint set data structure for keeping track of the assignment of peaks and linked lists to represent the lists of equivalent peaks (as these lists can get long and might have to be merged several times). The complexity is .
In another solution, employing the same sorted list, one performs a depth-first search starting from every non-visited cell (that is, every peak) and never walking upwards. The highest cells on the "borders" between the various "fields" established in this search represent possible transitions between the peaks, and it is easy to show that all relevant paths go through these cells. Thus, any algorithm for finding the shortest path can be used (slightly modified) to find the answer, for example yielding a complexity of for a set of instances of Dijkstra's algorithm.
Comments