Number of reverse order permutations
Title Description
Search engines on the internet often need to compare information, for example, by a person's ranking of something to estimate his or her interest in a variety of different information, so as to achieve personalized service.
For different ranking results, you can use reverse order to evaluate the differences between them. Consider the permutation of the,..., n i1,i2,...,in, if there is j,k, which satisfies J < K and ij > ik, then it is called (Ij,ik) as a reverse order of the permutation.
The number of reverse orders in a permutation is called the order of the permutations. For example, the permutation 263451 contains 8 reverse order (2,1), (6,3), (6,4), (6,5), (6,1), (3,1), (4,1), (5,1), so the reverse number of the permutation is 8. Obviously, all n! permutations consisting of,..., N, the smallest number of reverse order is 0, the corresponding arrangement is,..., N; the largest number of reverse order is n (n-1)/2, the corresponding arrangement is N, (n-1),..., 2, 1. The larger the number of reverse order, the greater the difference in the original arrangement.
Now given a permutation of,..., N, the number of reverse order is obtained.
Input
The first line is an integer n, which indicates that the permutation has n numbers (n <= 100000).
The second line is n different positive integers, separated by a space, indicating the arrangement.
Output
Outputs the number of reverse order for this arrangement.
Sample input
62 6 3 4 5 1
Sample output
8
Tips
1. Using binary merge sorting algorithm (divide and conquer);
2. Note that the results may exceed the range of int and need to be stored with a long long.
Code:
1#include <cstdio>2 Const intMAXV =100005;3 4 intC[MAXV], N;5 6 Long Longlbintx) {7 returnX &-x;8 }9 voidHsintXintp) {Ten for(inti = x; I <= N; i + =lb (i)) { OneC[i] + =p; A } - } - the Long LongQueryintx) { - Long LongDL =0; - for(inti = x; I I-=lb (i)) { -DL + =C[i]; + } - returnDL; + } A at intMain () { - Long LongAns =0; -scanf"%d", &n); - for(inti =1; I <= N; i++){ - intA; -scanf"%d", &a); inHS (A,1); -Ans + = i-query (a); to } +printf"%lld\n", ans); - return 0; the}
[Tree array] to arrange the number of reverse order