最小化步骤,以在一个糖果分布在圆最小化、糖果、步骤

2023-09-11 22:51:40 作者:据为己有

N 的孩子在一个圆圈。他们每个人都有一些糖果(可能是阴性,阳性或零)。他们可以在一台糖果给的时间到他们的邻居。最终的结果是,他们都应该有最基本的步骤零糖果。

There are n children in a circle. Each of them has some candies (may be negative, positive or zero). They can give at a time a single candy to their neighbors. The end result is that they all should have zero candies in minimum steps.

假设我们有4个孩子与( - 4,-2,4,2)糖果则该序列将

Suppose we have 4 children with (-4, -2, 4, 2) candies then the sequence will be

( - 3,-2,4,1) ( - 2,-2,4,0) ( - 2,-1,3,0) ( - 2,0,2,0) ( - 2,1,1,0) ( - 2,2,0,0) ( - 1,1,0,0) (0,0,0,0)

这是一个可能的答案,我一定要找到的最小步数。

This is one possible answer, I have to find minimum number of steps.

回路1:发现邻居是否具有正面糖果,然后把它与负糖果邻居直到糖果的数目等于零,并添加给予总和糖果的数目

Loop 1: find if a neighbor has positive candies,then give it to the neighbor with negative candies till number of candies is equal to zero and add the number of candies given to sum.

回路2:发现如果一个邻居的邻居有积极的糖果,然后交给负糖果邻居到糖果的数量等于零,并添加2(给予总结糖果的数量)

Loop 2: find if a neighbors' neighbour has positive candies, then give it to the neighbor with negative candies till number of candies is equal to zero and add 2 (the number of candies given to sum).

等等。

在我的解决方案的复杂性,造成了颞叶癫痫。我能做些什么来降低复杂性?

The complexity of my solution is causing a TLE. What can I do to reduce the complexity?

推荐答案

我不认为你需要循环轮的细节。写在每个地方为X1,X2,X3,X4糖果的数目。假设X1接收ķ从其左糖果(即,为4个)。在此之后它具有X1 + K糖果,所以它必须通过此其右侧。然后X2将具有X1 + X2 + K糖果,所以它必须通过此其右侧。然后X3将具有X1 + X2 + X3 + K糖果,所以它必须通过此至X4。我们知道X4传递ķ糖果,和这个检查(假设X1 + X2 + X3 + X4 = 0,如果它不存在没有解决方案)。

I don't think you need to loop round in detail. Write the number of candies in each place as X1, X2, X3, X4. Suppose that X1 receives k candies from its left (that is, for X4). After this it has X1+k candies, so it must pass this to its right. Then X2 will have X1 + X2 + k candies, so it must pass this to its right. X3 will then have X1 + X2 + X3 + k candies, so it must pass this to X4. We know X4 passed k candies, and this checks (assuming that X1 + X2 + X3 + X4 = 0 and if it doesn't there is no solution).

这需要| K | + | X 1 + K | + | X1 + X2 + K | + | X1 + X2 + X3 + K |步骤,所以如果我们想ķ我们知道有多少要采取的步骤。什么是k的最大价值?如果我们增加ķ我们增加的总和,如果有更多+已经术语X1 + X2 + ... k和是否有更多-ve方面降低。因此,k的最佳值是在其中的条款中正好有一半| K |,| X 1 + K | ..是+ ve和正好一半-ve,因为如果不是这样,我们可以增加或减少k以使案件事情更好 - k以选择的值是 - 的0位数,X1,X1 + X2,X1 + X2 + X3

This takes |k| + |X1 + k| + |X1 + X2 + k| + |X1 + X2 + X3 + k| steps, so if we guess k we know how many steps to take. What is the best value of k? If we increase k we increase the sum if there are more +ve terms X1 + X2 + ... k, and decrease if there are more -ve terms. So the best value of k is one in which exactly half of the terms |k|, |X1 + k|.. are +ve and exactly half -ve because if this is not the case we can either increase or decrease k to make things better - the value of k to choose is - the median of 0, X1, X1 + X2, X1 + X2 + X3.

我已经说过这为N = 4的情况下你的例子,但我希望你能制定出总体n个答案来源于此。

I have stated this for the n=4 case of your example but I hope that you can work out the answer for general n from this.