#P5923. Finding Obstacle Segments in a Bio Sequence
Finding Obstacle Segments in a Bio Sequence
Finding Obstacle Segments in a Bio Sequence
A bio sequence is defined as a sequence of M integers satisfying the following conditions:
- The sequence contains all integers from \(0, 1, \ldots, M-1\).
- The first element is \(0\) and the last element is \(M-1\).
- No integer \(E+1\) appears immediately after \(E\) in the sequence.
A contiguous subsequence of a bio sequence is called a segment. A segment is defined as a frame segment if:
- The starting element equals the minimum element in the segment.
- The ending element equals the maximum element in the segment and is different from the starting element.
- Every integer between the minimum and maximum (i.e. every \(k\) satisfying \(\min \le k \le \max\)) appears at least once in the segment.
A frame segment which does not contain any shorter (i.e. of strictly smaller length) frame segment as a contiguous subsegment is called an obstacle segment.
For example, consider the bio sequence \((0, 3, 5, 4, 6, 2, 1, 7)\). The whole sequence is a frame segment since it starts with \(0\) (the minimum) and ends with \(7\) (the maximum) and contains all integers from \(0\) to \(7\). However, it contains another frame segment \((3, 5, 4, 6)\). Therefore, the entire sequence is not an obstacle segment, while \((3, 5, 4, 6)\) is an obstacle segment (and the only one in the given bio sequence).
Your task is to write a program that, given a bio sequence as input, outputs all obstacle segments in the sequence.
inputFormat
The input consists of two lines:
- The first line contains a single integer \(M\) (the number of elements in the bio sequence).
- The second line contains \(M\) space-separated integers representing the bio sequence.
You may assume that the input always represents a valid bio sequence.
outputFormat
For each obstacle segment found in the bio sequence, output a single line containing the numbers of that segment separated by a single space. The segments should be output in the order of their appearance in the original sequence (i.e. sorted by their starting index). If multiple obstacle segments exist, output each on a new line. If there is no obstacle segment, output nothing.
sample
8
0 3 5 4 6 2 1 7
3 5 4 6