Time Limit: 2 Sec Memory Limit: 64 MB
Submit: 544 Solved: 78
Title Description
In one arrangement, if the longitudinal position of a pair of numbers in reverse order of magnitude, i.e. the number greater than the number in front of the latter, they are called a reverse order. A reverse arrangement on the total number of the called number in reverse order.
Now, to give you a sequence of N elements, you determine the number of reverse it is.
For example inverse number of 1132 it is.
Entry
Each row represents a first line of input test data an integer number of T (1 <= T <= 5) test data for each group is an integer N represents the number of columns in a total of N elements (2 <= N <= 1000000) followed by a total of one row of N integers Ai (0 <= Ai <1000000000), represents the number of all elements in the column. A plurality of sets of data to ensure that the test data, the test data of more than 100,000 at most a number of groups.
Export
Reverse order of the number of output number sequences
Sample input
2
2
1 1
3
1 3 2
Sample Output
0
1
#include<stdio.h>
#include<string.h>
int a[1000005],b[1000005];
long long count;
void merge(int low,int mid,int high){
int i=low,j=mid+1,k=low;
while((i<=mid)&&(j<=high)){
if(a[i]<=a[j]){
b[k++]=a[i++];
}
else{
b[k++]=a[j++];
count+=mid-i+1;
}
}
while(i<=mid)
b[k++]=a[i++];
while(j<=high)
b[k++]=a[j++];
for(int i=low; i<=high; i++)
a[i]=b[i];
}
void sort(long long low,long long high){
int mid=(low+high)/2;
int x,y,z;
if(low>=high)
return ;
sort(low,mid);
sort(mid+1,high);
merge(low,mid,high);
return ;
}
int main(){
int N;
scanf("%d",&N);
while(N--){
int n;
count=0;
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
sort(0,n-1);
printf("%lld\n",count);
}
return 0;
}