Empodia
Time limit1sMemory limit128 MB
Given a permutation biosequence, find every minimal framed interval: a segment whose endpoints are its min and max and that contains no shorter framed interval.
- Level
Hard8 of 10
- Topics
- Stack, Array, Implementation, Greedy
- Solved
- No attempts yet
Problem
Biologists study biosequences. A biosequence is a sequence of integers that
- contains each of the numbers exactly once,
- starts with and ends with , and
- never has two values and next to each other in that order (an element is never immediately followed by its successor).
A contiguous block of a biosequence is called a segment.
A segment is a framed interval if its first element is the smallest value of the segment, its last element is the largest value of the segment (and different from the first), and the segment contains every integer whose value lies between the first and the last. Equivalently, the segment from position to position is a framed interval when its first element is the minimum, its last element is the maximum, and the values it covers are exactly the consecutive integers from that minimum up to that maximum.
A framed interval is an empodio if it contains no shorter framed interval.
For example, in the biosequence the whole sequence is a framed interval, but it is not an empodio because it contains the shorter framed interval . That inner framed interval contains no shorter framed interval, so it is an empodio, and it is the only empodio of this biosequence.
Given a biosequence, find all of its empodia (the plural of empodio).
Input
The first line contains one integer , the number of elements of the biosequence. Each of the next lines contains one integer; taken in order, these integers form the biosequence.
Output
On the first line print one integer , the number of empodia in the biosequence. Then print lines describing the empodia in increasing order of their starting position. Each line contains two integers and separated by a single space, meaning that the empodio starts at the -th element and ends at the -th element of the biosequence. Positions are numbered from .
Constraints
In exactly one of the inputs, . In every other input, .