Rokua P1908 Inverse Pairs (merge sort)

Source: Internet
Author: User

Title Description

Cat Tom and Little mouse Jerry have recently played again, but after all, they are adults, they do not like to play that race each other game, now they like to play statistics. Tom's old cat recently looked up what humans call the "reverse pair", which is defined as: for a given sequence of positive integers, the inverse pair is the ordered pair of Ai>aj and i<j in the sequence. Knowing the concept, they compete on who first calculates the number of reverse pairs in a given sequence of positive integers.

Input/output format

Input format:

The first line, a number n, indicates that there are n numbers in the sequence.

The second row n number that represents the given sequence.

Output format:

The number of reverse pairs in the given sequence.

Input and Output sample input sample #: Copy
65 4 2 6 3 1
Output Example # #: Replication
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 namespacestd;5 Const intmaxn=100001;6InlineintRead ()7 {8     CharC=getchar ();intx=0, flag=1;9      while(c<'0'|| C>'9')    {if(c=='-') flag=-1; c=GetChar ();}Ten      while(c>='0'&&c<='9') x=x*Ten+c- -, C=getchar ();returnx*Flag; One } A intN; - intA[MAXN]; - intTMP[MAXN]; the intans=0; - voidSort (intLintR) - { -     if(L==R)return ; +     intMid=l+r>>1; -Sort (L,mid); Sort (mid+1, R); +     intNowl=l,nowr=mid+1, nowpos=l; A      while(nowl<=mid&&nowr<=R) at     { -         if(A[nowl]<=a[nowr]) tmp[nowpos++]=a[nowl],nowl++; -         Elsetmp[nowpos++]=a[nowr],nowr++,ans=ans+mid-nowl+1; -     } -      while(Nowl<=mid) tmp[nowpos++]=a[nowl],nowl++; -      while(nowr<=r) tmp[nowpos++]=a[nowr],nowr++; in      for(inti=l;i<=r;i++) a[i]=Tmp[i]; - } to intMain () + { -n=read (); the      for(intI=1; i<=n;i++) a[i]=read (); *Sort (1, n); $printf"%d", ans);Panax Notoginseng     return 0; -}

Rokua P1908 Inverse Pairs (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.