两套项目。集合A中的每个元素集合A中的O(nlogn)的每个项目,以项目集合B独特的比赛中集B.比赛时间 [英] Two sets of items. Each element of set A a unique match in set B. Match each item of set A to item in set B in O(nlogn) time

查看:109
本文介绍了两套项目。集合A中的每个元素集合A中的O(nlogn)的每个项目,以项目集合B独特的比赛中集B.比赛时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,要澄清一个问题:

So, to clarify the question:

设置A和B组  集合A中的每一个元素在集合B的合作伙伴  你不能排序任一组基于其比较同一组,即成员,家庭的每个B元素是没有区别集B(同样为A)的任何其他湾  当艾再配上双,你可以告诉我们,如果毕> AI 毕< AI 碧=爱。  设计一个算法O(nlogn)的运行时间。

Set A and Set B every element in set A has a partner in set B you can not sort either set based upon comparing it to members of the same set, ie, each b element of B is indistinguishable from any other b of set B (likewise for A). when Ai is matched with Bi you can tell if Bi > Ai, Bi < Ai or Bi = Ai. design an algorithm with O(nlogn) running time.

与二次时候答案显然是微不足道的,而不是有用的 - 尽管这是最好的,我想出了呢。日志(N)让我觉得,我应该使用递归或某种形式的二叉树,但我不知道如何可以创建一个二叉树,而能够比较来自相同的一组元素。另外,我不知道如何使用递归调用任何更大的影响,不只是为嵌套的for循环。任何提示将不胜AP preciated。

The obvious answer with quadratic time is trivial and not helpful -- although it's the best I've come up with yet. The log(n) makes me think that I should be using recursion or a binary tree of some sort, but I'm not sure how could create a binary tree without being able to compare elements from the same set. Also, I'm not sure how to use a recursive call to any greater effect than just running nested for loops. Any tips would be greatly appreciated.

推荐答案

您还没有说得很清楚,但你的问题看起来很像的的匹配螺母和螺栓的问题。

You haven't stated it very clearly, but your question looks suspiciously like the Matching Nuts and Bolts problem.

这个想法还有就是选择一个随机的螺母,找到匹配的螺栓B。分区使用螺母螺栓,并用螺栓B分区螺母,然后递归,像快速排序一样。

The idea there is to pick a random nut a, find the matching bolt b. Partition the bolts using nut a, and partition the nuts using Bolt b, and then recurse, like quicksort does.

(当然,我们说的是平均情况下为nlogn,而不是最坏的情况下)。

(Of course, we are talking about the average case being nlogn, rather than the worst case).

这篇关于两套项目。集合A中的每个元素集合A中的O(nlogn)的每个项目,以项目集合B独特的比赛中集B.比赛时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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