Decryption
Time limit1sMemory limit512 MB
Split the sequence into the fewest non-overlapping segments where each segment's first element is its minimum and last element is its maximum.
- Level
Hard8 of 10
- Topics
- Greedy, Stack, Array, Prefix sum
- Solved
- No attempts yet
Problem
It is known that if you keep the first and last letters of each word in a text and rearrange the remaining letters arbitrarily, the resulting text can still be read fairly easily. A computer science laboratory studies an analogous phenomenon for number sequences.
A sequence of positive integers is called correct if its first number is the minimum and its last number is the maximum. For example, the sequences and are correct, while the sequence is not.
A sequence is given. A segment of consecutive elements of the given sequence is called correct if it forms a correct sequence: is the minimum on this segment and is the maximum.
As part of the study, the given sequence must be split into the minimum number of non-overlapping correct segments. For example, the sequence can be split into three correct segments: , , and .
Write a program that, given a sequence, determines the minimum number of correct segments it can be split into.
Input
The first line contains the integer , the number of elements in the given sequence ().
The second line contains integers , the given sequence ().
Output
Print a single number: the minimum number of correct segments into which the given sequence can be split.