atcoder#ARC132B. [ARC132B] Shift and Reverse
[ARC132B] Shift and Reverse
Score : points
Problem Statement
Given is a permutation of . On this permutation, you can do the operations below any number of times in any order.
- Reverse the entire permutation. That is, rearrange to .
- Move the term at the beginning to the end. That is, rearrange to .
Find the minimum number of operations needed to sort the permutation in ascending order. In the given input, it is guaranteed that these operations can sort the permutation in ascending order.
Constraints
- is a permutation of .
- The operations in Problem Statement can sort in ascending order.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum number of operations needed to sort the permutation in ascending order.
3
1 3 2
2
You can sort it in ascending order in two operations as follows.
- Move the term at the beginning to the end: now you have .
- Reverse the whole permutation: now you have .
You cannot sort it in less than two operations, so the answer is .
2
2 1
1
Doing either operation once will sort it in ascending order.
You cannot sort it in less than one operation, so the answer is .
10
2 3 4 5 6 7 8 9 10 1
3
You can sort it in ascending order in three operations as follows.
- Reverse the whole permutation: now you have .
- Move the term at the beginning to the end: now you have .
- Reverse the whole permutation: now you have .
You cannot sort it in less than three operations, so the answer is .
12
1 2 3 4 5 6 7 8 9 10 11 12
0
No operation is needed.