Cow Run
Time limit1sMemory limit128 MB
Given N rounds, each with 8 cards, pick for each round whether Farmer John keeps the top or bottom 4 so that the cows always finish within distance K of the start, regardless of Bessie's choices.
- Level
Hard8 of 10
- Topics
- Game theory, Dynamic programming, Brute force, Math
- Solved
- No attempts yet
Problem
Farmer John and Bessie have invented a new exercise game for the cows. The cows run on a circular track of length (), and they all start from the same position. The game is played over () rounds using a deck of cards; each card shows a number ().
At the start of each round Farmer John takes the top cards as a separate pile and keeps either the top 4 or the bottom 4 of them. Bessie then keeps either the top 2 or the bottom 2 of those cards. This leaves an ordered pair: a top card and a bottom card .
Farmer John first announces , and the cows run a distance of , where is the total distance the cows have run so far. Bessie then announces , and the cows run an additional distance of . Because the track is circular, only the position taken modulo matters.
Farmer John worries the cows will be too tired to get home if they finish too far from the start. He decides they can get home only if their final distance from the starting position (measured as the shorter arc around the circle) is at most ().
It is guaranteed that, by playing correctly, Farmer John can always bring the cows home no matter what Bessie does. For each round you must decide which half Farmer John should keep so that, regardless of Bessie's current and future choices, the cows can still finish within distance of the start. Bessie then makes the move given in the input and you continue to the next round. Even though Bessie's moves are given to you, the moves you pick for Farmer John must work no matter what Bessie does (as if Farmer John does not know Bessie's choices in advance).
Input
- Line : Three space-separated integers , , .
- Line : A string of characters. If the -th character is
T, Bessie keeps the top cards in round ; if it isB, she keeps the bottom cards. - Lines : Line contains eight integers — the cards used in round , listed from top to bottom.
Output
- Line : A string of characters. The -th character is
Tif Farmer John should keep the top cards in round , orBif he should keep the bottom cards. If several sequences of choices bring the cows home, output the lexicographically smallest one (the alphabetically smallest string, whereBprecedesT).
Hint
Notes
The cows can return home only if they finish within distance of the starting position; in the sample , so they must finish exactly where they started. Note that Farmer John does not know Bessie's choices in advance — if he did, he could simply keep the bottom half every round.