36-Find the inverse of the array

Source: Internet
Author: User
Tags array sort

Title Description: http://ac.jobdu.com/problem.php?pid=1348
In the array of two digits, if the previous number is greater than the number that follows, then these two numbers form an inverse pair. Enter an array to find the total number of reverse pairs in this array.
As entered in {7,5,6,4} , there is a total of 5 reverse pairs:(7,6) (7,5) (7,4) (6,4) (5,4)

Analytical:

The intuitive idea is:

Starting with the first number, let's compare it to each number in the back, and find out all the reverse pairs. Complexity of Time o ( n 2 )

Improvement Ideas:

So long as a number is compared to every other number, then the time is o ( n 2 ) 。 So, based on the comparison of n^2 how to optimize, we can associate to the sorting process, the basic sort is n^2, the improved sorting algorithm is n l o g n 。

The idea of adopting Merge-sort:
The original array, evenly divided into the left and right sub-array in the reverse order (and the left to the array sort), and then find the left and right sub-array merge when the reverse pair , will be ordered around the array into 1 arrays.

For example: Left array {5,7}, right array {4,6}, merge left and right arrays
Because 7 is greater than 6, the inverse logarithm is +2 ( the remaining length of the right array ), 5 < 6 does not produce reverse pairs, 5 > 4, and the reverse logarithm is +1.
That
(1) If the maximum value of the existing left array is greater than the maximum value of the existing right array, the length of the inverse logarithm + right array;
If the maximum value of the existing left array is less than the maximum value of the existing right array, there is no reverse order;
(2) Put the left and right maximum values into the cache array, and use the merge algorithm to merge the left and right end into an ordered array.

The total number of final reverse pairs = left reverse logarithm + right inverse logarithm + left and the reverse logarithm of the merge ;

#include <iostream>using namespace std;intInvertpaircore (int* Nums,int*Copy,intStartintEnd);intInvertpair (intNums[],intLength) {if(nums = = NULL | | length <=0)return 0;int*Copy= newint[Length]; for(inti =0; i < length; i++)Copy[i] = nums[i];intresult = Invertpaircore (Nums,Copy,0, length-1); DeleteCopy;returnResult;}intInvertpaircore (int* Nums,int*Copy,intStartintEnd) {if(nums = = NULL | |Copy= = NULL | | Start >= end)return 0;intMid = (start+end)/2;intLeft_pairs =0, Right_pairs =0; Left_pairs = Invertpaircore (Copy, Nums, start, mid);//Note here, the input array is copy because copy is already in orderRight_pairs = Invertpaircore (Copy, Nums, mid+1, end);intCount =0;The number of reverse pairs that are connected to//around a string    inti = Mid, j = end;intK = end;//Copy index    //Merges the left and right ordered sub-arrays into an ordered array     while(I >= start && J >= mid+1) {if(Nums[i] > Nums[j]) {Copy[k--] = nums[i--];        Count + = J-mid; }Else{Copy[k--] = nums[j--]; }    } while(I >= start)Copy[k--] = nums[i--]; while(J >= mid+1)Copy[k--] = nums[j--];returnLeft_pairs + Right_pairs + count;}intMain () {intNums[] = {7,5,6,4}; cout << Invertpair (nums, sizeof (nums)/sizeof (nums[0])) << Endl;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

36-Find the inverse of the array

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.