#C10685. Minimum Operations to Sort a Sequence

    ID: 39917 Type: Default 1000ms 256MiB

Minimum Operations to Sort a Sequence

Minimum Operations to Sort a Sequence

You are given a sequence of N integers. Your task is to determine the minimum number of operations required to sort the sequence in non-decreasing order. In one operation, you can swap two elements if they are out of order.

This is equivalent to counting the number of inversion pairs in the sequence. An inversion is defined as a pair \( (i,j) \) such that \( i A[j] \).

If the sequence is already sorted, then the answer is 0.

Input Constraints: The first line contains a single integer \( N \) (the number of elements). The second line contains \( N \) space-separated integers representing the sequence \( A \).

inputFormat

The input is given via standard input (stdin) and has the following format:

N
A[0] A[1] ... A[N-1]

where \( N \) is an integer representing the number of elements, followed by a line with \( N \) space-separated integers.

outputFormat

Output a single integer denoting the minimum number of operations (swaps) required to sort the sequence in non-decreasing order on standard output (stdout).

## sample
3
1 2 3
0