Full arrangement and Reverse Order

Source: Internet
Author: User

Search Baidu for "Full sorting and Reverse Order Number" to find n multiple results, but the content is scattered. This article aims to summarize the problems in this aspect. If there are any incomplete or incorrect problems, please contact me.

The following is the text.

.

  I. Prerequisites

.

This part is a huge part of Baidu's search, but it should be emphasized.

.

1. Full Arrangement

M (m ≤ n) elements from n different elements are arranged in a certain order, which is called an arrangement of M elements from n different elements. When M = N, all data is arranged in full order of N. [1] for the full arrangement of N, a total of n! .

2. Reverse Order, reverse ordinal number, odd and even order

In an arrangement, if the front and back positions of a pair of numbers are in the opposite order of size, that is, the front number is greater than the back number, they are called a reverse order. The total number of reverse orders in an arrangement is called the number of reverse orders in this arrangement. An even-number sort is called an even-number sort; an odd-number sort is called an odd sort. [2]

For example, for the full arrangement of N = 3:

Full Arrangement

123

231

312

132

213

321

Number of reverse orders

0

2

2

1

1

3

Parity

Even

Odd

.

  Ii. Related Questions

.

1. Given an arrangement, calculate its number in reverse order. [3]

Question: given an arrangement, find the number of reverse orders.

Analysis: Set P1, P2 ,..., If the PN is a full arrangement of N, the reverse order is t = t1 + T2 +... + Tn =

Among them, Ti is the number that ranks before PI and is larger than Pi.

This part of the code is relatively simple. Skip it here.

.

2. Sort the numbers in reverse order. [4]

Problem: given an N-element arrangement, its number of reverse orders exists and is unique. In turn, it is known that the number of reverse orders for an N-yuan arrangement is M. How many kinds of such N-yuan arrangement are there?

Analysis: We use F (n, m) to represent the number of N-yuan orders with a reverse order of M, then the problem of finding the number of permutation that meets the condition is converted to F (n, m).

To obtain the final result, we first study the properties of F (n, m. The following propositions can be obtained:

[Proposition1]For anyN> = 2And0 <= m <= C (n, 2)HourF (n, m)> = 1; WhenM> C (n, 2)Hour, F (n, m) = 0

C (a, B) indicates the number of elements selected from Element.

Proof: prove the First Half of Proposition 1.

(1) When n = 2, m can only be 0 or 1. F () = 1, F () = 1

(2) If n = K, the proposition is true, that is, F (K, M)> = 1 (0 <= m <= C (K, 2 ))

When N = k + 1, arrange 1, 2, 3 ,..., K + 1 is 0 in descending order, so f (K +)> = 1

If M> = 1, consider the K-yuan arrangement of A1, A2,… with a reverse order of 1-1 ,..., AK (by induction assuming that this arrangement exists). Now insert k + 1 to A (k-1) and AK to get a k + 1 yuan arrangement A1, A2 ,..., A (k-1), k + 1, AK, the number of reverse orders in this arrangement is m, so f (k + 1, m)> = 1

In conclusion, for all 0 <= m <= C (n, 2), F (n, m)> = 1

Prove the second half of the proposition (obviously ).

For an N-element arrangement, if you want to maximize the number of reverse orders, any two numbers in the Arrangement constitute a reverse order, with the number of reverse orders being C (n, 2 ), therefore, it is impossible to arrange N yuan so that the number of reverse orders is greater than C (n, 2)

[Proposition2]
F (n, m) = f (N, C (n, 2)-m)

Proof: For an N-yuan arrangement with a reverse order of M, A1, A2 ,..., An, N yuan arrange an, A (n-1 ),..., A2 and A1 are in the descending order of C (n, 2)-M. Similarly, for an N-yuan arrangement of C (n, 2)-M, A1, A2 ,..., An, N yuan arrange an, A (n-1 ),..., A2 and A1 are in reverse order m.

As a result, the N-yuan permutation set with the reverse order number m and the N-yuan permutation set with the reverse order number C (n, 2)-m are one-to-one ing, so f (n, m) = f (N, C (n, 2)-m)

[Proposition3]
F (n + 1, m) = f (n, m) + f (m-1) +
...+ F (n, m-N)

Consider from 1, 2 ,..., An array composed of N: A1, A2 ,..., An, because any one is composed of 1, 2 ,..., An arrangement composed of N, N + 1 can be viewed as the left side of A1 That n + 1 joins, or the right side of an, or AI and a (I + 1) between (1 <= I <= N-1. Since n + 1 is added to any position, there is no greater number than N + 1 before it, the number behind it is smaller than n + 1, and after N + 1 is added, this increases the number of numbers larger than itself by 1 for each subsequent number. For the number before n + 1, the number before it is larger than itself. So when n + 1 is selected to add A1 to the left, A1, A2 ,..., The reverse order of an is m + n. When n + 1 is added to the (1 <= I <= N-1) between AI and a (I + 1), A1, A2, ..., The reverse order of an is m + n-I. When an is added to the right, A1, A2 ,..., The number of reverse orders of an is still M.

Therefore, for any n + 1 yuan arrangement with a reverse order of M, consider the position of N + 1. If n + 1 is at the K bit, the other n elements constitute the n-yuan arrangement of M-N + k-1, and for any reverse order number M-N + k-1 N yuan arrangement, when n + 1 put in the arrangement of a certain, so in the new arrangement n + 1 in the K bit, then the new order is a n + 1 yuan arrangement with a reverse order of M.

Therefore, the set of N + 1 yuan in the descending order of M and the set of N yuan in the descending order of m are one-to-one correspondence. In this way, for an n + 1 yuan arrangement, F (n, m) indicates the number of N + 1 arranged at the end, F (n, s-1) indicates the number of N + 1 in the second-to-last ranking ......, F (n, m-N) indicates the number of top N + 1 arrays, and F (n + 1, m) indicates the sum of these arrays.

So F (n + 1, m) = f (n, m) + f (N m-1) +... + F (n, m-N) (Note: When B is less than 0, f (a, B) = 0)

[Proposition4]
F (n, 0) = f (N, C (n, 2) = 1

[Proposition5]
F (n, 1) = f (N, C (n, 2)-1) = N-1 (n> 1)

Proof: created by Proposition 3 and proposition 4

F (n, 1) = f (n-1, 1) + f (n-1, 0) = f (n-1, 1) + 1

Based on F () = 1, obtain proposition 5.

[Proposition6]
F (n, 2) = f (N, C (n, 2)-2) = C (n, 2)-1 (n> 2)

Proof: from Proposition 3, 4, and 5

F (n, 2) = f (n-1, 2) + f (n-1, 1) + f (n-1, 0) = f (n-1, 2) + n-1

Apply the above formula repeatedly. Based on F (2, 2) = 0, obtain Proposition 6.

Likewise, it can prove

F (n, 3) = C (n, 3)-C (n, 2)-C (n, 1)
(N> 3)

F (n, 4) = C (n, 4) + 2C (n, 3)-C (n, 1)
(N> 4)

F (n, 5) = C (n, 5) + 3C (n, 4) + 2C (n, 3)-C (n, 2) + 1
(N> 5)

...

We can use the previously derived F (n, 1) to calculate F (n, k) infinitely )~ F (n, k-1 ). However, it is difficult to obtain a general formula. This problem is similar to the use of sigma (K ^ t-(k-1) ^ t) to calculate sigma (K ^ (t-1), each next to use all the previous results.

In Proposition 3, we have obtained a recursive formula about F (n, m) and some initial values. Therefore, a program can be designed to input n, m, and F (n, m ). [5]

.

Reference code:

program code1;  var    a:array[1..500,1..500]of longint;    b,c:longint;    i,j,m,n,k:longint;  begin    a[2,0]:=1;    a[2,1]:=1;    for i:=3 to 500 do      for j:=0 to 499 do        begin          b:=j-i;          if b<0 then b:=0            else b:=j-i+1;          for k:=n to j do            c:=c+a[i-1,k];          a[i,j]:=c;          c:=0;        end;    readln(n,m);    writeln(a[n,m]);  end.

.

3. Calculate the original order based on the reverse order of each number. [6]

Problem: For a set S = {1, 2, 3 ,... any arrangement of n} A1, A2, A3 ,... an: We define that the reverse order of AI is Σ (AJ> ai | j <I), that is, the number of all the numbers before AI that are larger than that of AI. We sort the reverse order of each number by subscript to form a reverse order of A1, A2, A3,.... For example, the descending order of 3 1 2 is 1 1 0 (from left to right is 1, 2, 3 ). Now we provide a reverse order for the request to get the original order. [7]

Analysis: <developing... For a solution, see reference 7>

.

4. For the given number of reverse orders, find the minimum order that satisfies the number of the reverse orders. [8]

Problem: For the full arrangement of N, a positive integer m is given to find the smallest arrangement where the number of backward orders is M. For example, if n = 5 and M = 4, the smallest order of 4 is 1 3 5 4 2.

Analysis: To solve this problem, you must first understand the following propositions and theorems. (Note: In the proof, the number is greater than or less than the relationship between the two positions in the full arrangement. The value is smaller than or equal to the first and greater than the opposite)

[Theorem1]ForNIn reverse order (that isN, n-1 ,..., 2, 1) The maximum number of reverse orders. 

Proof: This is omitted. It can be simply proved by the reverse verification method.

[Proposition1]For an image1, 2, 3 ,..., I-1, I, n ,... I + 1SortQ(For exampleN = 5Time1, 2, 5, 4, 3), That isNMake sure that the first item is1And strictly set the tolerance1IncrementalNArrange any number of columns, 

(1)When numberNFollowed by a decreaseQThe maximum number of reverse orders, which isT = C (n-I, 2). 

(2)ArrangeQIs the number of reverse ordersT. 

Proof: First prove (1 ).

This proof is easy. Because the first I + 1 item of the series has been determined, the reverse Number of the entire series is only related to I + 2 to N items. Obviously, from Theorem 1, when I + 2 to N are completely inverted, the number of reverse orders is the largest. For example, the numbers 1, 2, 5, 4, and 3 are more than the numbers 1, 2, 5, 3, and 4.

Now prove (2 ).

Assume that there is a forward order of P That is m and P is smaller than Q. We know that the first I bit of Q is the smallest arrangement of the number N at the current position. That is to say, in Q, no matter whether it is any number after N or N, replace it with any number from 1 to I in Q and the result is arranged more than Q. For example, the positions of the first two numbers 1 and 2 in the order of Q:, 3 cannot be changed. Otherwise, the positions will be greater than Q, such as, and 2.

Therefore, we can only start with the I + 1 to n bits of Q. From (1) We can see that when n is in the I + 1 position, no matter how the number is arranged, the reverse order cannot exceed M. Therefore, the number n of P must be in any position from I + 2 to n.

Okay, let's assume that the number N is on the I + 2 bits. In this case, it is not difficult to find that we fill the largest number after N to the I + 1 digit, and the reverse number is the most. For example, for 1, 2, 5, 4, 3, we move 5 to 4th bits, then fill 4 to 3rd bits, and the arrangement is changed to 1, 2, 4, 5, 3. Obviously, this is like 1, 2, 3, 5, 4.

Let's analyze the reverse order of P. Because the number N reaches the second I + 2, the number of reverse orders composed of it is n-i-2. Similarly, the number of reverse orders consisting of I + 1 (4 in the example, n-1) is also a n-i-2. Obviously, due to the decline of the feature, the number of reverse orders from I + 3 to N is C (n-i-2, 2 ). (For example, C (1, 2) = 1) at this time, the total number of reverse orders is

T1 = C (n-i-2, 2) + 2 * (n-i-2)

= (N-i-2) (n-i-3)/2 + 2 * (n-i-2)

= (N-i-2) (n-I + 1)/2.

Q's reverse order number

T2 = C (n-I, 2)

= (N-I) (n-i-1)/2.

Use the t2-t1 to obtain the value of 1. That is to say, no matter what value n is, T2 is always greater than T1.

If we move n to the right, we only need to keep the first 1 to I + 1 (1, 2, 3, 4 in the example) only when the values remain unchanged can the subsequent transformations maximize the number of reverse orders (this is obviously not particularly proven ). However, after the I + 1 digit of the current table is determined, how can we change the number behind it to maximize the total number of reverse orders? We still need to fill the largest number after N to the I + 1 digit. We have proved that it is better to do so without optimization. Therefore, there is no sort P smaller than Q so that the reverse order of P is M. Pass.

[Proposition2]In the proposition1ArrangedQOn the basisNTheKDecimal and numberNThe previous number (that isI) Switch, and then make the numberNThe backend is in reverse order. In this way, the number of reverse orders contained in the new arrangement isT = C (n-I, 2) + KIn the descending order.T. 

Proof: First, I would like to emphasize the structure of Q:

Since the number N remains backward after the operation, the number from I + 1 to n is still C (n-I, 2 ). For X, there is a K-1 smaller than it before the exchange, and after the exchange is added, so the number of X in the reverse order (that is, the number of X as the left in the reverse order) for K-1 + 1 = K, then the total reverse order is C (n-I, 2) + K.

The proof of the minimum number of forward orders T = C (n-I, 2) + k is similar to the proof of Proposition 1, which is not proved in detail here.

So far, we can conclude that proposition 2 is also a true proposition.

With these two propositions as the basis, we can easily obtain the algorithm for this question. First, use Proposition 1 and binary search to find the position of number n. We can obtain the reverse order of number N in the form of Q at this position. Then the difference between T and M is calculated, K is obtained, and then transformed Based on Proposition 2. Finally, the required arrangement is obtained.

.

Reference code:

program code2;  var    m,n,p:int64;    i,j:longint;  function find(x:longint):longint;    var      l,r,mid,k1,k2:int64;    begin      l:=1;      r:=n;      while l<r do        begin          mid:=(l+r)>>1;          k1:=((mid-1)*mid)div 2;          k2:=(mid*(mid+1))div 2;          if (x<k2)and(x>=k1)then            exit(mid)            else begin                   if x>k1 then l:=mid+1;                   if x<k1 then r:=mid;                 end;        end;      if l=r then exit(l);    end;  procedure make;    var      k:int64;      i,j:longint;      v:array[1..50000]of boolean;    begin      fillchar(v,sizeof(v),false);      k:=m-((p*(p-1))div 2);      for i:=1 to n-p-1 do        begin          write(i,' ');          v[i]:=true;        end;      v[n-p+k]:=true;      v[n]:=true;      if n-p+k>0 then write(n-p+k,' ');      write(n,' ');      for i:=n downto 1 do        if not v[i] then write(i,' ');    end;  begin    readln(n,m);    p:=find(m);    make;  end.

.

This article describes four problems related to full-order and reverse-order pairs, which have limited levels and are incomplete. I hope some of them will help you. If you have any questions, please reply to this Article or contact me as soon as possible. Thank you!

.

Reference Source:

[1]: Baidu encyclopediaEntries: fully arranged

[2]: Baidu encyclopediaEntry: Number of reverse orders 

[3]: Question Source: classic question

[4]: Question Source: classic question

[5]: charlesgao mathematics blogHttp://www.charlesgao.com /? P = 19

[6]: Question Source: boj1275 structure original arrangement

[7]: Baidu space of JoaquinHttp://hi.baidu.com/joaquin_shj/blog/item/77961aea310d09d6d539c988.html

[8]: Question Source: A poj question (searching)/A Simulation competition-Doomsday legend

 

Additional reading:

[1]: HDU 1394Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1394

Related question:Http://blog.163.com/prevBlogPerma.do? Host = lyt9469 @ 126 & SRL = 170442358201122002733316 & mode = Prev

[2]: poj 2828Http://poj.org/problem? Id = 2828

[3]: BoJ 1197

 

Address: http://www.cnblogs.com/saltless/archive/2011/06/01/2065619.html

(Saltless original, reprinted please indicate the source)

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.