World Football Cup (Codeforces 19A)
Tags: Implement, implementation

Everyone knows that 2010 FIFA World Cup is being held in South Africa now. By the decision of BFA (Berland's Football Association) next World Cup will be held in Berland. BFA took the decision to change some World Cup regulations:  the final tournament features n teams (n is always even)  the first n / 2 teams (according to the standings) come through to the knockout stage  the standings are made on the following principle: for a victory a team gets 3 points, for a draw — 1 point, for a defeat — 0 points. In the first place, teams are ordered in the standings in decreasing order of their points; in the second place — in decreasing order of the difference between scored and missed goals; in the third place — in the decreasing order of scored goals  it's written in Berland's Constitution that the previous regulation helps to order the teams without ambiguity. You are asked to write a program that, by the given list of the competing teams and the results of all the matches, will find the list of teams that managed to get through to the knockout stage.

Input: The first input line contains the only integer n (1 ≤ n ≤ 50) — amount of the teams, taking part in the final tournament of World Cup. The following n lines contain the names of these teams, a name is a string of lower-case and upper-case Latin letters, its length doesn't exceed 30 characters. The following n·(n - 1) / 2 lines describe the held matches in the format name1-name2 num1:num2, where name1, name2 — names of the teams; num1, num2 (0 ≤ num1, num2 ≤ 100) — amount of the goals, scored by the corresponding teams. Accuracy of the descriptions is guaranteed: there are no two team names coinciding accurate to the letters' case; there is no match, where a team plays with itself; each match is met in the descriptions only once.

Output: Output n / 2 lines — names of the teams, which managed to get through to the knockout stage in lexicographical order. Output each name in a separate line. No odd characters (including spaces) are allowed. It's guaranteed that the described regulations help to order the teams without ambiguity.


Sample Inputs:
4
A
B
C
D
A-B 1:1
A-C 2:2
A-D 1:0
B-C 1:0
B-D 0:3
C-D 0:3

2
a
A
a-A 2:1

Sample Outputs:
A
D

a

Solution (Python):
def main() -> None:
    n = int(input())
    teams = dict()
    for _ in range(n):
        team_name = input()
        teams[team_name] = [0, 0, 0]
    for _ in range(n * (n - 1) // 2):
        s = input()
        (team, score) = s.split()
        (team_a, team_b) = team.split('-')
        (score_a, score_b) = score.split(':')
        a = int(score_a)
        b = int(score_b)
        teams[team_a][1] += a
        teams[team_a][2] += b
        teams[team_b][1] += b
        teams[team_b][2] += a
        if a < b:
            teams[team_b][0] += 3
        elif a > b:
            teams[team_a][0] += 3
        else:
            teams[team_a][0] += 1
            teams[team_b][0] += 1
    teams_list = teams.items()
    teams_list = sorted(teams_list, key=lambda x: (x[1][0], x[1][1] - x[1][2], x[1][1]), reverse=True)
    qualified = teams_list[:n // 2]
    qualified = sorted(qualified, key=lambda x: x[0])
    for k in qualified:
        print(k[0])
if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
teams = [input() for _ in range(n)]
m = [input().split() for _ in range(n * (n - 1) // 2)]
teams_data = {element: [0, 0, 0] for element in teams}
matches = []
for element in m:
    line = []
    line.append(element[0].split('-'))
    line.append(element[1].split(':'))
    matches.append(line)
for match in matches:
    if int(match[1][0]) > int(match[1][1]):
        teams_data[match[0][0]][2] += 3
    elif int(match[1][0]) == int(match[1][1]):
        teams_data[match[0][0]][2] += 1
        teams_data[match[0][1]][2] += 1
    else:
        teams_data[match[0][1]][2] += 3
    teams_data[match[0][0]][0] += int(match[1][0])
    teams_data[match[0][1]][0] += int(match[1][1])
    teams_data[match[0][0]][1] += int(match[1][1])
    teams_data[match[0][1]][1] += int(match[1][0])
data = teams_data.items()
data = sorted(data, key=lambda x: (x[1][2], x[1][0] - x[1][1], x[1][0]), reverse=True)
last_teams = data[:n // 2]
sorted_teams = sorted(last_teams)
for element in sorted_teams:
    print(element[0])

Solution (Python):
n = int(input())
druzyny = {}
for i in range(n):
    druzyny[input()] = [0, 0, 0, 0]
for i in range(n * (n - 1) // 2):
    (zawodnicy, wyniki) = input().split()
    (z1, z2) = zawodnicy.split('-')
    (w1, w2) = list(map(int, wyniki.split(':')))
    if w1 == w2:
        druzyny[z1][0] += 1
        druzyny[z2][0] += 1
    elif w1 > w2:
        druzyny[z1][0] += 3
    elif w1 < w2:
        druzyny[z2][0] += 3
    druzyny[z1][2] += w1
    druzyny[z2][2] += w2
    druzyny[z1][3] += w2
    druzyny[z2][3] += w1
    druzyny[z1][1] = druzyny[z1][2] - druzyny[z1][3]
    druzyny[z2][1] = druzyny[z2][2] - druzyny[z2][3]
druzynyit = list(druzyny.items())
wynik = []

def sortowanie(lista):
    flaga = True
    while flaga:
        flaga = False
        for i in range(len(lista) - 1):
            if lista[i][1] < lista[i + 1][1]:
                (lista[i], lista[i + 1]) = (lista[i + 1], lista[i])
                flaga = True
    return lista
wynik = sortowanie(druzynyit)
listafinal = []
for i in range(n // 2):
    listafinal.append(wynik[i][0])
listafinal.sort()
for x in listafinal:
    print(x)