#C1143. Minimum Adjacent Swaps for Non-decreasing Array
Minimum Adjacent Swaps for Non-decreasing Array
Minimum Adjacent Swaps for Non-decreasing Array
You are given an array of integers. Your task is to compute the minimum number of adjacent swaps required to transform the array into a non-decreasing (sorted) order. In other words, determine the minimum number of swaps required where only adjacent elements can be swapped.
The number of swaps is equivalent to the inversion count of the array. Recall that the inversion count of an array \(A\) is given by \[ \text{Inv}(A)=\sum_{1\leq iA[j]\}}, \] where \(\mathbf{1}_{\{A[i]>A[j]\}}\) is 1 if \(A[i]>A[j]\) and 0 otherwise.
For instance:
- For the array [3, 2, 1], the required number of swaps is 3.
- For the array [1, 2, 3, 5, 4], the required number of swaps is 1.
- For the array [4, 3, 2, 1], the required number of swaps is 6.
You need to handle multiple test cases in a single run.
inputFormat
The first line of input contains an integer \(T\) denoting the number of test cases. Each test case is described as follows:
- The first line contains an integer \(n\), the number of elements in the array.
- The next line contains \(n\) space-separated integers representing the array.
Note: The input is provided via standard input (stdin).
outputFormat
For each test case, output a single line containing the minimum number of adjacent swaps required to convert the array into a non-decreasing array. The output should be written to standard output (stdout).
## sample3
3
3 2 1
5
1 2 3 5 4
4
4 3 2 1
3
1
6
</p>