排列数组中的行以消除增加的子序列
以下问题摘自算法问题(问题 653):
给你一个 x 2 的数字矩阵。找到一个 O(n log n) 算法,该算法对数组中的行进行排列,使得数组的两列都不包含长度超过 ⌈√n 的递增子序列(可能不包含连续的数组元素)。⌉
我不确定如何解决这个问题。我认为它可能会使用某种分而治之的循环,但我似乎找不到。
有谁有任何想法如何解决这个问题?
The following problem is taken from Problems on Algorithms (Problem 653):
You are given a n x 2 matrix of numbers. Find an O(n log n) algorithm that permutes the rows in the array such that that neither column of the array contains an increasing subsequence (that may not consist of contiguous array elements) longer than ⌈√n.⌉
I'm not sure how to solve this. I think that it might use some sort of divide-and-conquer recurrence, but I can't seem to find one.
Does anyone have any ideas how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我的解决方案。
1) 根据第一个元素从最大到最小对行进行排序。
2) 将其分成 ⌈√n⌉ 组,剩下的(最多不超过 ⌈√n⌉ 组)
3) 根据第二个元素从大到小对每组中的行进行排序
正确性证明:< /strong>
第 1 列中的递增子序列只能发生在单个组中(大小 <= ⌈√n⌉),
第 2 列中的递增子序列中没有 2 个元素在同一组(不超过⌈√n⌉组)
Heres's my solution.
1) Sort rows according to the first element from greatest to lowest.
2) Divide it into groups of ⌈√n⌉, and what is left(no more then ⌈√n⌉ groups)
3) Sort rows in each group according to the second element from greatest to lowest
Proof of correctness:
Increasing subsequences in column 1 can happen only in single group(size is <= ⌈√n⌉),
No 2 elements of increasing subsequences in column 2 are in the same group(no more than ⌈√n⌉ groups)