我该如何最大限度地发挥在本场比赛的总和?我该、总和、本场比赛、限度

2023-09-11 05:00:27 作者:剑影刀光

所以,有人问我这个问题,在接受采访时:

So, I was asked this question in an interview:

有两个朋友玩一个游戏中,他们选择了一些含有 N 正数的数组。这两个朋友选择一次一个号码,无论是玩家最佳的玩游戏。而且你必须找出什么是最高金额(即正在挑选号码)比赛结束后,你才可能获得。给予后,我给了回答同样的问题,没有约束的制约因素:

There are two friends playing a game in which they select a number from an array containing n positive numbers. Both friends select one number at a time, and both the players play the game optimally. And you have to find out what's the maximum sum (of numbers that are being selected) that you could obtain after the game ends. The constraints that were given after I gave the answer to the same question without constraints were:

在双方球员的第一招,他们可以选择任何号码。 除了第一移动,它们只能选择哪个是给定的阵列中邻近于previous号和还没有被选中的第一个和第二个玩家,直到这一刻在游戏中的号码。 (澄清为编辑)

如果一个球员是不是能够使一招,他/她停止播放。而本场比赛结束时,双方球员无法做出的举动。

And if a player is not able to make a move, he/she stops playing. And the game ends when both players cannot make a move.

现在,我给的解决方案是:

Now, the solution that I gave was:

请包含值以及输入阵列中的值的索引的结构。 请在previous结构的阵列和存储在该数组第一步的值。 排序此数组中的值的基础上,非递减的顺序。 在开始选择在一个贪婪的方式值并打印最大值。

他们的伪code,虽然我可以$ C C $它也期待更多。但是,面试官说,这将失败的一些情况。我想了很多关于这情况下,这会失败,但找不到任何。因此,我需要在这个问题上的帮助。

They were looking more for a pseudo-code though I can code it too. But, the interviewer said this will fail for some cases. I thought a lot on which cases this will fail but couldn't find any. Therefore, I need help in this question.

另外,如果可能的话,请包括我能做些什么来改善这样的伪code。

Also, if possible, please include a pseudo-code of what I can do to improve this.

编辑:我想我是不是我​​的问题不够清晰,特别是在第2个点。面试官的意思是:

I guess I wasn't clear enough in my question, specifically in 2nd point. What the interviewer meant was:

如果这不是球员的第一招,他必须选择一个数字是相邻的,他已经在previous移动所选号码中的一个。

If it is not the first move of the player, he has to choose a number which is adjacent to one of the number he already selected in previous moves.

此外,是的,无论是玩家最佳玩游戏,他们选择数字轮流转。

Also, yes, both the players play the game optimally and they choose numbers turn by turn.

EDIT2:那么,同样有人问我的朋友,但它被修改了一点。而不是一个数组,他被赋予了一个曲线图。所以,就像在我的情况,我只能选择那些靠近我的previously选择指数的指数,他被赋予了一个无向图(邻接表作为输入),他可以选择只在特定的移动的顶点这是直接连接到任何pviously所选顶点$ P $的。

So, the same question was asked from my friend but it was modified a little. Instead of an array, what he was given was a graph. So, like in my case, I can select only the indices that are adjacent to my previously selected indices, what he was given was an undirected graph (adjacency list as input) and he could select only those vertices in a particular move which are directly connected to any of the previously selected vertex.

有关,例如: 比方说,正整数的个数为3的整数的价值是 4 2 4 而且,如果我的名字被 A B的正整数 C ,然后,

For eg: Let's say the number of positive integers is 3. The value of those integers are 4, 2, 4 and also, if I name the positive integers by A, B and C, then,

A - B
B - C

以上是例子,我的朋友给出的答案上面会 6 。你可以点我在正确的方向如何我可以用这个了么?谢谢!

The above was the example that my friend was given and the answer to the above would be 6. Can you just point me in the right direction as to how I can begin with this? Thanks!

推荐答案

请注意,如果你把在索引中的第一招 X ,如果你的对手发挥最佳,他们的第一个此举将不得不在指数 X-1 X + 1 。否则,他们将有,他们可以挑,但没有的元素。要了解这一点,考虑两个不相邻的出发点:

Notice that if you make the first move at index x, if your opponent plays optimally, their first move will have to be at index x-1 or x+1. Otherwise, they will have elements that they could have picked but didn't. To see this, consider two non-adjacent starting points:

-------y-------------x-------

最后,他们都将采取从数组的元素,并最终喜欢的东西:

Eventually they will both take elements from the array and end up with something like:

yyyyyyyyyyyyyyxxxxxxxxxxxxxxx

所以,你可以重新定位的出发点,中间 YX ,得到了同样的解决方案。

So you can relocate the starting points to the middle yx, obtaining the same solution.

所以,假设你先在 X 移动。让:

So, assume you move first at x. Let:

s_left_x = a[0] + ... + a[x]
s_right_x = a[x] + ... a[n - 1]

s_left_y = a[0] + ... + a[x - 1]
s_right_y = a[x + 1] + ... + a[n - 1]

让我们假设你想赢得比赛:比你的对手在最后一个更大的总和。如果你的对手挑选 X + 1 ,你想 s_left_x> s_right_y ,如果你的对手挑选 X - 1 ,你想 s_right_x> s_left_y 。这是理想的,为了赢得。它并不总是能够获胜,虽然,你的问题不问怎么赢,而是如何获得最大的一笔。

Let's say you want to win the game: have a larger sum than your opponent at the end. If your opponent picks x + 1, you want s_left_x > s_right_y, and if your opponent picks x - 1, you want s_right_x > s_left_y. This is ideally, in order to win. It's not always possible to win though, and your question doesn't ask how to win, but rather how to get the largest sum.

因为你的对手会发挥最佳,他会强迫你进入最糟糕的情况。因此,对于每个 X 作为你的第一个举动,最好你能做的就是分(s_left_x,s_right_x)。挑选这前pression最大限度地为每个索引 X ,您可以在找到O(1)对于一些precomputations后各指标。

Since your opponent will play optimally, he will force you into the worst case. So for each x as your first move, the best you can do is min(s_left_x, s_right_x). Pick the maximum of this expression for each index x, which you can find in O(1) for each index after some precomputations.