World Cup Draw
Time limit2sMemory limit512 MB
Simulate the World Cup draw by placing each team into the leftmost group that keeps the rest of the pot placeable, then sort groups by total rank.
- Level
Medium7 of 10
- Topics
- Greedy, Backtracking, Implementation, Simulation
- Solved
- No attempts yet
Problem
The draw for the 2018 FIFA World Cup took place on Friday, December 1 at the State Kremlin Palace in Moscow. FIFA published the draw procedure on its official website a few months earlier, and it works as follows.
The 32 qualified finalists are first split into four seeding pots based on the FIFA ranking for October 2017. Pot 1 holds the host Russia and the seven highest ranked teams, Pot 2 holds the next eight highest ranked teams, and Pots 3 and 4 are filled the same way. The teams are then drawn into eight groups of four, labeled A to H. The pots are emptied into the groups in order from Pot 1 to Pot 4. The draw must obey these two rules.
- No two teams from the same pot can be drawn into the same group.
- With the exception of UEFA, no two teams from the same confederation can be drawn into the same group. UEFA is the exception because it has more qualified teams (14) than there are groups (8), and at most two UEFA teams can be drawn into the same group.
Order the groups alphabetically from A to H, with A the leftmost group and H the rightmost. At each step of the draw, the drawn team from Pot is placed in the first group from the left (starting at group A) that satisfies both of the following conditions. First, placing there violates neither rule 1 nor rule 2. Second, the teams of Pot that have not been drawn yet can still be distributed in the following steps without violating rule 1 or rule 2. Computer scientists have assured FIFA that the eight teams of Pot can always be distributed into the groups under rules 1 and 2, no matter how the teams of Pot 1 to Pot were distributed.
The table below shows the pots. In the table, means that team belongs to confederation and its rank in the FIFA ranking is . Write a program that simulates the draw for the given draw order of each pot and reports the groups.
Input
The input holds several test cases. Each test case consists of 4 lines. The th line gives the names of all 8 teams in Pot in the draw order, from left to right. Team names are written exactly as in the table and are separated by commas, and a name may have a space before or after it. In every test case the first team drawn from Pot 1 is Russia, following the old tradition of placing the host country in group A. The input ends with a line holding "End", which must not be processed.
Output
For each test case, simulate the draw in the given draw order, then compute the weight of each group, which is the sum of the ranks of the four teams in that group. For each group, print the group name (one uppercase letter) and its weight on one line, separated by a single space. Print the groups in increasing order of weight, from the strongest group (the one with the smallest weight) to the weakest group (the one with the largest weight). Break a tie by the alphabetical order of the group names.