1203: Reverse order Number (merge sort)

Source: Internet
Author: User

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)

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.