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
-
-
OJ The best solution on the Nanyang Polytechnic is as follows
#include <iostream> #include < Stdio.h>using namespace Std;int a[1000010];int b[1000010];long long sum=0;void marge (int *a,int beg,int end,int *b) { if (beg==end) return; int mid= (beg+end)/2; Marge (a,beg,mid,b); Marge (a,mid+1,end,b); int I=beg,j=mid+1,pos=beg; while (I<=mid&&j<=end) {while (a[i]<=a[j]&&i<=mid) b[pos++]=a[i++]; while (A[j]<a[i]&&j<=end) {b[pos++]=a[j++]; Sum+=mid+1-i; }} while (I<=mid) b[pos++]=a[i++]; while (J<=end) b[pos++]=a[j++]; for (int i=beg; i<=end; i++) a[i]=b[i];//write back to A in}int main () {int n; cin>>n; while (n--) {sum=0; int m; cin>>m; for (int i=1; i<=m; i++) scanf ("%d", &a[i]); Marge (a,1,m,b); printf ("%lld\n", sum);
} return 0;}
(c + + implementation) Nanyang Science and Technology ACM TOPIC 117 reverse Order number