atcoder#AGC014F. [AGC014F] Strange Sorting
[AGC014F] Strange Sorting
Score : points
Problem Statement
Takahashi loves sorting.
He has a permutation of the integers from through . Now, he will repeat the following operation until the permutation becomes :
- First, we will define high and low elements in the permutation, as follows. The -th element in the permutation is high if the maximum element between the -st and -th elements, inclusive, is the -th element itself, and otherwise the -th element is low.
- Then, let be the values of the high elements, and be the values of the low elements in the current permutation, in the order they appear in it.
- Lastly, rearrange the permutation into .
How many operations are necessary until the permutation is sorted?
Constraints
- is a permutation of the integers from through .
Input
Input is given from Standard Input in the following format:
...
Output
Print the number of operations that are necessary until the permutation is sorted.
5
3 5 1 2 4
3
The initial permutation is , and each operation changes it as follows:
- In the first operation, the -st and -nd elements are high, and the -rd, -th and -th are low. The permutation becomes: .
- In the second operation, the -st, -nd, -rd and -th elements are high, and the -th is low. The permutation becomes: .
- In the third operation, the -st, -th and -th elements are high, and the -nd and -rd and -th are low. The permutation becomes: .
5
5 4 3 2 1
4
10
2 10 5 7 3 6 4 9 8 1
6