2019 Dec Silver Problem 1 MooBuzz

From Wiki
Revision as of 23:01, 11 June 2023 by Admin (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Official Problem Statement[edit]

MooBuzz

Problem Statement[edit]

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.

Solution[edit]

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;