#C10129. Minimum Subarray Sort Operations

    ID: 39300 Type: Default 1000ms 256MiB

Minimum Subarray Sort Operations

Minimum Subarray Sort Operations

Given an array of nn integers, you are allowed to perform a subarray-sort operation. In one operation, you can choose any contiguous subarray [l,r][l, r] (where 1lrn1 \le l \le r \le n) and sort it in non-decreasing order. The goal is to make the entire array sorted in non-decreasing order using the minimum number of such operations.

More formally, let a=[a1,a2,,an]a = [a_1, a_2, \ldots, a_n] be the given array. An operation consists of selecting indices ll and rr (1lrn1 \le l \le r \le n) and replacing the subarray [al,al+1,,ar][a_l, a_{l+1}, \ldots, a_r] with its sorted version (in non-decreasing order). Determine the minimum number of operations required to sort the entire array aa.

Note: If the array is already sorted, no operations are needed.

inputFormat

The input is given via standard input. The first line contains an integer nn, the number of elements in the array. The second line contains nn space-separated integers representing the array.

outputFormat

Output via standard output the minimum number of subarray sort operations needed to make the entire array sorted in non-decreasing order.## sample

5
1 2 3 4 5
0

</p>