To find the time limit of reverse order:MS | Memory limit:65535 KB Difficulty:5
-
Describe
-
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.
Now, to give you a sequence of n elements, you have to figure out how many reverse-order it is.
For example, 1 3 2 reverse number is 1.
-
Input
-
The first line enters an integer T that represents the number of groups of test data (1<=t<=5)
Each row of test data for each group is an integer n representing a total of n elements in the sequence (2〈=n〈=1000000)
The following line has a total of n integer Ai (0<=ai<1000000000), representing all the elements in the sequence.
Data assurance in multiple sets of test data, more than 100,000 numbers of test data have a maximum of one set.
-
Output
-
Output the inverse number of the sequence
-
Sample input
-
221 131 3 2
-
Sample output
-
01
-
Source
-
[Zhang Yunzun] Original
-
Uploaded by
-
Zhang Yunzun
-
Idea: Understand merge sort naturally understand this.
Code:
1#include <iostream>2#include <cstdio>3 4 #defineMaxxx 10000055 6 using namespacestd;7 8 Long intA[MAXXX],B[MAXXX];//B as a temporary array9 Long Long intcou;Ten One voidMergee (Long intA[],intStartintMidintendd) { A intI=start,j=mid+1; - intk=start; - while(i<=mid&&j<=endd) { the if(a[i]<=A[j]) { -b[k++]=a[i++]; -}Else{ -cou+=j-K; +b[k++]=a[j++]; - } + } A while(i<=mid) { atb[k++]=a[i++]; - } - while(j<=endd) { -b[k++]=a[j++]; - } - for(inti=start;i<=endd;i++){ ina[i]=B[i]; - } to } + - voidMergeSort (Long intA[],intStartintendd) { the if(start<endd) { * intMid= (START+ENDD)/2; $ mergesort (a,start,mid);Panax NotoginsengMergeSort (a,mid+1, endd); - Mergee (A,START,MID,ENDD); the } + } A the intMain () + { - intT; $ intN; $scanf"%d",&t); - for(intj=0; j<t;j++){ -cou=0; thescanf"%d",&n); - for(intI=0; i<n;i++){Wuyiscanf"%d", A +i); the } -MergeSort (A,0, N-1); Wuprintf"%lld\n", cou); - } About return 0; $}
Find the reverse order number _ merge sort