#C6858. Minimum Operations to Sort by Reversal

    ID: 50664 Type: Default 1000ms 256MiB

Minimum Operations to Sort by Reversal

Minimum Operations to Sort by Reversal

You are given an array of N integers. In one operation, you can choose any contiguous subarray and reverse its order. Your task is to determine the minimum number of such reversal operations needed to transform the array into non-decreasing order.

If the array is already sorted in non-decreasing order, the answer is 0. Otherwise, if the array can be sorted by performing exactly one reversal on a contiguous subarray, then the answer is 1; if not, the answer is 2. (Note that in this problem, it is always possible to sort the array using at most two such operations.)

Formally, let the initial array be \(A = [a_0,a_1,\dots,a_{N-1}]\). You need to find the minimum number of operations required such that after performing the operations the array \(A'\) satisfies \(a'_0 \le a'_1 \le \dots \le a'_{N-1}\).

inputFormat

The input is read from standard input and consists of two lines:

  1. The first line contains a single integer N representing the number of elements in the array.
  2. The second line contains N space-separated integers, representing the elements of the array.

outputFormat

Output a single integer representing the minimum number of reversal operations needed to sort the array in non-decreasing order.

## sample
5
4 3 2 6 1
2

</p>