Luogp1908 Reverse Order (merge order), P1908

Source: Internet
Author: User

Luogp1908 Reverse Order (merge order), P1908
Description

TOM and JERRY, the cat and CAT, have been competing again recently, but after all they are adults. They no longer like to play the game that you catch up with. Now they like to play statistics. Recently, TOM found something that humans call "backward-order pairs". This is defined as follows: for a given positive integer sequence, reverse Order pairs are the ordered pairs of ai> aj and I <j in the sequence. After knowing this concept, they will first calculate the number of reverse pairs in a given positive integer sequence.

Input/Output Format

Input Format:

 

The first row, n, indicates the number of n in the sequence.

N number in the second row, indicating the given sequence.

 

Output Format:

 

Number of reverse pairs in a given sequence.

 

Input and Output sample input sample #1: Copy
65 4 2 6 3 1
Output example #1: Copy
11
Description

For 50% of data, n ≤ 2500

For 100% of data, n ≤ 40000.

 

 

 1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 using namespace std; 5 const int MAXN=100001; 6 inline int read() 7 { 8     char c=getchar();int x=0,flag=1; 9     while(c<'0'||c>'9')    {if(c=='-')    flag=-1;c=getchar();}10     while(c>='0'&&c<='9')    x=x*10+c-48,c=getchar();return x*flag;11 }12 int n;13 int a[MAXN];14 int tmp[MAXN];15 int ans=0;16 void Sort(int l,int r)17 {18     if(l==r)    return ;19     int mid=l+r>>1;20     Sort(l,mid);Sort(mid+1,r);21     int nowl=l,nowr=mid+1,nowpos=l;22     while(nowl<=mid&&nowr<=r)23     {24         if(a[nowl]<=a[nowr])    tmp[nowpos++]=a[nowl],nowl++;25         else tmp[nowpos++]=a[nowr],nowr++,ans=ans+mid-nowl+1;26     }27     while(nowl<=mid)    tmp[nowpos++]=a[nowl],nowl++;28     while(nowr<=r)        tmp[nowpos++]=a[nowr],nowr++;29     for(int i=l;i<=r;i++)    a[i]=tmp[i];30 }31 int main()32 {33     n=read();34     for(int i=1;i<=n;i++)    a[i]=read();35     Sort(1,n);36     printf("%d",ans);37     return 0;38 }

 

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.