#C11148. Minimum Swaps to Sort Heights

    ID: 40432 Type: Default 1000ms 256MiB

Minimum Swaps to Sort Heights

Minimum Swaps to Sort Heights

You are given an array representing the heights of n students. Your task is to determine the minimum number of adjacent swaps required to sort the array in non-decreasing order.

An inversion in the array is defined as a pair of indices \( (i, j) \) such that \( i A[j] \). The number of such inversions is equal to the minimum number of swaps needed if only adjacent swaps are allowed.

For example, if the input is 5\n5 4 3 2 1, the correct output is 10 since there are 10 inversions in the array.

inputFormat

The input consists of two lines:

  1. The first line contains one integer \( n \) (the number of students).
  2. The second line contains \( n \) space-separated integers indicating the heights of the students.

outputFormat

Output a single integer which is the minimum number of adjacent swaps needed to sort the array in non-decreasing order.

## sample
5
1 2 3 4 5
0