发现在阵列的(I,J)对总数使得i&所述; j和一个由[i]>一种[j]的 [英] find total number of (i,j) pairs in array such that i<j and a[i]>a[j]

查看:127
本文介绍了发现在阵列的(I,J)对总数使得i&所述; j和一个由[i]>一种[j]的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如在问题中提到,需要寻找的总数(I,J)对,这样

As mentioned in the question ,need to find total number of (i,j) pairs in array such that

(1) **i<j** 
(2) **a[i]>a[j]**

,其中i和j是该阵列的索引。有没有空间限制。

where i and j are indices of the array . There are no space constraints .

我的问题是

 1) Is there any approach which takes less than O(N^2) time?
 2) if so what is least complexity ?
 3) How do we prove that ? 

我希望我的问题清楚了。

I hope i'm clear with the question .

我的做法如下:

在做这个问题的方法之一是使用蛮力脱颖而出这需要O(N ^ 2)时间。

One way of doing the question is using brute fore which takes O(N^2) time .

但我认为,应该有一个更好的优化解决方案在-至少O(NlogN)sollution。原因这个问题,我的直觉是如下:

But I think that there should be a better optimised solution to this question at-least O(NlogN) sollution .The reason for my intuition is as follows

直觉
     1)以升序排列条件的数组,我们有有:I&LT;Ĵ,一个[1] - ; A [J]这是类似我的问题。我也读了排序已经下界欧米茄(N log n)的。所以我的问题也应该有欧米茄(N log n)的。我可能是完全错误的,如果这样,请大家指正。

我的第二直觉是:

假设我们有元件的阵列如下:4,9,7,3,2,1,8,12

Suppose we have an array of elements as follows : 4,9,7,3,2,1,8,12

我们计算上述条件 I&LT;Ĵ,一个[I]&gt;将研究[J] 的元素4,如I = 0分至4时,可能值j是3,4,5自从一个[0]>一个[3],A [0]>一个[4],A [0]>一个[5],因此我的总无的(I,J)对现在是3。
 当我增加I(指数)为1下一次,j的候选条件值是2,3,4,5,6。但是,我们应该用事实,当(当[I] = 4),比一个少,我们计算了3个元素[I = 0]这是不是反过来更少的I = 1],所以我不会I = 0与3,2,1比较9(要去掉不必要的计算)。如果我们可以删除不必要的计算那么我们可以复杂性降低到为O东西少(N ^ 2),否则无解小于O(N ^ 2)存在。但是,如果再解决存在我们怎么办that.I试图使图形,但我的努力是徒劳的。

we calculate above condition i<j , a[i]>a[j] for element 4 ,as i=0 points to 4 ,the possible values for j are 3,4,5 .since a[0]>a[3],a[0]>a[4],a[0]>a[5] ,so my total no of (i,j) pairs for now is 3 . Next time when I increment i(index) to 1,the possibles values of j are 2,3,4,5,6 . But we should use the fact that when i=0 (when a[i]=4) we have computed 3 elements less than a[i=0] which is in turn less than a[i=1] , so i will not compare 9 with 3,2,1 (To remove unnecessary computations ).If we can remove unnecessary computations then we can reduce complexity to something less than O(N^2) or else no solution less than O(N^2) exists.But if solution exists then how do we do that.I tried making graph but my efforts are futile .

方法1)在-为了获得O(nlogn)的复杂性,我认为我们需要调整各地的快速排序或归并排序得到解决,但问题就在这里,如果我们我们失去了对数组进行排序元素的实际位置。

方法 2),为了得到O(NlogN)的时间解决方案,我认为使用树我们可以得到优化sollution。我没有得到任何线索。

方法3)如果存在任何O(N)时间的算法应该与散列。但是,在这种情况下,简单的散列行的事工作。

所以,请让我知道这上面的直觉和方法是正确的(如果正​​确的话哪种方法会导致优化sollution以及如何)。

So please let me know which of the above intuitions or approaches are correct (If correct which approach will lead to optimised sollution and how).

推荐答案

您可以倒计数的算法,类似于对到排序合并,作为解释的这里

You can count inverted pairs with the algorithm, similar to merge sort, as explained here.

我们的想法是排序合并计算时,有多少倒在每一步都改变了数组。

The idea is to merge sort the array while counting, how many inversions were changed on each step.

另一种方法是使用顺序统计树。您按顺序插入数组中的元素融入到这棵树,每个插入后看到,有多少个元素,preceding插入的元素,是比它大。

A different approach is to use an order-statistics tree. You sequentially insert elements of the array into this tree, and after each insertion see, how many elements, preceding the inserted element, are larger than it.

这是另一种下令统计树是可转位skiplist

An alternative to order-statistics tree is Indexable skiplist.

这两种算法有O(N日志N)的时间复杂度。

Both algorithms have O(N log N) time complexity.

要获得倒置的大致数量,O(N)的时间复杂度是可能有一些限制。我们可以修改铲斗排序中相同的方式合并排序被修改

To get approximate number of inversions, O(N) time complexity is possible with some limitations. We can modify Bucket sort in the same manner merge sort was modified.

在疏导相桶排序,我们应该估计桶较大元素的元素的数量,而在一些斗结束插入元素(在每个桶的元素保留在原来的顺序)。

On "scatter" phase of Bucket sort we should estimate number of elements in buckets for larger elements, while inserting element at the end of some bucket (elements in each bucket remain in original order).

在分类斗阶段的排序,我们应该修改(以同样的方式)排序算法(插入排序,最有可能的)。同时插入元素成适当的地方,我们应该算过有多少其他元素跳了下去。

On "sort" phase of Bucket sort we should modify (in the same way) sorting algorithm (insertion sort, most likely). While inserting the element into its proper place, we should count over how many other elements it jumped.

至于局限性,该算法与数字只能(或使用对象,易于转换为数字),我们应该事先知道这些数字是如何分布的。所以,如果我们有均匀分布的整数数组,该算法应能正常工作。

As for limitations, this algorithm works only with numbers (or with objects, easily convertible to numbers), and we should know in advance how these numbers are distributed. So, if we have an array of uniformly distributed integers, this algorithm should work properly.

这篇关于发现在阵列的(I,J)对总数使得i&所述; j和一个由[i]&gt;一种[j]的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆