两个数组的最大子集和
我什至不确定这是否可以在多项式时间内完成。
问题:
给定两个实数数组,
A = (a[1], a[2], ..., a[n]), B = (b[1], b[2], ..., b[n]), (b[j] > 0, j = 1, 2, ..., n)
和一个数字
k
,找到A的子集
,其中恰好包含A'
(A' = (a[i(1)], a[i(2)], ..., a[i(k)]))k
个元素,这样,(sum a[i (j)])/(sum b[i(j)])
最大化,其中j = 1, 2, ..., k
。
例如,如果 k == 3
,且结果为 {a[1], a[5], a[7]}
,则应
(a[1] + a[5] + a[7])/(b[1] + b[5] + b[7])
大于任何其他组合。有什么线索吗?
I am not even sure if this can be done in polynomial time.
Problem:
Given two arrays of real numbers,
A = (a[1], a[2], ..., a[n]), B = (b[1], b[2], ..., b[n]), (b[j] > 0, j = 1, 2, ..., n)
and a number
k
, find a subsetA'
ofA (A' = (a[i(1)],
, which contains exactly
a[i(2)], ..., a[i(k)]))k
elements, such that,(sum a[i(j)])/(sum b[i(j)])
is maximized, wherej = 1, 2, ..., k
.
For example, if k == 3
, and {a[1], a[5], a[7]}
is the result, then
(a[1] + a[5] + a[7])/(b[1] + b[5] + b[7])
should be larger than any other combination. Any clue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设
B
的条目为正(听起来这个特殊情况可能对您有用),则存在一个O(n^2 log n)
算法。我们首先解决一个问题,即对于特定的
t
,是否存在一个解使得分母清零,这个条件相当于
我们所要做的就是选择
k
code> a[i(j)] - t*b[i(j)] 的最大值。现在,为了解决
t
未知时的问题,我们使用动力学算法。将t
视为时间变量;我们感兴趣的是具有n
个粒子的一维物理系统的演化,该粒子具有初始位置A
和速度-B
。每个粒子最多与其他粒子交叉一次,因此事件数量为O(n^2)
。在交叉之间,sum (a[i(j)] - t*b[i(j)])
的最优值线性变化,因为k
的子集相同是最优的。Assuming that the entries of
B
are positive (it sounds as though this special case might be useful to you), there is anO(n^2 log n)
algorithm.Let's first solve the problem of deciding, for a particular
t
, whether there exists a solution such thatClearing the denominator, this condition is equivalent to
All we have to do is choose the
k
largest values ofa[i(j)] - t*b[i(j)]
.Now, in order to solve the problem when
t
is unknown, we use a kinetic algorithm. Think oft
as being a time variable; we are interested in the evolution of a one-dimensional physical system withn
particles having initial positionsA
and velocities-B
. Each particle crosses each other particle at most one time, so the number of events isO(n^2)
. In between crossings, the optimum ofsum (a[i(j)] - t*b[i(j)])
changes linearly, because the same subset ofk
is optimal.如果 B 可以包含负数,那么这就是 NP-Hard。
由于这个问题的 NP 难度:
在这种情况下,A 就变得无关紧要了。
当然,从你的评论看来,B 必须包含正数。
If B can contain negative numbers, then this is NP-Hard.
Because of the NP-Hardness of this problem:
The A becomes immaterial in that case.
Of course, from your comment it seems like B must contain positive numbers.