Title Description
Cat Tom and Little mouse Jerry have recently played again, but after all, they are adults, they do not like to play that race each other game, now they like to play statistics. Tom's old cat recently looked up what humans call the "reverse pair", which is defined as: for a given sequence of positive integers, the inverse pair is the ordered pair of Ai>aj and i<j in the sequence. Knowing the concept, they compete on who first calculates the number of reverse pairs in a given sequence of positive integers.
Input/output format
Input format:
The first line, a number n, indicates that there are n numbers in the sequence.
The second row n number that represents the given sequence.
Output format:
The number of reverse pairs in the given sequence.
Input and Output sample input sample #: Copy
65 4 2 6 3 1
Output Example # #: Replication
11
Description
For 50% of data, n≤2500
For 100% of data, n≤40000.
1#include <cstdio>2#include <iostream>3#include <cmath>4 using namespacestd;5 Const intmaxn=100001;6InlineintRead ()7 {8 CharC=getchar ();intx=0, flag=1;9 while(c<'0'|| C>'9') {if(c=='-') flag=-1; c=GetChar ();}Ten while(c>='0'&&c<='9') x=x*Ten+c- -, C=getchar ();returnx*Flag; One } A intN; - intA[MAXN]; - intTMP[MAXN]; the intans=0; - voidSort (intLintR) - { - if(L==R)return ; + intMid=l+r>>1; -Sort (L,mid); Sort (mid+1, R); + intNowl=l,nowr=mid+1, nowpos=l; A while(nowl<=mid&&nowr<=R) at { - if(A[nowl]<=a[nowr]) tmp[nowpos++]=a[nowl],nowl++; - Elsetmp[nowpos++]=a[nowr],nowr++,ans=ans+mid-nowl+1; - } - while(Nowl<=mid) tmp[nowpos++]=a[nowl],nowl++; - while(nowr<=r) tmp[nowpos++]=a[nowr],nowr++; in for(inti=l;i<=r;i++) a[i]=Tmp[i]; - } to intMain () + { -n=read (); the for(intI=1; i<=n;i++) a[i]=read (); *Sort (1, n); $printf"%d", ans);Panax Notoginseng return 0; -}
Rokua P1908 Inverse Pairs (merge sort)