atcoder#ARC132B. [ARC132B] Shift and Reverse

[ARC132B] Shift and Reverse

Score : 400400 points

Problem Statement

Given is a permutation p1,,pnp_1,\dots,p_n of 1,,n1,\dots,n. On this permutation, you can do the operations below any number of times in any order.

  • Reverse the entire permutation. That is, rearrange p1,p2,,pnp_1,p_2,\dots,p_n to pn,pn1,,p1p_n,p_{n-1},\dots,p_1.
  • Move the term at the beginning to the end. That is, rearrange p1,p2,,pnp_1,p_2,\dots,p_n to p2,,pn,p1p_2,\dots, p_n, p_1.

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

  • 2n1052 \leq n \leq 10^5
  • p1,,pnp_1,\dots,p_n is a permutation of 1,,n1,\dots,n.
  • The operations in Problem Statement can sort p1,,pnp_1,\dots,p_n in ascending order.

Input

Input is given from Standard Input in the following format:

nn

p1p_1 \dots pnp_n

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.

  1. Move the term at the beginning to the end: now you have 3,2,13, 2, 1.
  2. Reverse the whole permutation: now you have 1,2,31, 2, 3.

You cannot sort it in less than two operations, so the answer is 22.

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 11.

10
2 3 4 5 6 7 8 9 10 1
3

You can sort it in ascending order in three operations as follows.

  1. Reverse the whole permutation: now you have 1,10,9,8,7,6,5,4,3,21,10,9,8,7,6,5,4,3,2.
  2. Move the term at the beginning to the end: now you have 10,9,8,7,6,5,4,3,2,110,9,8,7,6,5,4,3,2,1.
  3. Reverse the whole permutation: now you have 1,2,3,4,5,6,7,8,9,101,2,3,4,5,6,7,8,9,10.

You cannot sort it in less than three operations, so the answer is 33.

12
1 2 3 4 5 6 7 8 9 10 11 12
0

No operation is needed.