Cow Run

Time limit1sMemory limit128 MB

Summary
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 MM (2≤M≤1092 \le M \le 10^9), and they all start from the same position. The game is played over NN (1≤N≤141 \le N \le 14) rounds using a deck of 8N8N cards; each card shows a number XiX_i (0≤Xi<M0 \le X_i < M).

At the start of each round Farmer John takes the top 88 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 44 cards. This leaves an ordered pair: a top card XtopX_{top} and a bottom card XbottomX_{bottom}.

Farmer John first announces XtopX_{top}, and the cows run a distance of R⋅XtopR \cdot X_{top}, where RR is the total distance the cows have run so far. Bessie then announces XbottomX_{bottom}, and the cows run an additional distance of XbottomX_{bottom}. Because the track is circular, only the position taken modulo MM 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 KK (0≤K≤⌊M/2⌋0 \le K \le \lfloor M/2 \rfloor).

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 KK 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 11: Three space-separated integers NN, MM, KK.
  • Line 22: A string of NN characters. If the ii-th character is T, Bessie keeps the top 22 cards in round ii; if it is B, she keeps the bottom 22 cards.
  • Lines 3…N+23 \ldots N+2: Line i+2i+2 contains eight integers — the 88 cards used in round ii, listed from top to bottom.

Output

  • Line 11: A string of NN characters. The ii-th character is T if Farmer John should keep the top 44 cards in round ii, or B if he should keep the bottom 44 cards. If several sequences of choices bring the cows home, output the lexicographically smallest one (the alphabetically smallest string, where B precedes T).

Hint

Notes

The cows can return home only if they finish within distance KK of the starting position; in the sample K=0K = 0, 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.

Examples4

  1. Example 1

    Input
    2 2 0
    TT
    1 0 0 0 0 0 0 1
    0 1 1 1 0 0 1 0
    
    Expected output
    TB
    
  2. Example 2

    Input
    1 3 0
    T
    5 0 7 0 2 1 3 1
    
    Expected output
    T
    
  3. Example 3

    Input
    1 3 0
    B
    2 1 3 1 5 0 7 0
    
    Expected output
    B
    
  4. Example 4

    Input
    1 10 2
    B
    3 1 4 2 7 8 1 9
    
    Expected output
    B