Editorial for COCI '23 Contest 2 #4 Kuglice
Submitting an official solution before solving the problem yourself is a bannable offence.
Prepared by: Fran Babić
In the first subtask, it holds that there are at most two different colors in the sequence. The first ball taken will definitely score point (for Marin), so the only possible outcomes are 1:0
, 1:1
, and 2:0
. Solving the first subtask boils down to decomposing it into 3 different cases:
- All elements in the sequence are the same, i.e., the number of different colors is one.
In this case, the game result will be1:0
. - The element at the left end of the sequence is different from the one at the right end, i.e., .
Whichever element Marin chooses in the first move, Josip can take the one from the other end of the sequence, which is different from the one Marin took. We can see that in this case, the game result will be1:1
. - Not all elements in the sequence are the same, and the elements at the ends of the sequence are the same ().
It is easy to see that the answer will depend on the parity of the longest prefix and suffix of equal elements.
Let and , i.e., the index of the first occurrence and the index of the last occurrence of a color in the sequence. Consider some game state where is the index of the leftmost ball in the box, and is the index of the rightmost ball in the box. For a complete solution, we need the following observation:
Observation. For the current game state , a ball of color has been taken in some previous moves if and only if or .
Using this, we can write a function that returns the difference in points between the first and second player in that game state. The value returned by depends on the values of and . The complexity of such an algorithm is , but by memoizing the function, we reduce the complexity to , which is sufficient to score all of the points.
Comments