Editorial for COCI '06 Contest 5 #5 Ivana


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

The task description doesn't explicitly state Zvonko's strategy, just that he tries to win when he can, i.e. looks for a sequence of moves that guarantees victory. One such strategy is to try to win with the largest score difference possible.

Zvonko does not know how well Ivana plays so part of his strategy is to assume she plays optimally as well, since he wants to be absolutely certain that he won't lose.

After Ivana's first move, the circle falls apart and we're left with a chain of numbers. The players alternate in taking numbers from the two ends of the chain (they get to pick which end to take off).

Suppose Ivana has made her first move and a chain of N-1 numbers is left. Let f(a, b) the largest score difference that the player currently up can achieve (this number can be negative if the player always loses), if what is left of the chain are numbers originally at indices a to b.

The current player either takes the number at index a and leaves the opponent with the subproblem f(a+1, b), or he takes the number at index b and the opponent is left with the subproblem f(a, b-1). The function f can be efficiently calculated for all pairs of indices using dynamic programming.

The following identities hold:

  • f(i, i) = 1, if the number at index i is odd, or 0 if it is even
  • f(a, b) = \max\{ f(a, a) - f(a+1, b), f(b, b) - f(a, b-1) \}

The solution has Ivana try each of the N numbers as her first move and use the function f to calculate if she has a winning strategy. The function f can be calculated at once for all the chains, because the chains share many subproblems.


Comments

There are no comments at the moment.