2019 Dec Silver Problem 1 MooBuzz

Official Problem StatementEdit

MooBuzz

Problem StatementEdit

MooBuzz is a game played with a deck of N cards, numbered 1 through N. Two players take turns. On each turn, a player must choose a card from the deck and discard it. The player who discards the last card is the winner.

SolutionEdit

The solution is to use a greedy approach. On each turn, the player should choose the card with the largest number that has not yet been discarded. This ensures that the player will discard the last card, and thus win the game.

In C++, this can be implemented as follows:

// N is the number of cards in the deck
vector<bool> used(N+1, false);

// Player's turn
int max_card = 0;
for (int i = 1; i <= N; i++) {
    if (!used[i] && i > max_card) {
        max_card = i;
    }
}
used[max_card] = true;