#K57902. Counting Unfavorable Pairs

    ID: 30523 Type: Default 1000ms 256MiB

Counting Unfavorable Pairs

Counting Unfavorable Pairs

Given a permutation of the first ( n ) positive integers, your task is to count the number of unfavorable pairs. An unfavorable pair is defined as a pair of indices ( (i, j) ) such that ( i < j ) and ( permutation[i] > permutation[j] ). This is equivalent to counting the number of inversions in the permutation.

For example, if the permutation is [2, 3, 1, 5, 4] for ( n = 5 ), the unfavorable pairs are (2, 1), (3, 1), and (5, 4), making a total of 3 unfavorable pairs.

inputFormat

The first line contains an integer ( n ), representing the number of elements in the permutation. The second line contains ( n ) space-separated integers representing the permutation of the first ( n ) positive integers.

outputFormat

Output a single integer, which is the number of unfavorable pairs (inversions) in the permutation.## sample

5
2 3 1 5 4
3