ultra-quicksort--poj2299 (merge sort to find inverse number pairs)

Source: Internet
Author: User

http://poj.org/problem?id=2299

Merge sort: For example now there is a sequence [l,r], we can divide this sequence into two sequences [L,mid], [mid,r], using recursion to follow the

The method gradually reduces the sequence, the order of the sub-sequence, and then the sequence of sub-sequences, and then the orderly interval of the merger, very good drop embodies the idea of division.

Number of reverse order (if there is i<j, A[i] > A[j], then a[i] and a[j] are reversed number pairs)

#include <stdio.h>#include<algorithm>#include<math.h>#include<cstring>#include<cstdlib>#include<queue>#include<iostream>using namespacestd;#defineN 500050#defineMemset (A, B) memset (A,b,sizeof (a))Long Longcont;intA[n],b[n];voidMergeintLintR) {    if(l>=R)return; intMid= (L+R)/2;    Merge (L,mid); Merge (Mid+1, R); intx=l; intY=mid+1, i=l;  while(X<=mid | | y<=r) {if(Y>r | | (X<=mid && a[x]<A[y])) B[i++]=a[x++]; Else        {            if(x<=mid) Cont+=mid-x+1; B[i++]=a[y++]; }    }     for(i=l;i<=r;i++) A[i]=b[i];}intMain () {intN;  while(SCANF ("%d",&N), N) { for(intI=0; i<n;i++) {scanf ("%d",&A[i]); } cont=0; Merge (0, N-1);///Merge Sortprintf"%lld\n", cont); }    return 0;}

ultra-quicksort--poj2299 (merge sort to find inverse number pairs)

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.