#T327. 求排列的逆序数
求排列的逆序数
Description
Search engines on the Internet often need to compare information. For example, they can estimate a person's interest in various types of information based on their rankings of certain items, thereby enabling personalized services. The differences between different ranking results can be evaluated using inversions. Consider a permutation of . If there exist indices and such that and , then is called an inversion of this permutation. The total number of inversions in a permutation is called its inversion number. For example, the permutation contains inversions: , , , , , , , and . Therefore, the inversion number of this permutation is .
Clearly, among all permutations of , the smallest inversion number is , corresponding to the permutation ; the largest inversion number is , corresponding to the permutation . The larger the inversion number, the greater the difference between the permutation and the original ordered permutation.
Given a permutation of , compute its inversion number.
Input Format
The first line contains an integer , indicating that the permutation consists of numbers ().
The second line contains distinct positive integers separated by spaces, representing the permutation.
Output Format
Output the inversion number of the permutation.
6
2 6 3 4 5 1
8