#P11207. Minimum Operations to Ensure Her Victory
Minimum Operations to Ensure Her Victory
Minimum Operations to Ensure Her Victory
You and she are playing a card game. Both of you have n cards arranged in order. Each card has one of three colors: pink, purple, or white.
The game proceeds as follows. Starting with her, you and she take turns playing one card from the front of your respective decks. The played card is added to a pile. Let m be the number of cards in the pile at any moment. If after a move the counts of the three colors in the pile are all equal, i.e. if \[ \text{count}(\text{pink}) = \text{count}(\text{purple}) = \text{count}(\text{white}) = \frac{m}{3}, \] then the player who played the last card wins and the game ends immediately. If all cards are played without triggering such a win, the game is a draw.
Before the game starts, you are allowed to perform a number of operations. In one operation, you may change the color of any one card (from either deck) to any of the three colors. Your goal is to make her win the game, and you want to know the minimum number of operations required to guarantee that outcome. It can be proved that there is always at least one way to achieve her victory.
Note: Since the game stops immediately at the first winning move, you only need to force a winning condition on one of her turns. A simple strategy is to force a win on her first opportunity. Observe that the first possible moment for a win is after 3 moves (i.e. when m=3), and since she plays on odd-numbered moves, those 3 moves are: her first card, your first card, and her second card. Thus, to guarantee her win, you can reassign these three cards so that they are exactly one of each color (i.e. {pink, purple, white}) in some order. Since there is no possibility of meeting the winning condition in the first or second move, the game will end immediately at move 3 with her win.
Formally, let the original sequences be:
- Her cards: H1, H2, ..., Hn
- Your cards: Y1, Y2, ..., Yn
The moves are played in the following order:
- Move 1: H1 (by her)
- Move 2: Y1 (by you)
- Move 3: H2 (by her)
You need to choose a permutation of {pink, purple, white} to assign to these three moves such that the number of modifications (i.e. the number of cards whose color is changed) is minimized. The answer is the minimal number of operations needed.
inputFormat
The input consists of three lines:
- The first line contains an integer n (n ≥ 2) — the number of cards for each player.
- The second line contains n space-separated strings, each being either
pink
,purple
, orwhite
, representing the colors of her cards in order. - The third line contains n space-separated strings, in the same format as above, representing the colors of your cards in order.
outputFormat
Output a single integer — the minimum number of operations required to ensure that she wins the game.
sample
2
pink purple
white purple
0