Hihocoder_ Two-point and merge sort in reverse order

Source: Internet
Author: User

I. TopicsTopic 1: Two-point and merge-sort in reverse ordertime limit:10000ms single point time limit:1000ms memory limit:256MB
Describe

We knew Nettle was playing "Battleship これ" on the previous, last, and upper back. After a bitter struggle, nettle again got a lot of boats.
This day nettle is checking its fleet list:

As we can see, the ship's default sort is the rank parameter. But in fact a ship's fire value and the level of the relationship is not big, so there will be a ship than B ship grade, but a ship fire is lower than the case of B ship. For example, the 77-stage dragon to two firepower is less than the 55-level of the XI-Li two.
Now that the nettle will give all the ship's firepower in order of rank, please calculate how many pairs of ships meet the above mentioned situation.

Tip: High firepower is fierce!

Input

Line 1th: an integer n. N indicates the number of ships, 1≤n≤100,000
Line 2nd: n integers, the number of I indicates the fire value of the ship with low rank a[i],1≤a[i]≤2^31-1.

Output

Line 1th: An integer that indicates how many pairs of ships meet "a ship is higher than B ship, but a ship's firepower is lower than ship B."

Sample input
101559614248 709366232 500801802 128741032 1669935692 1993231896 369000208 381379206 962247088 237855491
Sample output
27

two. Problem solving skillsaccording to the tip of the topic, the problem can be sorted using binary merge to sort the array, and the number of reverse order counts. The specific method is to divide the two sub-problems by the two-sorted sequence of the number of reverse and the sub-problem to merge, the next half of the elements moved to get the final number of reverse.

Three. Implementing the Code#include <iostream>

#include <vector>

using namespace Std;

Long Long Merge (vector<int>::iterator beginite, int number)
{
if (number < 2)
{
return 0;
}

int Mid = NUMBER/2;

int indexfirst = 0;
int indexsecond = Mid;

Vector<int> Tmparray;
Long long, Count = 0;
int Index = 0;
while ((Indexfirst < Mid) && (Indexsecond < number))
{
int tmpfirst = * (Beginite + indexfirst);
int tmpsecond = * (Beginite + indexsecond);

if (Tmpfirst > Tmpsecond)
{
Tmparray.push_back (Tmpsecond);
Count + = indexsecond++-index++;
}
Else
{
Tmparray.push_back (Tmpfirst);
indexfirst++;
index++;
}
}

while (Indexfirst < Mid)
{
Tmparray.push_back (* (Beginite + indexfirst++));
}

while (Indexsecond < number)
{
Tmparray.push_back (* (Beginite + indexsecond++));
}

Copy the array back
for (int Index = 0; Index < number; index++)
{
* (beginite++) = Tmparray[index];
}

return Count;

}


Long Long mergesort (vector<int>::iterator beginite, int number)
{
if (number < 2)
{
return 0;
}

int Mid = NUMBER/2;
Long long, Count = 0;
Count + = MergeSort (Beginite, Mid);
Count + = mergesort (Beginite + Mid, number-mid);
Count + = Merge (beginite, number);

return Count;

}


int main ()
{
int number = 0;
CIN >> number;

int Index = 0;
Vector<int> Array;
while (index++ < number)
{
int Tmp = 0;
Cin >> TMP;
Array.push_back (TMP);
}

cout << mergesort (Array.begin (), number) << Endl;

for (int Index = 0; Index < number; index++)
//    {
cout << Array[index] << Endl;
//    }

cout << "The result is" << Count << Endl;



return 0;
}

Four. ExperienceThis problem has made me see, I remember my previous practice is to use the STL inside the sort function array to order, and then according to the order of the elements of the array after the subscript and the same element in the original array in relation to the subscript, to calculate the number of reverse pairs (that is, reverse number). This method not only needs to sort the time O (NLGN), when using hash_table, also need O (n) time to determine the element in the sorted array of the subscript and the original array of the relationship between the subscript, a little bit more complex. It is a practical method to use two-point merge to sort the order, and also to recursively calculate the number of inverse pairs in the array. This method is mainly based on the number of inverse pairs in the array equal to the number of inverse pairs that exist in the sub-problem, plus the number of inverse pairs that exist in the merge process. The number of reverse pairs that exist in the merge process equals the number of bits that need to be moved when the next half of the elements move to the correct position.


Copyright, welcome reprint, reproduced Please indicate the source, thank you



Hihocoder_ Two-point and merge sort in reverse order

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.