B - All Minus 解説 by evima
Let \(S=\lbrace s_1,s_2,\dots,s_k \rbrace\) denote the multiset of non-negative integers written on the blackboard.
Given \(S\), the winning player can be determined as follows.
- As both players repeatedly take turns, the player who first takes their turn when one of the following conditions is satisfied wins.
- \(S = \lbrace 0 \rbrace\)
- \(m \ge 2\)
- \(S\) contains two or more \(0\)s.
Note that the above winning player is uniquely determined because, as long as none of the conditions is satisfied, the operation is unique (due to the latter two conditions).
To prove the above fact, it suffices to show that the first player wins when the game starts from an \(S\) satisfying any of the above conditions.
The case \(S = \lbrace 0 \rbrace\) is trivial.
When \(m \ge 2\), we case-split based on the winning player when the game starts from the board \(S'=\lbrace s_1-m,s_2-m,\dots,s_k-m \rbrace\) obtained by choosing \(x = m\).
- When the first player wins starting from $S'$
- When the second player wins starting from $S'$
The first player should choose $x = m-1$. This operation makes $m = 1$, so the only operation available to the second player on the next turn is to choose $x = 1$. As a result, $S'$ comes back to the first player, so the first player wins.
The first player should choose $x=m$. They can pass $S'$ to the second player, so the first player wins.
When \(S\) contains two or more \(0\)s, let \(c\) \((\ge 2)\) be the number of \(0\)s in \(S\), and we case-split based on the winning player when the game starts from the board \(S'\) obtained by erasing all \(c\) zeros.
- When the first player wins starting from $S'$
- When the second player wins starting from $S'$
The first player should erase $c-1$ zeros. This operation makes $S$ contain exactly one $0$, so the only operation available to the second player on the next turn is to erase that $0$. As a result, $S'$ comes back to the first player, so the first player wins.
The first player should erase all $c$ zeros. They can pass $S'$ to the second player, so the first player wins.
Thus, we have proved that the first player wins when one of the conditions is satisfied.
To determine the winning player given \(A\), we simulate the operations until the board satisfies any of the above conditions. Since the only possible operations when no condition is satisfied are subtracting \(1\) from everything or erasing exactly one \(0\), we can simulate by sorting \(A\) and tracking the total amount subtracted from everything so far.
By implementing the above, this problem can be solved in \(\mathrm{O}(N \log N)\) time.
投稿日時:
最終更新: