D - Stones Editorial by en_translator
Intended wrong answer
You cannot solve this problem with a greedy approach of “remove as many stones as they can in each turn.” The counterexample is:
10 3 1 3 4Let us check that this is indeed the counterexample of the greedy algorithm. If Takahashi uses greedy approach, an example of the progression of the game is as follows. (Here, Takahashi greedily remove stones, but Aoki doesn’t, so this is merely an example)
- Takahashi removes stones.
- Aoki removes stones.
- Takahashi removes stones.
- Aoki removes stones.
In this example, Takahashi is forced to remove stones due to Aoki’s move. However, if Takahashi removes stones, no matter how many stones Aoki remove in the next turn, Takahashi can remove or more stones in the next turn, so he can remove at least stones.
Thus, depending on and , this problem may require a complex strategy. Therefore, we came up with using exhaustive-search algorithm.
Intended answer
This problem can be solved with the following DP (Dynamic Programming):
the number of stones that the first player can take if the game starts with a pile with stones.
The transition is:
The semantics of this equation is: if the first player removes stones, he will end up with removing (the number of stones that the second player can remove if the game starts with a pile with ) stones, so the desired value is the maximum over all .
This DP has states, each transition of which is processed in an time, so we can find in a total of time.
posted:
last update: