#C11723. Minimum Adjacent Swaps

    ID: 41071 Type: Default 1000ms 256MiB

Minimum Adjacent Swaps

Minimum Adjacent Swaps

You are given a list of n books, numbered arbitrarily. Your task is to determine the minimum number of adjacent swaps required to sort the books in ascending order.

The solution is based on the concept of inversions. An inversion is a pair of indices \(i, j\) such that \(i a[j]\). The number of inversions in the list is exactly equal to the minimum number of adjacent swaps needed to sort the list in ascending order.

For example, given the sequence [4, 3, 2, 1, 5], there are 6 inversions, so the answer is 6.

inputFormat

The input is read from stdin and consists of two lines:

  • The first line contains a single integer n, the number of books.
  • The second line contains n integers separated by spaces, representing the current order of the books.

outputFormat

Output a single integer to stdout which is the minimum number of adjacent swaps required to sort the list in ascending order.

## sample
5
4 3 2 1 5
6

</p>