1203: Reverse Order number
Time limit:1 Sec Memory limit:128 MB
submit:125 solved:26
[Submit] [Status] [Web Board]
Description
In one arrangement, if the front and back positions of a pair of numbers are opposite the size order,that is, the preceding number is not less than the subsequent number, then they are called a reverse order. 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 is reverse, the reverse number is 4. An integer sequence is given to find the inverse number of the sequence.
Input
Multiple sets of test data
Each set of test data is divided into two lines, the first line a positive integer n (n <= 50000)
The second line has n elements (0 <= a[i] <= 10^9)
Output
Each set of test data output one line represents the number of reverse order
Sample Input
4
2 4 3 1
3
1 1 1
Sample Output
4
3
Note the red part of the title, the corresponding code to modify the red place, the general situation is <= relationship #include<cstdio> #include <algorithm>//#include <bits/stdc ++.h>using namespace Std;template<class t>inline T Read (t&x) {char C; while ((C=getchar ()) <=32) if (c==eof) return 0; BOOL Ok=false; if (c== '-') Ok=true,c=getchar (); for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 '; if (OK) x=-x; return 1;} Template<class t> inline T read_ (t&x,t&y) {return read (x) &&read (y);} Template<class t> inline T read__ (t&x,t&y,t&z) {return read (x) &&read (y) &&read (z);} Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x; if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');} Template<class t>inline void Writeln (T x) {write (x); Putchar (' \ n ');} -------ZCC IO template------const int MAXN=50011;CONST double inf=999999999; #define Lson (rt<<1), L,m#define Rson (rt<<1|1), M+1,r#define M ((l+r) >>1) #define FoR (i,t,n) for (int i= (t);i< (n); i++) typedef long Long Ll;typedef double db;typedef pair<int,int> P; #define BUG Prin TF ("---\ n"); #define MOD 10007int ans=0;void merge_sort (int *a,int x,int y,int *t) {if (y-x>1) {int p=x,m= ( X+y)/2; int i=x; int q=m; Merge_sort (a,x,m,t); Merge_sort (a,m,y,t); while (p<m| | Q<y) {if (q>=y| | (P<m&&<span style= "color: #FF0000;" >A[p]<A[q]</span>)) t[i++]=a[p++]; else {t[i++]=a[q++];ans+=m-p;} } for (int i=x;i<y;i++) a[i]=t[i]; }} int A[MAXN]; int B[maxn];int Main () {#ifndef Online_judge//Freopen ("In.txt", "R", stdin); #endif//Online_judge int n,m,i,j,k,t; while (read (n)) {for (i,0,n) {read (a[i]); } ans=0; Merge_sort (A,0,N,B); Writeln (ANS); } return 0;}
1203: Reverse order Number (merge sort)