【发布时间】:2020-07-17 03:03:40
【问题描述】:
在政治活动中,介绍 2 个人决定了他们是否代表同一政党。 假设 n 参加者中有一半以上代表同一方。我正在尝试找到一种有效的算法,该算法将使用尽可能少的介绍来识别该党的代表。
蛮力解决方案是在与会者数组上维护两个指针,在 O(n2) 时间内将 n 个与会者介绍给 n-1 个其他与会者。我不知道如何改进这一点。
编辑:正式地,
You are given an integer n. There is a hidden array A of size n, such that more than half of the values in A are the same. This array represents the party affiliation of each person.
You are allowed queries of the form introduce(i, j), where i≠j, and 1 <= i, j <= n, and you will get a boolean value in return: You will get back 1, if A[i] = A[j], and 0 otherwise.
Output: B ⊆ [1, 2. ... n] where |B| > n/2 and the A-value of every element in B is the same.
希望这能更好地解释问题。
【问题讨论】:
-
我没有完全理解这个问题,但是 disjoint-set 你需要什么?
-
我不这么认为 - 我编辑了帖子以更清楚地解释问题。
-
只需在 O(n) 中执行快速排序的第一遍,就足以将数组分成大小不相等的两组。
-
这甚至让我更加不解。听起来你只需要存储一个类似哈希映射的东西并在
O(n)时间实现它,你甚至不需要快速排序 -
@PatrickRoberts 为什么是快速排序
O(n)?快速排序不是线性算法。
标签: algorithm