Kuglice (Christmas Ornaments)
InterviewTime limit0.5sMemory limit1024 MB
Two players alternately draw ornaments from either end of a box, scoring a point for each color drawn for the first time. Find both final scores under optimal play.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Game theory, Array
- Solved
- No attempts yet
Problem
Christmas time is approaching, the most beautiful time of the year. Marin and Josip have returned from their Christmas shopping and started decorating their Christmas tree.
They bought Christmas ornaments arranged next to each other in an elongated box, and the -th ornament has the color . The box is open on both sides, so ornaments can be taken out from both the left and the right end. The box is transparent, so Marin and Josip can see the color of each ornament.

The illustration shows the initial state of the box. On his first move, Marin can draw either an ornament of color from the left end or an ornament of color from the right end.
Marin and Josip take turns, and Marin starts. The player in turn draws an ornament from either the left or the right end of the box and places it on the tree. If the color has not been drawn yet, the player scores a point. The game ends when the last ornament is drawn. The player with more points wins, so both players try to maximize their own points. Both play optimally. Print the result at the end of the game.
Input
The first line contains an integer (), the number of ornaments in the box.
The second line contains integers (), the colors of the ornaments.
Output
Print one line with two numbers connected by the character :, Marin's score and Josip's score.
Hint
In the first move, Marin takes an ornament of color from the left end and scores a point. Josip takes an ornament of color from the right end, but he scores nothing because color was already drawn. Marin takes another ornament of color from the left end and scores nothing. Josip takes an ornament of color from the left end, the first one of that color, so he scores a point. Marin takes the last ornament (color ) from the left end, scores nothing, and the game ends. Marin has 1 point and Josip has 1 point, so the result is 1:1.