algorithm - Codility EvenSums 游戏

标签 algorithm

我正在尝试完成 codility 挑战以提高我的编程技能。挑战详情为here .

我也会在这里复制问题陈述。

Even sums is a game for two players. Players are given a sequence of N positive integers and take turns alternately. In each turn, a player chooses a non-empty slice (a subsequence of consecutive elements) such that the sum of values in this slice is even, then removes the slice and concatenates the remaining parts of the sequence. The first player who is unable to make a legal move loses the game.

You play this game against your opponent and you want to know if you can win, assuming both you and your opponent play optimally. You move first.

Write a function:

string solution(vector< int>& A);

that, given a zero-indexed array A consisting of N integers, returns a string of format "X,Y" where X and Y are, respectively, the first and last positions (inclusive) of the slice that you should remove on your first move in order to win, assuming you have a winning strategy. If there is more than one such winning slice, the function should return the one with the smallest value of X. If there is more than one slice with the smallest value of X, the function should return the shortest. If you do not have a winning strategy, the function should return "NO SOLUTION".

For example, given the following array:

A[0] = 4 A[1] = 5 A[2] = 3 A[3] = 7 A[4] = 2 the function should return "1,2". After removing a slice from positions 1 to 2 (with an even sum of 5 + 3 = 8), the remaining array is [4, 7, 2]. Then the opponent will be able to remove the first element (of even sum 4) or the last element (of even sum 2). Afterwards you can make a move that leaves the array containing just [7], so your opponent will not have a legal move and will lose. One of possible games is shown on the following picture:

https://codility-frontend-prod.s3.amazonaws.com/media/task_img/even_sums_game/media/auto/tikz242689cc8162bed50db48168cf844337.png

Note that removing slice "2,3" (with an even sum of 3 + 7 = 10) is also a winning move, but slice "1,2" has a smaller value of X.

For the following array:

A[0] = 2 A[ 1 ] = 5 A[2] = 4 the function should return "NO SOLUTION", since there is no strategy that guarantees you a win.

Assume that:

N is an integer within the range [1..100,000]; each element of array A is an integer within the range [1..1,000,000,000]. Complexity:

expected worst-case time complexity is O(N); expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). Elements of input arrays can be modified.



首先,我对问题陈述有一个误解,为什么在给定的示例中,第一个玩家在第一步中不能将 4 作为偶数范围,所以该函数应该返回 0,0 而不是 1,2。

其次,我解决问题的想法是计算输入数组中偶数范围的数量,如果计数是偶数,那么玩家一获胜,我应该找到玩家可以采取的第一个偶数范围。

但是我不能在规定的时间内解决,不能再考​​了。

这是我尝试的代码:
    string solution(vector<int> &A) {
    int num_even_ranges = 0;
    int sum = 0;
    for (int i = 0; i < A.size(); i++)
    {
        sum += (A[i]%2);
        if (sum % 2 == 0 || A[i]%2 == 0)
        {
            num_even_ranges++;
            sum = 0;
        }
    }
    if (num_even_ranges % 2 == 0)
        return "NO SOLUTION";
    sum = 0;
    int start = 0;
    for (int i = start; i < A.size(); i++)
    {
        sum += A[i];
        if (sum % 2 == 0 && i > start)
        {
            return to_string(start) + "," + to_string(i);
        }

        if (i + 1 < A.size())
        {
            if (A[i] % 2 == 0 && A[i+1] % 2 == 1)
            {
                sum = 0;
                start = i + 1;
            }
        }
    }
}

如果不是这个有趣问题的正确解决方案,我对问题的解决方案是否正确。

最佳答案

关于你的第一个问题,如果第一个玩家拿了 4 (0,0),剩下的数组是:5,3,7,2。然后对手可以拿走 3,7,2,第一个玩家会输,因为剩下的 5 是奇数。所以这不是有效的解决方案。

关于algorithm - Codility EvenSums 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36782314/

相关文章:

algorithm - 基于物联网时间戳算法的理论模型

c++ - 如何高效地将元素插入到数组的任意位置?

c# - 文本选择交集算法

java - 这个简单程序的运行时间——时间复杂度

algorithm - 寻找产品评论数据集

algorithm - 优化快速排序

c++ - Codility 测试中的算术溢出

algorithm - 我可以在 Prolog 的 catch 调用中共享变量吗?

python - 对majorclust算法感到困惑

java - 该字符串需要多少份?