Finding Team Member (Codeforces 579B)
Tags: Implement, sortings

There is a programing contest named SnakeUp, 2n people want to compete for it. In order to attend this contest, people need to form teams of exactly two people. You are given the strength of each possible combination of two people. All the values of the strengths are distinct.Every contestant hopes that he can find a teammate so that their team’s strength is as high as possible. That is, a contestant will form a team with highest strength possible by choosing a teammate from ones who are willing to be a teammate with him/her. More formally, two people A and B may form a team if each of them is the best possible teammate (among the contestants that remain unpaired) for the other one. Can you determine who will be each person’s teammate?

Input: There are 2n lines in the input. The first line contains an integer n (1 ≤ n ≤ 400) — the number of teams to be formed.The i-th line (i > 1) contains i - 1 numbers ai1, ai2, ... , ai(i - 1). Here aij (1 ≤ aij ≤ 106, all aij are distinct) denotes the strength of a team consisting of person i and person j (people are numbered starting from 1.)

Output: Output a line containing 2n numbers. The i-th number should represent the number of teammate of i-th person.

Note: In the first sample, contestant 1 and 2 will be teammates and so do contestant 3 and 4, so the teammate of contestant 1, 2, 3, 4 will be 2, 1, 4, 3 respectively.

Sample Inputs:
2
6
1 2
3 4 5

3
487060
3831 161856
845957 794650 976977
83847 50566 691206 498447
698377 156232 59015 382455 626960

Sample Outputs:
2 1 4 3

6 5 4 3 2 1

Solution (Python):
n = int(input())
strengths = []
for i in range(1, 2 * n):
    strengths.append(list(map(int, input().split())))
teammates = [0] * (2 * n)
pairs = []
for i in range(1, 2 * n):
    for j in range(i):
        pairs.append((strengths[i - 1][j], i, j))
pairs.sort(reverse=True)
for (s, i, j) in pairs:
    if teammates[i] == 0 and teammates[j] == 0:
        teammates[i] = j + 1
        teammates[j] = i + 1
print(*teammates)

Solution (Python):
from sys import stdin
n = int(stdin.readline())
c = 0
poss = []
for i in range(2 * n - 1):
    c += 1
    ppl = map(int, stdin.readline().split())
    for (j, p) in enumerate(ppl):
        poss.append((c, j, p))
taken = set()
poss = sorted(poss, reverse=True, key=lambda x: x[2])
answer = [-1 for i in range(n * 2)]
for p in poss:
    if p[0] not in taken and p[1] not in taken:
        answer[p[0]] = str(p[1] + 1)
        answer[p[1]] = str(p[0] + 1)
        taken.add(p[0])
        taken.add(p[1])
print(' '.join(answer))

Solution (Python):
from sys import stdin
live = True
if not live:
    stdin = open('data.in', 'r')
numbers = []
n = int(stdin.readline().strip())
for i in range(2, 2 * n + 1):
    line = list(map(int, stdin.readline().strip().split()))
    it = 1
    for j in line:
        numbers.append((j, i, it))
        it += 1
teams = 0
pairs = [0] * (2 * n + 1)
numbers = sorted(numbers, key=lambda tup: tup[0], reverse=True)
for (m, i, j) in numbers:
    if teams == n:
        break
    if not pairs[i] and (not pairs[j]):
        pairs[i] = j
        pairs[j] = i
        teams += 1
print(' '.join(list(map(str, pairs[1:]))))
if not live:
    stdin.close()