World Cup Draw

Time limit2sMemory limit512 MB

Summary
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.

  1. No two teams from the same pot can be drawn into the same group.
  2. 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 xx from Pot ii is placed in the first group from the left (starting at group A) that satisfies both of the following conditions. First, placing xx there violates neither rule 1 nor rule 2. Second, the teams of Pot ii 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 ii can always be distributed into the groups under rules 1 and 2, no matter how the teams of Pot 1 to Pot i−1i-1 were distributed.

The table below shows the pots. In the table, xx (r,c)(r, c) means that team xx belongs to confederation cc and its rank in the FIFA ranking is rr. Write a program that simulates the draw for the given draw order of each pot and reports the groups.

Seeding Pot 1Seeding Pot 2Seeding Pot 3Seeding Pot 4
Russia (65, UEFA)Spain (8, UEFA)Denmark (19, UEFA)Serbia (38, UEFA)
Germany (1, UEFA)Peru (10, CONMEBOL)Iceland (21, UEFA)Nigeria (41, CAF)
Brazil (2, CONMEBOL)Switzerland (11, UEFA)Costa Rica (22, CONCACAF)Australia (43, AFC)
Portugal (3, UEFA)England (12, UEFA)Sweden (25, UEFA)Japan (44, AFC)
Argentina (4, CONMEBOL)Colombia (13, CONMEBOL)Tunisia (28, CAF)Morocco (48, CAF)
Belgium (5, UEFA)Mexico (16, CONCACAF)Egypt (30, CAF)Panama (49, CONCACAF)
Poland (6, UEFA)Uruguay (17, CONMEBOL)Senegal (32, CAF)South Korea (62, AFC)
France (7, UEFA)Croatia (18, UEFA)Iran (34, AFC)Saudi Arabia (63, AFC)

Input

The input holds several test cases. Each test case consists of 4 lines. The iith line gives the names of all 8 teams in Pot ii 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.

Examples1

  1. Example 1

    Input
    Russia, Germany, Brazil, Portugal, Argentina, Belgium, Poland, France
    Spain, Peru, Switzerland, England, Colombia, Mexico, Uruguay, Croatia
    Denmark, Iceland, Costa Rica, Sweden, Tunisia, Egypt, Senegal, Iran
    Serbia, Nigeria, Australia, Japan, Morocco, Panama, South Korea, Saudi Arabia
    Russia, Germany, Brazil, Portugal, Argentina, Belgium, Poland, France
    Spain, Peru, Switzerland, England, Colombia, Mexico, Uruguay, Croatia
    Denmark, Iceland, Costa Rica, Sweden, Tunisia, Egypt, Senegal, Iran
    Serbia, Nigeria, Morocco, Panama, Australia, Japan, South Korea, Saudi Arabia
    End
    
    Expected output
    B 73
    C 78
    E 83
    D 92
    H 107
    F 110
    G 118
    A 136
    C 77
    B 78
    E 83
    D 87
    H 108
    F 110
    G 118
    A 136