简体   繁体   English

在所有元素都来自{1,2,…,n}集的数组中,哪个数组的反转次数最多? 它有多少个反转?

[英]Among all arrays with elements from the set {1, 2, …, n}, which array has the most inversions? How many inversions does it have?

Setup: 设定:
Let E[1 .. n] be an array of n distinct numbers. E[1 .. n]n不同数字的数组。
Let i,j be indices of elements in array E. i,j为数组E中元素的索引。
If 1 ≤ i < j ≤ n and E[i] > E[j] , then the pair of indices (i, j) is called an inversion of array E. 如果1 ≤ i < j ≤ n并且E[i] > E[j] ,则索引对(i, j)称为数组E的求逆。

Question: 题:
Among all arrays with elements from the set {1, 2, …, n} : 在具有集合{1, 2, …, n}元素的所有数组中:
1. Which array has the most inversions? 1.哪个数组的反转次数最多?
2. How many inversions does it have? 2.它有多少个反转?

My idea: 我的想法:
My initial thoughts was, if the array element is in sequence, doesn't it mean that the array is sorted and hence the inversion count is 0? 我最初的想法是,如果数组元素是按顺序排列的,那不是意味着该数组已排序,因此反转计数为0? If the array is sorted in descending order that inversion count is the maximum so the first element should have n-1 inversion, second is n-2 inversion all the way until the last element, 2 which have 1 inversion count. 如果数组按降序排列,则反转计数为最大,因此第一个元素应具有n-1个反转,第二个一直是n-2个反转,直到最后一个元素(2个具有1个反转计数)。 So I add up (n-1) + (n-2) + (n-3) +...+ 1 = n(n-1)/2. 所以我加起来(n-1)+(n-2)+(n-3)+ ... + 1 = n(n-1)/ 2。 I would like to ask is this question asking any orders? 我想问这个问题是否要求任何命令? Is my thinking correct? 我的想法正确吗? How should I solve it? 我该怎么解决?

Yes, your reasoning is correct - the maximal number of inversions is obtained when the array is reverse sorted. 是的,您的推理是正确的-对数组进行反向排序时,将获得最大数量的反转。 You can prove it formally by induction (if you really want). 您可以通过归纳法正式证明它(如果您确实需要)。

For an array with two elements, say, {1, 2} , check the two options. 对于具有两个元素的数组,例如{1,2} ,请检查两个选项。

For an array with n elements, the number of inversions can be divided to those attributed to the smallest element, and those attributed to the rest. 对于具有n个元素的数组,可以将求反数除以归因于最小元素的归因数和归因于其余元素的归因数。 By the induction hypothesis, the inversions attributed to the rest are obtained when the array (except for the last element, perhaps), is sorted in reverse order. 通过归纳假设,当数组(也许最后一个元素除外)以相反顺序排序时,将获得归因于其余部分的求逆。 The number of inversions attributed to the smallest element is the most when it is the last. 归因于最小元素的倒数是最后一个元素时的最大值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM