#K10641. Minimum Energy to Sort Buildings
Minimum Energy to Sort Buildings
Minimum Energy to Sort Buildings
You are given several test cases. In each test case, there is a sequence of buildings identified by their heights. Your task is to compute the minimum energy needed to rearrange the buildings in non-decreasing order.
The energy consumed is directly proportional to the number of adjacent swaps needed to sort the list. In other words, you must count the number of swaps required if you were to sort the list using an algorithm such as bubble sort. Mathematically, this number of swaps is equal to the number of inversions in the sequence, i.e., the number of pairs \( (i,j) \) such that \( i a_j \).
Input: The first line contains an integer \( T \) representing the number of test cases. For each test case, the first line contains an integer \( n \) denoting the number of buildings, followed by a line with \( n \) integers representing the heights of the buildings.
Output: For each test case, output the minimum energy required (i.e., the number of swaps) on a new line.
inputFormat
The input is read from standard input and is in the following format:
T n h1 h2 ... hn ... (repeated T times)
Where:
T
is an integer representing the number of test cases.- For each test case,
n
is the number of buildings followed by a list ofn
integers denoting the height of each building.
outputFormat
Output to standard output. For each test case, print a single integer on a new line representing the minimum energy required (i.e., the number of swaps needed to sort the building heights in non-decreasing order).
## sample3
5
4 3 2 1 5
4
4 3 5 1
3
3 3 3
6
4
0
</p>