Algorithm Basics-Reverse number, merge sort

Source: Internet
Author: User

In one arrangement, if the front and back positions of a pair of numbers are opposite to the size order, that is, the previous number is greater than the subsequent number, then they are called an inverse. 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.

-----------------------------------------------------------------------------

The idea of binary sorting, combined with two ordered series A and a, if the sequence b first element is small then the reverse number increases the current length of sequence A.

#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intN =50500;inta[2][n];intMain () {Long LongAns =0; intN;cin>>N;  for(intI=0; i<n;i++) scanf ("%d", a[0]+i); BOOLFlag =false; intAid,bid,cid;  for(intStride =1; stride<n;stride<<=1){         for(intI=0; i<n;i+= (stride<<1u) ) {Bid= (cid = AID = i) +Stride; intAED =std::min (bid,n); intBed = Std::min (bid+stride,n);  while(aid<aed&&bid<bed) {                if(a[flag][aid]<=A[flag][bid]) {a[!flag][cid++] = a[flag][aid++]; }                Else{a[!flag][cid++] = a[flag][bid++]; Ans+=aed-aid; }            }             while(aid<aed) a[!flag][cid++] = a[flag][aid++];  while(bid<bed) a[!flag][cid++] = a[flag][bid++]; } Flag= !Flag; } printf ("%lld\n", ans); return 0;}

Algorithm Basics-Reverse 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.