"Introduction to Algorithms" Introduction to the third edition of Exercise 2-4 Reverse Pairs _ algorithm

Source: Internet
Author: User

There are many answers to this question on the Internet, such as: Http://blog.csdn.net/qcgrxx/article/details/8005221 said in detail.

Questions about reverse alignment: http://www.cppblog.com/ickchen2/articles/62422.html

But in the last question on how to find the number of reverse pairs in a permutation, I know that many of the online modified merge sort O (NLGN) algorithm can be solved, but I have another use of stack O (n) method, I think it is correct, but by a certain volume is wrong, is very depressing, Below I write down the main process, I hope you can help me.

For example, an introduction to the algorithm, array a <2,3,8,6,1> has 5 reverse pairs, respectively (2,1), (3,1), (8,6), (8,1), (6,1).

So I can do this, using the stack data structure, which holds the number pairs (x,y), where x is the element in array a, and y the number of reverse pairs that can be formed at the beginning of x in the array. The initial stack is empty, the number of reverse pairs is ans=0, and the array is scanned from right to left, as follows:

1. Because the first element is 1 and cannot form a reverse order, the (1,0) is pressed into the stack.

2. Because 6 is larger than the top of the stack, so can be formed (6,1) this reverse, and because 1 corresponding to the reverse order is 0, so 6 corresponding to the reverse is 0++ that 1, so will (6,1) pressure stack, ans++ 1.

3. Because 8 is 6 larger than the top element of the stack, so it can be formed (8,6) This reverse pair, since 8 and 6 can form a reverse pair, then 8 can and with 6 elements are formed in reverse order, that is, 8 can form a 1++=2 reverse pair, so will (8,2) pressure stack, ans+=2 3.

4. Because 3 is smaller than the top element of the stack, the stack is (8,2).

5. Because 3 is smaller than the top element of the stack, the stack is (6,1).

6. Because 3 is larger than the top of the stack, at this time the top of the stack is (x,y) = (1,0), so you can form a y++=1 reverse pair, will (3,1) pressure stack, ans+=1 4.

7. Because 2 is smaller than the top element of the stack, the stack is (3,1).

8. Because 2 is larger than the top element of the stack, it can form a y++=0++=1 reverse pair, 2,1 the stack, ans+=1 5.

9. All elements have been tested, and the final ANS 5 is the number of reverse pairs.


Because the above algorithm simply traverses the array, the time complexity is O (n).

I think the above ideas are reasonable, do not know where there are mistakes, please advise.


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.