Global Warming
InterviewTime limit2sMemory limit512 MB
Given column heights, find the greatest number of maximal above-water runs over all real sea levels.
- Level
Medium5 of 10
- Topics
- Union-find, Sorting, Array
- Solved
- No attempts yet
Problem
A scientist wants to study how a rising sea level changes the landform, and above all how it changes the number of islands. He starts with one-dimensional worlds. A one-dimensional world is a sequence of non-negative integers , where is the altitude at location . The figure below shows the world given by the sequence .

If the sea level is at altitude , there are 3 islands: the first two columns, the fourth column, and the last two columns. If the sea level is at altitude , there are only 2 islands. When the sea level is at altitude , land at altitude counts as submerged, so at sea level there are again 2 islands. In this world 3 is the largest number of islands over all sea levels.
The sea level is any real number, and an island is a maximal run of side by side columns that stay above the water. Given a one-dimensional world, find the largest number of islands over all sea levels.
Input
Your program reads from standard input. The first line contains the integer , the number of integers in the sequence. Each of the next lines contains one integer, and these numbers form the sequence . Every number in the sequence is non-negative and smaller than .
Output
Your program writes to standard output one integer, the largest number of islands.