简体   繁体   English

找到大小为k的子集,使得值之间的最小距离最大

[英]Find subset of size k such that the minimum distance between values is maximum

Suppose i have an array which contain n integers . 假设我有一个包含n整数的数组。
How to find subset of size k such that the minimum distance between all pairs of integers in the subset is maximized , i mean they are at farthest distance . 如何找到大小为k的子集,使得子集中所有整数对之间的minimum距离maximized ,我的意思是它们处于最远距离。

example : array a[]={1,2,6,7,10} and k=3 , 例如:数组a[]={1,2,6,7,10}k=3
subset = {1,6,10} , the minimum distance is 4 between 10 and 6 . subset = {1,6,10} ,最小距离为4到10之间。
Wrong subsets : 错误的子集
{1,7,10} , minimum distance is 3 {1,7,10} ,最小距离为3
{1,2,6} , minimum distance is 1 {1,2,6} ,最小距离为1

I came up with a solution : 我提出了一个解决方案:

1) sort array 1)排序数组
2) select a[0] , now find ceil(a[0]+ x ) = Y in array ....and then ceil(Y+ x ) and so on k-1 times , also kth element will be a[n-1] 2)选择a [0],现在在数组中找到ceil(a [0] + x )= Y ....然后ceil(Y + x )等等k-1次,同样kth元素将是a[n-1]

To find x : 找到x
dp[i,j] be the x for selecting j elements from first i elements . dp[i,j]是用于从第一个i元素中选择j个元素的x
Finally we want dp[n][k] which is x 最后我们想要dp[n][k] ,即x

But i am facing problem in finding x . 但我在寻找x遇到了问题。

dp[i,j] = max( min( dp[k,j-1], dp[i]-A[k] ) ) dp [i,j] = max(min(dp [k,j-1],dp [i] -A [k]))
over k=1 to i-1 , i=2 to n , j=2 to i 在k = 1到i-1,i = 2到n,j = 2到i

dp[i][1] = 0 over i = 1 to n dp [i] [1] = 0,i = 1到n

EDIT : I want to correct the dynamic programming solution , though i know x can be found out by binary searching over x . 编辑 :我想纠正动态编程解决方案,虽然我知道x可以通过二进制搜索x找到。

UPDATE 2 : I have updated the code , but still not getting correct solution . 更新2 :我已更新代码,但仍未获得正确的解决方案。 Please point the error . 请指出错误。
code : http://ideone.com/J5vvR9 代码: http//ideone.com/J5vvR9

UPDATE 3 : Thanks @Gassa , @Niklas B. and @Fallen for your answers !. 更新3 :感谢@Gassa,@ Niklas B.和@Fallen的回答!

I think there is no need in finding x if time allows to search for possible values of x . 我认为这是在寻找无需x如果时间允许搜索的可能值x Just add the outer loop which will be a binary search on the answer (that is, the minimum distance, let us call it x ). 只需添加外部循环,这将是答案的二进制搜索(即最小距离,我们称之为x )。

Once x is fixed, you can greedily pick values starting from a[0] . 一旦x被修复,你可以贪婪地从a[0]开始选择值。 The next selected value will be such a[i] that i is minimal and a[i] - a[0] >= x . 下一个选定的值将是a[i] ,即i是最小值,而a[i] - a[0] >= x The third one will be a[j] such that j is minimal and a[j] - a[i] >= x , and so on. 第三个将是a[j] ,使得j是最小的并且a[j] - a[i] >= x ,依此类推。 If you are able to pick at least k values in this fashion, the actual answer is at least the current x ; 如果你能够以这种方式选择至少k值,那么实际答案至少是当前的x ; if not, the answer is less than x . 如果没有,答案就小于x

The total running time will be O (n log (C)) where C is the total number of possible values in the array. 总运行时间将为O(n log(C)),其中C是数组中可能值的总数。 Say, if the integers in the array are from 0 to 1000000, C will be 1000001 and log (C) (rounded up) will be 20. First, you try x = 500000 ; 比如说,如果数组中的整数是0到1000000,C将是1000001,log(C)(向上舍入)将是20.首先,你尝试x = 500000 ; if it fails, you are left with the range [0; 如果它失败了,你留下的范围是[0; 500000) for the answer; 500000)答案; if not, with the range [500000; 如果没有,范围[500000; 1000000], etc. 1000000]等

The base should be: 基数应该是:

dp[i][1] = INFINITY for i = 1 to n

The reason being that minimum of an empty set is positive infinity. 原因是空集的最小值是正无穷大。

In practice, any integer larger than the maximum possible a[i] - a[j] for some i and j will suffice as an INFINITY constant. 实际上,任何大于某个ij的最大可能a[i] - a[j]整数都可以作为INFINITY常量。

Additionally, the correct transition would be: 此外,正确的过渡将是:

dp[i,j] = max{for k=1 to i-1} (min(dp[k,j-1], a[i]-a[k]))

Do a binary search over value of X. Then for each such x, write a DP/Greedy function that checks if there's an array with result(maximal distance between elements) more than or equal to X. 对X的值进行二进制搜索。然后对于每个这样的x,编写一个DP / Greedy函数,检查是否存在一个结果(元素之间的最大距离)大于或等于X的数组。

Correctness: If for any X, we can have M elements, such that minimum distance between them is greater than or equal to X, then for every x, x < X, at least this same array will server as result. 正确性:如果对于任何X,我们可以有M个元素,使得它们之间的最小距离大于或等于X,那么对于每个x,x <X,至少这个相同的数组将作为结果服务。 And for any X, if there's no M elements, such that the minimum distance between the elements is greater than or equal to X, then for no x, x > X, M such elements are available. 并且对于任何X,如果没有M个元素,使得元素之间的最小距离大于或等于X,那么对于无x,x> X,M这样的元素是可用的。 So we can binary search on X. 所以我们可以在X上二进制搜索。

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

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