英文:
Operating on a pair of elements in an array and dropping one
问题
有一类问题,要求我们从给定的数组中随机选择两个元素,对这些元素执行一个操作,将结果添加到一个"答案"中,然后删除其中一个元素并将另一个元素放回数组中。
最近,我遇到了这个问题 - 给定一个数组,选择两个元素a和b,将(a XOR b)添加到"答案"中。然后删除a或b中的一个,并将另一个元素放回数组中。重复这些操作,直到数组中只剩下一个元素。你能得到的"答案"的最大值是多少(请参阅问题这里)。
示例输入/输出:-
arr = [3,2,1]
Output = 5;
可能的操作 - 2 ^ 1 (= 3) -> 删除2 -> 3 ^ 1 (= 2) -> 删除3。将3 + 2相加得到5,这是答案的最终值。
现在,根据这个问题,我可以编写一个递归+回溯的代码来处理它,但当然时间复杂度是阶乘的顺序。我一直在研究这个问题,似乎可以通过最小/最大生成树来处理。我不明白为什么最大生成树在这里有效。我在问题帖子上发表了评论,以下是评论的一部分。
通过一个场景 - 当按照最大生成树的Prim算法时,假设我们选择的第一条边是(A,B)。现在我们选择的下一条边应该是连接到A或B并且具有最大权重的边。假设我们选择了(A,C)。同样,现在我们需要选择一个连接到A或B或C并且具有最大权重的边。它可能是一条边(A,B)。现在,在这种情况下,我们已经选择(A,C)和(A,B)都作为我们最大生成树的一部分。然而,根据问题,当我们选择第一条边(A.B)时,我们应该删除A或B中的一个,这意味着一旦我们选择(A,B),我们现在只能选择(A,C)(即删除B,所以不能选择(B,D)或将来的任何与B连接的边)或选择(B,D)(即删除A,所以不能选择(A,C)或将来的任何与A连接的边),但理论上不能同时选择(A,C)和(B,D)。
为了澄清,选择任何一条边作为最大生成树的一部分意味着我们在答案中添加了A^B(因此必须删除A或B)。根据上述情况,不清楚最大生成树在这里是如何工作的。请你能解释一下吗?
英文:
There is a class of questions, that requires us to randomly select two elements from a given array, perform an operation on those elements, add the result to an "answer" and then drop any one of the elements and put the other element back in the array.
Most recently, I've come across this - Given an array, pick two elements a and b, add (a XOR b) to "answer". Then drop either a or b and put back the other in the array. Repeat these operations until there's only one element left in the array. What is the maximum value of "answer" that you can get (See Q here)
Sample I/O:-
arr = [3,2,1]
Output = 5;
Possible operations - 2 ^ 1 (= 3) -> Drop 2 -> 3 ^ 1 (= 2) -> Drop 3. Sum up 3+2=5 which is the final value of answer.
Now, given the question, I can write a recursion+backtracking code to handle this, but of course the time complexity is of the order of N!. I've been reading up on this, and it seems that it can be handled via Minimum/Maximum Spanning Tree. I don't understand why Maximum Spanning Tree works here at all. I put a comment on the question thread. Pasting part of comment below.
Running through a scenario - When following Prim's algorithm for MaxST, assume the first edge we choose is (A,B). Now the next edge we choose should be an edge that is attached to A or B and should have max weight. Let's say we choose (A,C). Similarly, now we need to choose an edge that is attached to A or B or C and have max weight. It could be an edge (B,D). Now in this circumstance, we have chosen (A,C) and (B,D) both to be parts of our MaxST. However, per the question, when we chose the first edge (A.B), we should have dropped either A or B from the array, meaning that once we chose (A,B) we can now either only choose (A,C) {i.e. drop B so can't choose (B,D) or any other edge connected to B in any step in the future} or choose (B,D) {i.e. drop A so can't choose (A,C) or any other edge connected to A in any step in the future}, but ideally can't choose both (A,C) and (B,D)
Just to clarify, choosing any edge to be part of our MaxST means that we're taking the XOR of those two elements in the answer, i.e. if we choose an edge (A,B), it means that we're adding A^B to the answer (and hence must drop either A or B). Given the above, it doesn't make sense how MaxST works here. Could you please explain?
答案1
得分: 1
不考虑最小生成树中节点选择的顺序。您始终可以重新排列边以满足问题的约束条件。
考虑一个具有节点 A, B, C, D, E
的图。最小生成树中的边为 A-B, A-C, B-D, D-E
。根据您的情况,要么在选择了 A-B
后应该放弃 A
要么放弃 B
,以便它们不会在后续的边中被同时选择。然而,如果您重新排列边的顺序为 D-E, B-D, A-B, A-C
,您会发现这个顺序可以用于从数组中选择元素。元素依次为 E, D, B, A, C
被选择。
英文:
Don't consider the order of picking nodes in the MST. You can always rearrange the edges to meet the constraints of the question.
Consider a graph with nodes A, B, C, D, E
. Let the edges in the MST be A-B, A-C, B-D, D-E
. According to your scenario, either A
or B
should be dropped after picking A-B
so that both don't get picked in later edges. However, if you rearrange the order of edges as D-E, B-D, A-B, A-C
, you can see that this order can be used for picking elements from the array. Elements E, D, B, A, C
get selected at each stage respectively.
答案2
得分: 1
为了了解如何使用最小生成树(MST)来解决这些问题,您需要理解两个重要点:
-
每个有效的成对序列都会创建一棵生成树。您可以通过归纳法来证明这一点,使用以下方法:
- 在每次选择之前和之后,每个剩余的顶点都连接到一棵树;
- 对于每棵树,只有一个剩余顶点。
-
对于每棵生成树,都存在一个可以创建该树的成对序列。通过反复选择一个叶子节点及其父节点,然后丢弃叶子节点,可以轻松构造出这个序列。当父节点的所有子节点都被丢弃时,父节点将变成一个叶子节点。
因此,对于每个序列都存在一棵生成树,而对于每棵生成树都存在一个序列。要解决问题,首先找到最大生成树,然后选择任何可以创建它的序列,按照第二点的方法进行选择。
英文:
In order to see how you can use MST to solve these problems, you need to understand two things:
-
Every valid sequence of pairs creates a spanning tree. You can prove this inductively, using:
- before and after each selection, every remaining vertex is connected to a tree; and
- for each tree, there is only one remaining vertex.
-
For each spanning tree, there is a sequence of pairs that creates that tree. It's easy to construct this sequence by repeatedly choosing a leaf and its parent, and then dropping the leaf. When a parent's children are all dropped, the parent will become a leaf itself.
So for every sequence there is a spanning tree, and for every spanning tree there is a sequence. To solve the problem, find the maximum spanning tree, and then choose any sequence that creates it, following (2).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论