この前の解答例

全然最適化とか考えてない実行例。

lines = 0
input = []

# input from standard input
while line = STDIN.gets
  line.chomp!
  if line != ""
    if lines == 0
      limit = line.to_i
    else
      line = line.split(" ")
      input << [line[0], line[1].to_i]
    end
    lines += 1
  else
    break
  end
end

players = input.length
matches = (players - 1) / 4 + 1

player_list = []
match_players = []
for i in 0 .. matches - 1 do
  match_players[0] = input[i * 4 + 0]
  match_players[1] = input[i * 4 + 1] || ["dummy1", 0]
  match_players[2] = input[i * 4 + 2] || ["dummy2", 0]
  match_players[3] = input[i * 4 + 3] || ["dummy3", 0]
  
  # player[1] = player's score
  # original sort by sorts smaller first, so take negative
  players_sorted = match_players.sort_by{ |player| -player[1] }
  # maximum score is placed in match_players[0][1]
  max = players_sorted[0][1]
  # for each player calculate rank and loss rate
  for j in 0 .. 3 do
    match_players[j] << players_sorted.index(match_players[j]) + 1
    match_players[j] << match_players[j][1].to_f / max.to_f
    player_list << match_players[j]
  end
end

# loss rate is placed in player[3]
players_sorted = player_list.sort_by{ |player| -player[3] }

result = []
count = limit

players_sorted.each do |player|
  if (player[2] == 1 || player[2] == 2) && !(player[0] =~ /dummy/)
    # if player's rank is 1 or 2 then pass
    result << player
    count -= 1
  end
end

players_sorted.each do |player|
  if !(player[2] == 1 || player[2] == 2) && count >= 1 && !(player[0] =~ /dummy/)
    # players pass until reaches limit
    result << player
    count -= 1
  end
end

result.each do |player|
  puts player[0]
end