#P17136. [KOI 2026 #1] 数列排序
[KOI 2026 #1] 数列排序
Problem Description
You are given a sequence of length . You may perform the following operation any number of times, possibly times:
- Choose a positive integer .
- From sequence , take all elements with values not greater than , keeping their relative order from the original sequence, forming a subsequence .
- From sequence , take all elements with values greater than , keeping their relative order from the original sequence, forming a subsequence .
- Replace the original sequence with the sequence obtained by concatenating and in order, i.e., .
Write a program to compute the minimum number of operations needed to sort sequence in non-decreasing order, i.e., to satisfy .
It can be proven that for all inputs satisfying the constraints, the given sequence can always be sorted into non-decreasing order using the operations above.
Input Format
The first line contains an integer .
The second line contains integers , separated by spaces.
Output Format
Output a single integer on the first line, indicating the minimum number of operations required to sort sequence in non-decreasing order.
6
3 4 5 1 2 6
1
9
1 5 9 9 5 1 1 5 9
2
Hint
Sample Explanation 1
You can sort sequence into non-decreasing order using operation as follows.
- Let . Keeping the original relative order, extract all elements with values not greater than , obtaining . Keeping the original relative order, extract all elements with values greater than , obtaining . Therefore, sequence is replaced by .
Sample Explanation 2
You can sort sequence into non-decreasing order using operations as follows.
- Let . Keeping the original relative order, extract all elements with values not greater than , obtaining . Keeping the original relative order, extract all elements with values greater than , obtaining . Therefore, sequence is replaced by .
- Let . Keeping the original relative order, extract all elements with values not greater than , obtaining . Keeping the original relative order, extract all elements with values greater than , obtaining . Therefore, sequence is replaced by .
It can be proven that it is impossible to sort sequence into non-decreasing order using fewer than operations.
Constraints
- All numbers given in the input are integers.
- .
- For each integer (), .
Subtasks
- ( points) For each integer (), .
- ( points) .
- ( points) .
- ( points) .
- ( points) For any integers (), .
- ( points) No additional constraints.
Translation completed by ChatGPT-5.6.
Translated by ChatGPT 5