1019 Reverse Order number
Base time limit:1 seconds space limit:131072 KB
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.
Such as 2 4 3 1 ,2 1,4 3,4 1,3 1 are in reverse order, The reverse number is 4. An integer sequence is given to find the inverse number of the sequence.
Input
Line 1 :n,n is the length of the sequence (n <= 50000)
2-n + 1 lines: Elements in a sequence (0 <= a[i] <= 10^9)
Output
Output reverse order number
Input Example
4
2
4
3
1
Output Example
4
ImportJava.util.Scanner; Public classMain1 {Static inta[]; Static intb[]; Static intCount=0; Static voidMerger (intLowintMidintHigh ) { inti,j,k; for(I=low,j=mid+1,k=0;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(k=0;low<=high;low++,k++) {A[low]=B[k]; } } Static voidMersort (intLowintHigh ) { if(Low==high)return ; intMid= (Low+high)/2; Mersort (Low,mid); Mersort (Mid+1, high); Merger (Low,mid,high); } Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner sc=NewScanner (system.in); intI,n; {Count=0;n=Sc.nextint (); A=New int[N+1]; b=New int[N+1]; for(i=0;i<n;i++) A[i]=Sc.nextint (); Mersort (0,n-1); System.out.println (count); } sc.close (); }}
--------------------------post a set of timeout algorithms .... ------------------------------------- Packagein reverse order;ImportJava.util.Scanner; Public classMain {Static intCountinta[]) { intCount=0; for(inti=0;i<a.length;i++){ for(intj=i+1;j<a.length;j++){ if(A[i]>a[j]) count++; } } returncount; } Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner sc=NewScanner (system.in); while(Sc.hasnext ()) {intn=Sc.nextint (); inta[]=New int[n]; for(inti=0;i<n;i++) A[i]=Sc.nextint (); System.out.println (Count (a)); } sc.close (); }}
1019 Reverse Order Number