Find the number of reverse order

Source: Internet
Author: User

To find the time limit of reverse order: -Ms | Memory Limit:65535KB 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
 
 
Code 1,
 #include <stdio.h>long long A[1000005];long long t[1000005]; Long Long count;void Merg (Long long *a,int left,int Right,long long *t) {if (right-left>1) {int mid=left+ (right-left)/2; int P=left,q=mid,i=left;merg (a,left,mid,t); Merg (a,mid,right,t); while (p<mid| | Q<right) {if (q>=right| |  (P<mid&&a[p]<=a[q]))  T[i++]=a[p++];else{t[i++]=a[q++];count=count+mid-p;} }for (i=left;i<right;i++) {a[i]=t[i];}}} int main (void) {int t,n;scanf ("%d", &t), while (t--) {scanf ("%d", &n), for (int i=0;i<n;i++) {scanf ("%lld", &a[i]);} Count=0;merg (a,0,n,t);p rintf ("%lld\n", count);} return 0;} 


 
 
Code 2,
 #include <stdio.h>long long A[1000005];long long t[1000005]; Long Long count;void Merg (Long long *a,int left,int Right,long long *t) {if (right-left>1) {int mid=left+ (right-left)/2; int L=left,r=mid,i=left;merg (a,left,mid,t), Merg (A,mid,right,t), while (L<mid&&r<right) {if (a[l]<=  A[R]) t[i++]=a[l++];else{t[i++]=a[r++];count=count+mid-l;} }while (L<mid) {t[i++]=a[l++];} while (r<right) {t[i++]=a[r++];count=count+mid-l;} for (i=left;i<right;i++) {a[i]=t[i];}}} int main (void) {int t,n;scanf ("%d", &t), while (t--) {scanf ("%d", &n), for (int i=0;i<n;i++) {scanf ("%lld", &a[i]);} Count=0;merg (a,0,n,t);p rintf ("%lld\n", count);}        return 0;} 


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Find the number of reverse order

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.