StarCowraft

Interview

Time limit1sMemory limit128 MB

Summary
Given test battle outcomes and the constraint that no unit strength exceeds 100 times another, determine for each new battle whether one army must win or the result is undecidable.
Level

Hard8 of 10

Topics
Geometry, Math, Brute force, Implementation
Solved
No attempts yet

Problem

The beta version of StarCowraft II is ready! Farmer John and Bessie are testing it, trying different strategies in one-on-one battles against each other's armies. The goal in StarCowraft II is to defeat your opponent's army in a battle.

Each player's army fights in a battle. An army comprises as many as three different types of "units", with respective strengths denoted by constant positive real numbers unknown to the players: cattlebruisers with strength S1S_1, cow templars with strength S2S_2, and ultracows with strength S3S_3. The only bounding information given is that no unit is more than 100 times as strong as any other unit; that is, Si≤100⋅SjS_i \le 100 \cdot S_j for every pair i,ji, j.

An army's total strength is the sum of the individual strengths of each of its units. For example, an army that has, among other units, 23 cattlebruisers gains 23⋅S123 \cdot S_1 strength just from those cattlebruisers.

When two opposing armies fight, the army with the higher total strength wins. If the two armies have exactly equal total strength, one of the players wins at random.

Farmer John and Bessie played NN (0≤N≤3000 \le N \le 300) "test battles". In the ii-th test battle, FJ's army had J1,iJ_{1,i} cattlebruisers, J2,iJ_{2,i} cow templars, and J3,iJ_{3,i} ultracows (0≤J1,i+J2,i+J3,i≤10000 \le J_{1,i} + J_{2,i} + J_{3,i} \le 1000). Similarly, Bessie's army had B1,iB_{1,i} cattlebruisers, B2,iB_{2,i} cow templars, and B3,iB_{3,i} ultracows (0≤B1,i+B2,i+B3,i≤10000 \le B_{1,i} + B_{2,i} + B_{3,i} \le 1000). After the armies fought, FJ and Bessie recorded the winner as a single "victory letter" ViV_i: "J" if Farmer John won, "B" if Bessie won.

Although these victory results are the only information they have, they hope to predict the outcomes of some additional battles when given the unit compositions of the two opposing armies. For some battles, though, it might not be possible to determine the winner with certainty.

Given the results of the NN test battles Farmer John and Bessie already played, write a program that decides the winner (when possible) for MM (1≤M≤20001 \le M \le 2000) new battles.

The reported results of the test battles are correct; there exists at least one set of strength values S1,S2,S3S_1, S_2, S_3 consistent with them.

To demonstrate how army strength is evaluated, consider these test battles fought in a game where we (but neither FJ nor Bessie) know that S1=9.0S_1 = 9.0, S2=7.0S_2 = 7.0, and S3=4.0S_3 = 4.0:

   ---- Farmer John ----    ------- Bessie ------    Battle
   J1  J2  J3 J_Strength    B1  B2  B3 B_Strength   Outcome
    6   5   4    105         5   4   7    101          J
    5   4   2     81         3   5   5     82          B
    9   0  10    121         8   2   7    114          J

These results imply the following deduced outcomes, for the reasons shown:

   ---- Farmer John ----    ------- Bessie ------    Battle
   J1  J2  J3 J_Strength    B1  B2  B3 B_Strength   Outcome
    6   6   4    112         5   4   7    101          J
              FJ's army is even stronger than in test battle 1
    9   0  10    121         8   2   6    110          J
              Bessie's army is even weaker than in test battle 3

Input

  • Line 1: Two space-separated integers NN and MM.
  • Lines 2 through N+1N+1: Line i+1i+1 describes a test battle with seven space-separated items — a victory letter and six space-separated integer unit counts: ViV_i, J1,iJ_{1,i}, J2,iJ_{2,i}, J3,iJ_{3,i}, B1,iB_{1,i}, B2,iB_{2,i}, B3,iB_{3,i}.
  • Lines N+2N+2 through N+M+1N+M+1: Line i+N+1i+N+1 describes a "new battle" using six space-separated integers: J1,iJ_{1,i}, J2,iJ_{2,i}, J3,iJ_{3,i}, B1,iB_{1,i}, B2,iB_{2,i}, B3,iB_{3,i}.

Output

  • Lines 1 through MM: Line ii contains the outcome of the ii-th new battle: "J" if Farmer John definitely wins, "B" if Bessie definitely wins, and "U" (undecidable) if it is impossible to decide the winner with the given information.

Hint

The first two new battles in the example correspond to the two battles deduced in the description. The result of the third new battle cannot be determined with only the information Farmer John and Bessie currently have. Specifically, both S1=9.0,S2=7.0,S3=4.0S_1 = 9.0, S_2 = 7.0, S_3 = 4.0 and S1=12.0,S2=20.0,S3=10.0S_1 = 12.0, S_2 = 20.0, S_3 = 10.0 are consistent with the test battles, but they give different results when plugged into the third new battle.

Examples1

  1. Example 1

    Input
    3 3
    J 6 5 4 5 4 7
    B 5 4 2 3 5 5
    J 9 0 10 8 2 7
    6 6 4 5 4 7
    9 0 10 8 2 6
    3 4 8 4 4 6
    
    Expected output
    J
    J
    U