About "Reverse Order Number"

Source: Internet
Author: User

Yesterday, we made a qualifying round for Tsinghua. Shen Da, Liang laoda, and Xiao cross each handled a single question, and almost fell out of 60. I did B and F, where F is about the number of reverse orders. The complexity is that the worst complexity of nlog2n + Mn may be reduced to O (N ^ 2 ). But the result I submitted is not TLE, but MLE and RE. I really don't know if there is a problem with the Tsinghua discriminant system or my program. In short, I am not satisfied, so I decided to spend some time today to summarize the "Reverse Order" questions, write a report for everyone, and provide some information. First, the reverse order (inversion pair) refers to the {A0, A1, a2... in an}, if AI <AJ (I> J), then (AI, AJ) the previous pair of reverse order. Inversion number is the number of Reverse Order pairs in the sequence. For example, if 1 2 3 is an order, the number of reverse orders is 0. If (2, 3) in 1 3 2 meets the condition of the backward order, the number of reverse orders is only 1; 3 2 1 (1, 2) (1, 3) (2, 3) satisfies the reverse order, so the reverse order is 3. It cannot be imagined by definition. The number of reverse orders of sequence n ranges from 0 to N * (n-1)/2], where the number of reverse orders is 0, in full reverse order, the number of reverse orders is N * (n-1)/2.

Currently, the fastest algorithm I know is to calculate the number of reverse orders for ACM/ICPC. the time complexity is nlog2n, and the space complexity is 2n. Java template (the server is on campus ).

Simple Principle of Reverse Order for merge:
Merging and sorting is the idea of divide and conquer. Let's read the book on your own. The reverse order of merge means that when the child sequences S1 and S2 are merged, if S1 [I]> S2 [J] (Reverse Order), the reverse order number is added with s1.length-I, because the numbers after I in S1 are in reverse order for S2 [J.

Tju2242:
Directly upload the template. Remember to consider M's parity.

PKU 1007:
Calculate the number of reverse orders, and then sort the output.

PKU 1804, PKU 2299:
It is the simplest question about backward-order pairs. The question is to give a sequence, and to find the minimum number of steps that may be moved in order, it must be moved adjacent to each other.
If A and B are adjacent to each other, if a <B is exchanged, the number of reverse orders is increased, so it is best not to do this exchange. If a = B is not exchanged, do not perform this exchange. When A> B, the exchange reduces the reverse order and makes the sequence more ordered.
It can be seen from the above that there is only one case of moving, that is, A> B, and the result of one moving is a descending order minus 1. Assume that the initial reverse order is N, and each moving minus 1 requires n moves, and the sequence changes to order. Therefore, the question can be converted to the reverse order of the sequence directly.

Zju1481:
This question is slightly similar to F in this qualifying round, but it is much simpler. The meaning of the question is to specify the sequence s, and then move the first item of the sequence to the end of the sequence in sequence, so that a total of N-1 operations will return to the original sequence (the operation is similar to the loop left shift ). How many of the n-1 operations and the original sequence have the smallest number of reverse orders?
With a template in hand, we can intuitively think of the fact that we can calculate the number of reverse orders for all N times, and then output the smallest one time, but the complexity of this operation is O (n * nlogn ), too complicated.
If only the reverse Number of the initial sequence is obtained, the total O (nlogn) can be guaranteed as long as the number of the n-1 operation can be obtained under the O (1) algorithm) complexity. In fact, for each operation, we can use the O (1) algorithm to obtain the number of reverse orders. After moving the AI in the sequence to the end of AJ, AI performs the J-I switching with the right neighbor, and each switching has three results: the reverse order + 1, the reverse order-1, and the reverse order remain unchanged. Because there are no identical items in the sequence described in the question, you can ignore them without changing the reverse order. The addition and subtraction in reverse order is to look at the relationship between AI and AJ (including AJ) numbers, so we can find the numbers between AI and AJ that are greater than AI and the numbers smaller than AI and then get the difference, that is, the reverse order value changes when AI moves to the end of AJ.
According to the above principle, because the question requires AI to move to the last number, and AI must be the first item, therefore, you only need to calculate the difference between the number larger than AI and the number smaller than AI. Then, this difference is added to the number of reverse orders of the previous operation, which is the reverse value after this operation.

PKU 2086:
This question is not about reverse order pairs, but about the number k in reverse order to create a sequence. The minimum sequence is required. The comparison values of the two sequences are compared from left to right, with a large sequence.
As a matter of fact, it is not difficult to create a sequence. As we can see from 1804, we only need to adjust the adjacent numbers to achieve a certain number of reverse orders. The difficulty is to find the smallest sequence. For example 1 2 3 4 5, the minimum sequence of Reverse Order 1 is required to exchange 4 5. If any other adjacent numbers are exchanged, the minimum number cannot be guaranteed. From this we can think that, to ensure the minimum sequence, the first part of the sequence can not move (because they are already the smallest), only change the second half. We know that the maximum reverse order of N numbers is N * (n-1)/2, so we can find a minimum P, so that K <p * (p-1)/2. The first half is 1 to n-P, and all the reverse orders are completed by the number of P in the second half.
Considering K = 7, n = 6, P = 5 is obtained, that is, the first part of 1 does not move, and the last five digits are adjusted. The maximum reverse order of 4 numbers is 5 4 3 2, the reverse order number is 6, the 5 number is 6 5 4 3 2, and the reverse order number is 10. We can guess that, to ensure that the four numbers in five do not move in the reverse order, adjusting the position of the other number can increase or decrease the reverse order, so that we can adjust any backward order between 6 and 10. To minimize the number, we can move the number as small as possible to the leftmost position. 2. After moving forward, adjust backward to 4. 3. After moving forward, adjust 3. 4. Adjust 2. 5. Adjust 1. If not, adjust 0. You can adjust 6 to 10. Therefore, the rule is to find the number to be adjusted, the rest is output in reverse order. The number to be adjusted can be obtained through the total reverse K-(p-1) * (P-2)/2 + (n-P.

PKU 1455:
This is a difficult question about reverse order number reasoning. The question requires n people to form a ring, and finding the number of adjacent exchange operations can change the neighbors of each person at least, that is, from the left to the right, from the right to the left. It is easy to think of is to give n person numbers, from 1 .. n, then the initial state is 1 .. n, then the right side of N is 1, and the target State is n .. 1. The left side of N is 1.
Initially, it seems that the result is the descending order (N * (n-1)/2 ?), However, the difficulty is that the sequence of this question is a loop. In the case of rings, it can be removed many times. First, from the non-ring situation, the original 1-N sequence should be converted to n-1 sequence, that is, n (n-1)/2 operations. Because it is a ring, (k)... 1, N. k + 1 can also be regarded as the target State. For example, the target of 1 2 3 4 5 6 can be 6 5 4 3 2 1, or 4 3 2 1 6 5. Therefore, the problem can be converted to the smallest number of reverse orders when K is taken in the target State of (k)... 1, N. k + 1.
After the above steps, the problem has been similar to zju1481. However, there are still rules to follow. For a K, his number is the number of the reverse order on the left + the number of the reverse order on the right, that is (K * (k-1)/2) + (n-k) * (n-k-1)/2) (k> = 1 & K <= N ). Expand, we can obtain the minimum number of reverse orders when K is equal to n/2 (n * n-n)/2). Now we can get the solution by inserting K into it.
Note that K is an integer, and n/2 is not necessarily an integer, so there is still room for modification to the formula, which can be changed to (n/2) * (n-1)/2.

PKU 2893:
The idea of finding the number of reverse orders is used, but there are still optimizations for the questions. We can see the optimization of M * n puzzle.

PKU 1077:
This is a classic search question. However, when no solution is found, the reverse order is a great deal. We can see the OCTA digital experiment report.

 

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.