Title Description reverse order to the problem for everyone is already very familiar with the problem, that is, when seeking I<j, A[i] > A[j] The number of groups. Now, please find out the number of reverse pairs in a series of numbers, it should be noted that these numbers are within [0,9].
Enter the first line input T, indicating that there is a T group of test data for each group of data, first enter N, representing the number of N (0<n<=10^6) Next enter n number, each number is within [0,9]
Output
Output the number of reverse pairs, and the 10^9+7 modulo
--Text
Using the merge sort to reverse the order, in fact there are many kinds of
PS: Playing 10^9 time is always careless more dozen a 0 less dozen a 0,wa a half a day, eh
#include <stdio.h>inta[1000001];inttempa[1000001];Long LongTotal =0;voidMergeintLeftintMidintRight ) { intEndl = mid-1, Endr =Right ; intL = Left,r =mid; intTMP =Left ; intnumelements = Right-left +1; while(l<= Endl && R <=Endr) { if(A[l] <=A[r]) {tempa[tmp+ +] = a[l++]; } Else{ Total+ = Endl-l +1; Tempa[tmp+ +] = a[r++]; } } while(l<=Endl) {tempa[tmp+ +] = a[l++]; } while(r<=Endr) {tempa[tmp+ +] = a[r++]; } inti; for(i=0; i<=numelements;i++,endr--) {A[endr]=Tempa[endr]; }}voidMergeSort (intLeftintRight ) { intMid = (left + right)/2; if(Left <Right ) {mergesort (Left,mid); MergeSort (Mid+1, right); Merge (Left,mid+1, right); }} intMain () {inttime,t; scanf ("%d",&T); for(time=1; time<=t;time++){ intN; scanf ("%d",&N); inti; for(i=1; i<=n;i++) {scanf ("%d",&A[i]); } Total=0; MergeSort (1, N); printf ("%lld\n", Total%1000000007); } return 0;}
Xidianoj 1024 Simple Reverse