eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків

Peaks

An alpinist who lives on a mountainous island has climbed to some peak and now wants to reach a higher peak. To be more precise, every point on the island has a positive elevation above sea level (the elevation of the sea is \textbf{0}) and if the peak the alpinist is currently on has elevation \textbf{E_i}, then his aim is to reach some peak with elevation \textbf{E_j} (\textbf{E_j} >\textbf{E_i}). Because he is on a peak there is no immediate path uphill -- to get to a higher point the alpinist first needs to go downhill to some lower level and only then he can go uphill again. The way down is never as remarkable as the way up, thus, the alpinist wants to maximize the elevation of the lowest point on the path from the current location to the higher peak. \includegraphics{https://static.e-olymp.com/content/a4/a488364cfce0b158c10cc0818684a08969fd84cd.jpg} For example, if the profile of the island is as shown in the figure and the alpinist is at the peak with elevation \textbf{E_4}, then there are three peaks with higher elevation (\textbf{E_5}, \textbf{E_6} and \textbf{E_7}), but the path with the lowest point having the highest elevation is the path to the peak with elevation \textbf{E_7} -- on this path he never goes below level \textbf{E_2} (in the other cases he will be forced to go down to level \textbf{E_1}). If he started from \textbf{E_5}, the corresponding lowest level would be \textbf{E_3} (path to \textbf{E_6}), but if he started from \textbf{E_6} it would be \textbf{E_1}. The map of the island is a two-dimensional rectangular table containing \textbf{N}×\textbf{M}squares and it describes the elevation of particular parts of the island -- the number in a cell describes the elevation of the corresponding region of the island. Two cells are adjacent if they share a common point. Thus, each cell (except those on the border) is adjacent to eight other. A path is a sequence of cells where each two consecutive cells are adjacent. A flat area is a set of one or more cells having the same elevation, any pair of them being connected by a path only visiting cells within the set. Any two adjacent cells with equal elevation belong to the same flat area. A peak is a flat area whose cells don’t have any adjacent cells with higher elevation. Write a program which finds all peaks on the island and for each of them finds the elevation of the highest possible lowest point on a path to some peak with a higher elevation. For the highest peak on the island (for which there is no higher peak on this island) we assume that the alpinist will leave the island looking for higher peaks, thus, the lowest point will be \textbf{0} (the level of the sea). \InputFile The first line of the input file contains two positive integers \textbf{N} and \textbf{M} (\textbf{1} ≤ \textbf{N}, \textbf{M} ≤ \textbf{2000}, \textbf{N}×\textbf{M} ≤ \textbf{10^5}), the height and the width of the map, respectively. The next \textbf{N} lines contain the description of the map of the island. Each of these lines contains \textbf{M} integers \textbf{E_ij} (\textbf{1} ≤ \textbf{E_ij} ≤ \textbf{10^6}) separated by spaces. The elevation of the cell \textbf{E_ij} (corresponding to \textbf{i}-th row and \textbf{j}-th column on the map) is given as the \textbf{j}-th number in the \textbf{i+1}-st line of the file. \OutputFile The first line of the output file must contain one integer P, the number of peaks found on the island. The next \textbf{P} lines must each contain two integers: the elevation of the particular peak and the elevation of the highest possible lowest point on the path to some higher peak. The information about peaks should be written in descending order of their elevation; if several peaks have the same elevation then they should be sorted in descending order of the lowest point elevation. \textit{\textbf{Comment for example 1}}: \includegraphics{https://static.e-olymp.com/content/fa/faa2dbc5b0d7aeacdcfc2241d58979f14560ba89.jpg} All peaks are marked by circles. One of the possible paths from peak with elevation \textbf{15} is shown with dark colouring.
Ліміт часу 1 секунда
Ліміт використання пам'яті 64 MiB
Вхідні дані #1
6 6
21 16 9 11 6 7
21 21 10 14 15 9
18 20 8 9 13 14
11 10 9 9 8 13
8 12 12 14 13 8
7 13 12 9 5 1
Вихідні дані #1
4
21 0
15 11
14 13
13 12
Джерело BOI-2012