Islands in the Data Stream
InterviewTime limit1sMemory limit128 MB
Count the nested islands, contiguous blocks higher than both neighbors, in each 15-number stream that starts and ends at zero.
- Level
Easy3 of 10
- Topics
- Simulation, Stack
- Solved
- No attempts yet
Problem
You are given a sequence of integers . An island is a contiguous block of the sequence in which every element is greater than the element immediately before the block and greater than the element immediately after it. A block with no element before it, or no element after it, is not an island.
An island that lies entirely inside another island still counts as its own island, so each one adds 1 to the total.
You are given a sequence of 15 non-negative integers in which each integer differs from the previous integer by at most 1. Write a program that counts the islands in the sequence.
Input
The first line contains the number of data sets (). The data sets are independent and are all processed the same way.
Each of the next lines holds one data set. The line begins with the data set number , followed by 15 non-negative integers separated by single spaces. The first and the last of those 15 integers are 0. Each integer differs from the previous integer by at most 1.
Output
Print one line for each data set. The line consists of the data set number , a single space, and the number of islands in that sequence.