#C7029. Minimum Number of Adjacent Swaps to Sort an Array
Minimum Number of Adjacent Swaps to Sort an Array
Minimum Number of Adjacent Swaps to Sort an Array
You are given an array of n integers. Your task is to compute the minimum number of adjacent swaps required to transform the array into a non-decreasing order.
This process is equivalent to counting the number of inversions in the array. In fact, the number of inversions can be represented by the formula:
$$ inversions = \sum_{1 \leq i A_j] $$
where \([A_i > A_j]\) equals 1 if \(A_i > A_j\) and 0 otherwise.
Note: You must read the input from stdin
and output the answer to stdout
.
inputFormat
The first line contains an integer n denoting the number of elements in the array. The second line contains n space-separated integers representing the array elements.
Example:
5
3 2 1 5 4
outputFormat
Output a single integer representing the minimum number of adjacent swaps required to sort the array in non-decreasing order.
Example:
4
5
3 2 1 5 4
4