D - Choose Your Role Editorial by evima
Another Solution
This shares part of the reasoning with the main solution.
This problem can be interpreted as follows.
- There is a complete directed graph $G$ with $N$ vertices.
- The first player chooses up to $K$ edges sharing the same start vertex and removes them from $G$.
- The second player chooses up to $K$ edges with pairwise distinct start vertices and removes them from $G$.
- The player who first makes $G$ have no edges wins.
As the second player's goal, we want the existence of edge $u \rightarrow v$ to be equivalent to the existence of edge $v \rightarrow u$ after the second player's move.
This can be rephrased as: we want the sequence of the numbers of stones remaining in the piles after the second player's move to be a possible degree sequence of a simple undirected graph with $N$ vertices.
One strategy to achieve this is as follows.
- If the first player just took $k$ stones, choose $k$ piles with the most remaining stones, and remove a stone from each of them.
Let us prove the correctness. By the above discussion, it is possible to make it a possible degree sequence after the second player's move. Let $(d_1, d_2, \cdots, d_N)$ be the degree sequence in that case, and let $(d'_1, d'_2, \cdots, d'_N)$ be the sequence when choosing $k$ piles with the most remaining stones. Assume both are in ascending order. It suffices to show that $d'$ can also be a degree sequence.
The sums of $d_i$ and $d'_i$ are the same. Thus, it can be shown that $d'$ is a sequence obtained from $d$ by repeating the following change some number of times.
- Choose a pair of integers $(i, j)$ satisfying $1 \leq i < j \leq N$, and set $d_i \leftarrow d_i + 1, d_j \leftarrow d_j - 1$. However, the sequence must remain in ascending order after the change.
If the sequence before the change is a degree sequence, it can be confirmed by the Erdos-Gallai theorem that the sequence after the change is also a degree sequence. From this, it is shown that $d'$ is also a degree sequence, and the correctness is established.
There is an article on the Erdos-Gallai theorem in Algorithm Lectures. Also, to understand this claim intuitively, it is enough to grasp the point that "bringing the degree values closer to each other makes it more likely to be a degree sequence."