Official

C - Swiss-System Tournament Editorial by en_translator


Simulate what is written in the problem statement.

We can simulate the matches if we know the number of each person’s wins, so it is sufficient to manage an array that stores “the number of wins and ID who ranks \(i\)-th”.

It is a good idea to separate as a function the part determining the result of a match.

Sample code (Python)

N,M = map(int,input().split())
S = [input() for i in range(2*N)]
rank = [[0,i] for i in range(2*N)]
# rank[i] = [x,y]  -> the person who ranks $i$-th has won -x times and has ID y

def judge(a,b):
  # -1 if draw, 0 if the former wins, or 1 if the latter wins
  if a==b: return -1
  if a=='G' and b=='P': return 1
  if a=='C' and b=='G': return 1
  if a=='P' and b=='C': return 1
  return 0

for j in range(M):
  for i in range(N):
    player1 = rank[2*i][1]
    player2 = rank[2*i+1][1]
    result = judge(S[player1][j], S[player2][j])
    if result !=-1: rank[2*i+result][0] -= 1
  rank.sort()

for _,i in rank: print(i+1)

posted:
last update: