Memory
InterviewTime limit1sMemory limit256 MB
Given R times C face-down cards forming pairs, find the best-case and worst-case number of actions a perfect-memory player needs to clear the board.
- Level
Medium6 of 10
- Topics
- Game theory, Math, Dynamic programming, Implementation
- Solved
- No attempts yet
Problem
Moraeduji carries cards everywhere. In that deck, every pattern is drawn on exactly two cards. He carries them so that he can play a matching game when he is alone. The game runs like this.
-
Shuffle the cards well, then lay them out face down in rows and columns so that no pattern is visible.
-
Repeat the following action until no card is left.
- Pick one card, turn it over, and look at its pattern.
- Pick one more card among the rest, turn it over, and look at its pattern.
- If the two patterns are the same, remove both cards from the game. If they differ, put both back face down where they were.
-
Win the game!
Moraeduji remembers the pattern and the position of every card he has ever turned over, and he plans his strategy so that the number of actions in the worst case is as small as possible. Under that strategy, find the number of actions when his luck is best and the number of actions when his luck is worst.
Input
The first line contains two integers and separated by a single space. (, and is even.)
Output
Print the minimum number of actions and the maximum number of actions needed to win the game on one line, separated by a space.
Hint
When and there are four cards and two pairs. If the first action turns over two cards with the same pattern, remove them, then remove the remaining two with the second action, so two actions finish the game. If the first action turns over two different patterns, the second action checks one card that has not been turned over yet and also turns over the card of the same pattern among the two seen in the first action, removing that pair, and the third action removes the last two cards. So the minimum is two and the maximum is three.