51Nod
In one arrangement, if the front and back positions of a pair of numbers are opposite to the size order, that is, the previous number is greater than the subsequent number, then they are called an inverse. The total number of reverse order in a permutation is called the inverse number of the permutation. such as 2 4 3 1, 2 1,4 3,4 1,3 1 is reverse, the reverse number is 4. An integer sequence is given to find the inverse number of the sequence. Input
Line 1th: N,n is the length of the sequence (N <= 50000) 2-n + 1 lines: Elements in the sequence (0 <= a[i] <= 10^9)
Output
Output reverse order number
Input example
42431
Output example
4
#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<map>#defineN 52005using namespacestd;intA[n],c[n],d[n];intLowbit (intt) { ///Set K is the number of T at the end of 0, then the 2^k=t& (t^ (t-1)) is obtained; returnt& (t^ (t1));}voidUpdateintx) { while(X >0) {C[x]+=1; X-=lowbit (x); }}intSumintLi) { intsum=0; while(li<=N) {sum+=C[li]; Li=li+Lowbit (LI); } returnsum;} Map<int,int>Q;intMain () {intn,x; intSum; while(SCANF ("%d", &n)! =EOF) {Sum=0; Memset (c,0,sizeof(c)); for(intI=0; i<n;i++) {scanf ("%d",&A[i]); D[i]=A[i]; } sort (A,a+N); intPre=1, tot=1; for(intI=0; i<n;i++) { if(a[i]==pre) {A[i]=tot; Q[pre]=tot; } Else{Pre=A[i]; A[i]=++tot; Q[pre]=tot; } } for(intI=0; i<n;i++) {D[i]=Q[d[i]]; }//for (int i=0;i<n;i++)//cout<<d[i]<< ""; for(intI=0; i<n;i++) {Sum+=sum (d[i]+1); Update (d[i]); } printf ("%d\n", Sum); } return 0;}
Number of reverse order