1019 Nod number in reverse order (merge sort)

Source: Internet
Author: User

Title Link: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1019

Test instructions: In one arrangement, if the front and back positions of a pair of numbers are opposite the size order, that is, the previous number is greater than the number of subsequent numbers, 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 inverse of the number of the two, one is in the process of bubble sequencing (direct violence enumeration, which is our normal O (n^2) level of the method) also is in the merge sort process, the time complexity can be reduced to O (NLOGN) on the merger sort of reverse order number, I lift a chestnut, A heap is (4 6 7 8), and the other pile is (2 6). We first merge the second pile of 2 into the first heap to find the first larger than its number 4, and then the resulting reverse number is len1-i+1=4-1+1=4 6 of the reverse number is 4-3+1=2. That's what this means .... The idea of merging sort is simple, which is to separate, order, and then merge. Be aware that updates to the data are good.
1#include <iostream>2#include <algorithm>3 using namespacestd;4 5 Const intn=50000+Ten;6typedefLong LongLL;7 8 LL CNT;9 intNum[n],result[n];Ten  One voidMergeint*num,int*result,intStartintend) { A     intT=start,mid= (start+end)/2; -     intI=start,j=mid+1; -      while(i<=mid&&j<=end) { the         if(Num[i]<=num[j]) result[t++]=num[i++]; -         Else if(num[i]>Num[j]) { -result[t++]=num[j++]; -cnt+=mid-i+1; +         } -     } +      while(I<=mid) result[t++]=num[i++]; A      while(j<=end) result[t++]=num[j++]; at      for(inti=start;i<=end;i++)//Update -num[i]=Result[i]; - } -  - voidMerge_sort (int*num,int*result,intStartintend) { -     if(end-start==1){ in         if(num[start]>Num[end]) { -             inttmp=Num[start]; tonum[start]=Num[end]; +num[end]=tmp; -cnt++; the         } *         return ; $     }Panax Notoginseng     if(end-start==0)return ; -Merge_sort (Num,result,start, (End+start)/2); theMerge_sort (Num,result, (End+start)/2+1, end); + merge (num,result,start,end); A } the  + intMain () { -     intN; $Cin>>N; $      for(intI=1; i<=n;i++) cin>>Num[i]; -Merge_sort (Num,result,1, n); -cout<<cnt<<Endl; the     return 0; -}

1019 Nod number in reverse order (merge sort)

Related Article

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.