#C5293. Minimum Adjacent Swaps to Sort Books
Minimum Adjacent Swaps to Sort Books
Minimum Adjacent Swaps to Sort Books
You are given a list of integers representing the heights of books arranged in a shelf. Your task is to compute the minimum number of adjacent swaps required to sort the list in ascending order. An adjacent swap means that you can only swap two consecutive elements. Mathematically, this is equivalent to counting the number of inversions in the list, i.e. if we denote the list as \(a_1,a_2,\dots,a_n\), the number of swaps is the number of pairs \((i,j)\) such that \(i a_j\).
The problem consists of multiple test cases. For each test case, you are given the number of books followed by the list of book heights. You need to output the minimum number of adjacent swaps required to sort the books for each test case.
inputFormat
The first line contains an integer \(T\) denoting the number of test cases. Each test case is described in two lines:
- The first line of each test case contains an integer \(N\) denoting the number of books.
- The second line contains \(N\) space-separated integers representing the heights of the books.
outputFormat
For each test case, output a single line containing the minimum number of adjacent swaps required to sort the books in ascending order.
## sample4
3
3 2 1
4
4 3 2 1
3
1 2 3
4
2 1 3 4
3
6
0
1
</p>