#K36342. Contiguous Subarray Reversal Count

    ID: 25733 Type: Default 1000ms 256MiB

Contiguous Subarray Reversal Count

Contiguous Subarray Reversal Count

You are given an array of n integers. Your task is to determine the minimum number of contiguous subarray reversals required to transform the array into a non-decreasing sequence. A contiguous subarray is a continuous segment of the array and a reversal operation reverses the order of elements in that segment.

Formally, for an array \(a_1, a_2, \dots, a_n\), the array is non-decreasing if \[ a_1 \le a_2 \le \dots \le a_n \] Determine the minimal number of reversal operations needed such that after applying the operations, the array meets the above condition.

Note: It is always allowed to select any consecutive segment for a reversal operation.

inputFormat

The input is read from standard input (stdin) and is structured as follows:

T
n
a_1 a_2 ... a_n
... (repeated for each test case)

Where:

  • T is the number of test cases.
  • For each test case, the first number n is the size of the array, followed by a line with n integers representing the array.

outputFormat

For each test case, output on a new line the minimum number of contiguous subarray reversals required to transform the array into a non-decreasing sequence. The output is printed to standard output (stdout).

## sample
2
6
3 2 1 5 4 6
5
1 2 3 4 5
2

0

</p>