The sword refers to reverse order pairs in the offer (36) array.

Source: Internet
Author: User

Question: two numbers in the array. If the first number is greater than the next number, the two numbers form a reverse order.

 

Problem Analysis:

First, we should think of a very simple solution, traversing the array in sequence, comparing the number and the subsequent number for each number one by one, T (n) = O (N ^ 2)

 

(1) The overall meaning is to divide the array into two sections. First, calculate the number of reverse pairs in the segment. For example, the following two sections of code are used to calculate the number of reverse pairs in the array segments on both sides.

Count + = Merge (data, temp, first, mid); // find the number of reverse pairs in the left half segment count + = Merge (data, temp, Mid + 1, end ); // find the number of reverse pairs in the right half

 

(2) then calculate the number of Reverse Order pairs between two adjacent ordered segments.

First, the two adjacent segments are ordered in the following ranges: [first, mid] [Mid + 1, last]

We set two cursors, I and j, to indicate the last position of the two ordered segments: I = mid J = last

Note: When we find the reverse order between two segments, we need to keep the two segments in order.

Here we need a temp array, first store the result in temp, and finally copy temp to Data.

When temporary temp is saved, we move forward from the last position

 

We constantly compare data [I] AND DATA [J]:

If data [I]> data [J], because the next segment is ordered, data [I] is greater than each value in [Mid + 1, J] of the next segment, in this case, you need to save temp temporarily and increase the number of reverse orders.

If data [I] <= data [J], you only need to temporarily store temp.

 

int Merge(std::vector<int>& data, std::vector<int>& temp, int first, int last){    if (first == last) {        temp.at(first) = data.at(first);        return 0;    }    int mid = first + (last - first) / 2;    int left  = Merge(data, temp, first, mid);    int right = Merge(data, temp, mid + 1, last);    int i = mid;    int j = last;    int tempIndex = last;    int count = 0;    while (i >= first && j >= mid + 1) {        if (data.at(i) > data.at(j)) {            temp.at(tempIndex--) = data.at(i--);            count += (j - (mid + 1) + 1);        } else {            temp.at(tempIndex--) = data.at(j--);        }    }    while (i >= first) {        temp.at(tempIndex--) = data.at(i--);    }    while (j >= mid + 1) {        temp.at(tempIndex--) = data.at(j--);    }
std::copy(temp.begin() + first, temp.begin() + last + 1, data.begin() + first); return left + right + count;}int InvertPairs(std::vector<int>& data, std::vector<int>& temp){ if (data.size() == 0) { return 0; } int count = Merge(data, temp, 0, data.size() - 1); return count;}

 

 

 

 

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.