World Cup Group Stage
InterviewTime limit2sMemory limit512 MB
Given a group table with one missing score for team T, find the smallest goal count that lets T finish in the top two, or -1 if none exists up to K.
- Level
Medium5 of 10
- Topics
- Binary search, Implementation, Sorting
- Solved
- No attempts yet
Problem
The FIFA World Cup is held every four years. Starting with the 2022 Qatar World Cup, teams are divided into groups of four, and each team plays every other team in its group once. Only the top two teams in each group advance to the knockout stage. Group standings are decided by the following criteria, in order:
- Points: the team with more points ranks higher. Points depend on the match results: 3 for a win, 1 for a draw, 0 for a loss.
- Goal difference: the team with a larger (goals scored - goals conceded) ranks higher.
- Goals scored: the team that scored the most goals ranks higher.
- Head-to-head: the team with the higher head-to-head rank in the group ranks higher.
Standings are sorted by points first. If points are tied, goal difference decides. If goal difference is also tied, goals scored decides. If that is tied too, head-to-head rank decides.
Teams A, B, C, and D are placed in one group. Their head-to-head ranks are always A first, B second, C third, and D fourth. After all matches, a score table shows how many goals each team scored in each match.
is the number of goals team scored against team . Equivalently, is the number of goals team conceded against team .
Table 1
The result of any match can be read from the table. To find the result of Team A against Team B, check row Team A, column (Team B), and row Team B, column (Team A). Here the result is 2:0, a win for Team A.
In the match between Team A and Team D, Team D's score is missing and is marked . Team D must score at least 2 goals against Team A to advance.
Given a score table where one score of team is missing, find the minimum value of that lets team advance. If team cannot advance regardless of the value of , print -1.
Input
The first line contains the head-to-head rank of team and a positive integer , separated by a space.
The next 4 lines contain the score table. Each line contains the scores separated by spaces.
- If , the value is given as
0. - If :
- A score that is not missing is given as a non-negative integer.
- The missing score is given as
-1.
The second line is the scores of Team A, the third line Team B, the fourth line Team C, and the fifth line Team D.
The score table always has exactly one missing score, and team always has a missing score in one of its matches.
Output
Print the minimum value of that lets team advance.
However, if the minimum is greater than , or if team cannot advance regardless of the value of , print -1.