Official

D - Pocky Game Editorial by evima


Let us use DP. We define \(left[i][j]\) as the answer to the following question:

  • Let the two players play the game using the segment \([i,j]\), where we can freely change the initial value of \(a_i\). FirstLeft goes first. What is the minimum value of \(a_i\) under which FirstLeft can win?

Similarly, we define \(right[i][j]\) as the answer to the following question:

  • Let the two players play the game using the segment \([i,j]\), where we can freely change the initial value of \(a_j\). SecondRight goes first. What is the minimum value of \(a_j\) under which SecondRight can win?

Then, we can see that we can find the values \(left[i][j]\) and \(right[i][j]\) from the values \(left[i][j-1]\) and \(right[i+1][j]\). We only need to consider taking one stone and taking all stones from a pile as the moves that the players actually use, which makes us able to compute the transition.

Thus, we can solve the problem in \(O(N^2)\) time.

posted:
last update: