Peaks
Time limit1sMemory limit128 MB
Given an elevation grid, find every peak flat-region and for each compute the maximum possible minimum elevation on a path leading to some strictly higher peak, using union-find over sorted elevations.
- Level
Hard8 of 10
- Topics
- Union-find, Sorting, Graph
- Solved
- No attempts yet
Problem
An alpinist who lives on a mountainous island has climbed to some peak and now wants to reach a higher one.
More precisely, every point on the island has a positive elevation above sea level (the sea has elevation ). If the peak the alpinist currently stands on has elevation , his goal is to reach some peak with elevation . Because he is already on a peak, there is no path leading immediately uphill: to reach a higher point he must first descend to some lower level and only then climb up again. The descent is never as rewarding as the climb, so the alpinist wants to maximize the elevation of the lowest point on the path from his current location to the higher peak.

For example, suppose the profile of the island is as shown in the figure and the alpinist stands on the peak with elevation . There are three higher peaks (, , and ), but the path whose lowest point is as high as possible is the path to the peak with elevation — along this path he never descends below level (on the other paths he is forced down to level ). Had he started from , the corresponding lowest level would be (the path to ); had he started from , it would be .
The map of the island is a two-dimensional rectangular table of squares describing the elevation of the individual regions of the island: the number in a cell gives the elevation of the corresponding region. Two cells are adjacent if they share at least one common point, so every cell (except those on the border) is adjacent to eight others. A path is a sequence of cells in which every two consecutive cells are adjacent. A flat area is a set of one or more cells of equal elevation such that any two of them are connected by a path visiting only cells of the set; any two adjacent cells of equal elevation belong to the same flat area. A peak is a flat area none of whose cells has an adjacent cell of higher elevation.
Write a program that finds every peak on the island and, for each of them, the highest possible elevation of the lowest point on a path to some peak of higher elevation. For the highest peak on the island (the one that has no higher peak) we assume the alpinist leaves the island in search of higher peaks, so its lowest point is (sea level).
Input
The first line contains two positive integers and (, ) — the height and the width of the map. Each of the next lines contains integers () separated by single spaces. The value , given as the -th number on the -th line, is the elevation of the cell in row and column .
Output
On the first line print one integer — the number of peaks on the island. Each of the next lines must contain two integers: the elevation of a peak and the highest possible elevation of the lowest point on a path from that peak to some higher peak. Print the peaks in descending order of their elevation; peaks of equal elevation must be printed in descending order of the elevation of their lowest point.
Hint

All peaks are marked with circles. One possible path starting from the peak with elevation is shown in a darker shade.