#K70212. Minimum Adjacent Swaps for Sorting Books

    ID: 33259 Type: Default 1000ms 256MiB

Minimum Adjacent Swaps for Sorting Books

Minimum Adjacent Swaps for Sorting Books

In this problem, you are given a list of unique integers that represent the widths of books on a shelf. Your task is to determine the minimum number of adjacent swaps required to rearrange the books so that their widths are in ascending order.

You can solve this problem by simulating the bubble sort algorithm. Every time two adjacent elements are out of order, you swap them and count the swap. The total count of such swaps is the desired answer.

The problem can be interpreted mathematically using inversion counting. Formally, the number of required swaps is given by \( \text{Minimum Swaps} = \sum_{i a_j) \), where \(\mathbb{1}(\cdot)\) is the indicator function.

inputFormat

The first line contains a single integer (n), the number of books. The second line contains (n) space-separated integers representing the widths of the books in their current order.

outputFormat

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

4
4 3 1 2
5

</p>