Problem:


野球大会

 野球の国別対抗戦 WBC で、日本が2連覇達成!! 野球人気が高まる中、会津学園高校を会場に野球大会が行われました。この大会では、総当りのリーグ戦を行い、以下のような方法で順位を決めることになりました。

 勝ち数の多いチームを上位とする

 勝ち数が同数の場合は負け数の少ないチームを上位とする

 各チームの成績を入力とし、チーム名を上位のチームから順に出力するプログラムを作成してください。同順位のチームが存在する場合は、入力順に出力してください。ただし、チーム数 n は 2 以上 10 以下の整数、チーム名 t は 1 文字の半角英字、試合毎の成績 r は n - 1 個の数字で表され、勝ちの場合は 0 、負けの場合は 1 、引き分けの場合は 2 とします。また、チーム名に重複はないものとします。

Input

複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。
各データセットは以下の形式で与えられます。

n
score1
score2
:
scoren

1 行目にチームの数 n (2 ≤ n ≤ 10) 、続く n 行に第 i のチームの成績 scorei が与えられます。各成績は次の形式で与えられます。

t r1 r2 ... rn&minus;1

チーム名 t (１文字の半角英字)、t の試合毎の成績 ri (0, 1, または 2) が空白区切りで与えられます。

データセットの数は 50 を超えません。

Output

データセットごとに、チーム名を上位のチームから順に出力します。

Sample Input

6
A 1 0 0 2 0
B 0 0 1 1 0
C 1 1 1 1 1
D 1 0 0 1 2
E 2 0 0 0 0
F 1 1 0 2 1
4
g 1 1 1
h 0 1 2
w 0 0 0
b 0 2 1
0

Output for the Sample Input

E
A
B
D
F
C
w
h
b
g



Sample input:
6
A 1 0 0 2 0
B 0 0 1 1 0
C 1 1 1 1 1
D 1 0 0 1 2
E 2 0 0 0 0
F 1 1 0 2 1
4
g 1 1 1
h 0 1 2
w 0 0 0
b 0 2 1
0


Sample output:
E
A
B
D
F
C
w
h
b
g

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    if n == 0: break
    tmp = [list(input().split()) for _ in range(n)]
    score = {'0': 2, '1': 0, '2': 1}
    ans = []
    for t in tmp:
        tmpv = 0
        for v in t[1:]:
            tmpv += score[v]
        ans.append(t[0] + str(tmpv))
    for i in range(len(ans)):
        for j in range(i+1, len(ans))[::-1]:
            if ans[j][1] > ans[j-1][1]:
                ans[j], ans[j-1] = ans[j-1], ans[j]
    for a in ans: print(a[0])

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    if n == 0: break
    tmp = [list(input().split()) for _ in range(n)]
    score = {'0': 2, '1': 0, '2': 1}
    ans = []
    for t in tmp:
        tmpv = 0
        for v in t[1:]:
            tmpv += score[v]
        ans.append(t[0] + str(tmpv))
    for i in range(len(ans)):
        for j in range(i+1, len(ans))[::-1]:
            if ans[j][1] > ans[j-1][1]:
                ans[j], ans[j-1] = ans[j-1], ans[j]
    for a in ans:
        print(a[0])

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    if n == 0: break
    tmp = [list(input().split()) for _ in range(n)]
    score = {'0': 2, '1': 0, '2': 1}
    ans = []
    for t in tmp:
        tmpv = 0
        for v in t[1:]:
            tmpv += score[v]
        ans.append(t[0] + str(tmpv))
    for i in range(len(ans)):
        for j in range(i+1, len(ans))[::-1]:
            if ans[j][1] > ans[j-1][1]:
                ans[j], ans[j-1] = ans[j-1], ans[j]
    print('\n'.join(str(a[0]) for a in ans))

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    if n == 0: break
    tmp = [list(input().split()) for _ in range(n)]
    score = {'0': 2, '1': 0, '2': 1}
    ans = []
    for t in tmp:
        tmpv = 0
        for v in t[1:]:
            tmpv += score[v]
        ans.append(t[0] + str(tmpv))
    for i in range(len(ans)):
        for j in range(i+1, len(ans))[::-1]:
            if ans[j][1] > ans[j-1][1]:
                ans[j], ans[j-1] = ans[j-1], ans[j]
    print('\n'.join([a[0] for a in ans]))

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    if n == 0: break
    tmp = [list(input().split()) for _ in range(n)]
    score = {'0': 2, '1': 0, '2': 1}
    ans = []
    for t in tmp:
        tmpv = 0
        for v in t[1:]:
            tmpv += score[v]
        ans.append(t[0] + str(tmpv))
    for i in range(len(ans)):
        for j in range(i+1, len(ans))[::-1]:
            if ans[j][1] > ans[j-1][1]:
                ans[j], ans[j-1] = ans[j-1], ans[j]
    print('\n'.join([a[0] for a in ans]))

Submission status: Wrong Answer

Solution code:
A = [1.,0.,0.1]
while True:
    n = input()
    if n == 0:
        break
    print "\n".join(map(str,zip(*sorted((raw_input().split() for _ in xrange(n)),key = lambda a:sum(A[b] for b in map(int,a[1:])),reverse = True))[0]))

Submission status: Wrong Answer

Solution code:
A = [1.,0.,00.1]
while True:
    n = input()
    if n == 0:
        break
    print "\n".join(map(str,zip(*sorted((raw_input().split() for _ in xrange(n)),key = lambda a:sum(A[b] for b in map(int,a[1:])),reverse = True))[0]))

Submission status: Wrong Answer

Solution code:
A = [1.,0.,0.1]
while True:
    n = input()
    if n == 0:
        break
    L =sorted((raw_input().split() for _ in xrange(n)),
              key = lambda a:sum(A[b] for b in map(int,a[1:])),
              reverse = True)
    print "\n".join(zip(*L)[0])

Submission status: Wrong Answer

Solution code:
A = [1,-0.1,0]
while True:
    n = input()
    if n == 0:
        break
    L =sorted((raw_input().split() for _ in xrange(n)),
              key = lambda a:sum(A[b] for b in map(int,a[1:])),
              reverse = True)
    print "\n".join(zip(*L)[0])

Submission status: Wrong Answer

Solution code:
while 1:
	n = input()
	if n == 0: break
	rslt = [raw_input().replace("2","").split() for i in range(n)]
	rslt = sorted(rslt, key = lambda x:x.count("1"))
	for i in range(n):
		print rslt[i][0]

Submission status: Accepted

Solution code:
import sys
import operator
f = sys.stdin

while True:
    n = int(f.readline())
    if n == 0:
        break
    teams = [f.readline().split() for i in range(n)]
    
    teams = [(t[0], -t.count('0'), t.count('1'), i) for i, t in enumerate(teams)]
    
    teams.sort(key=operator.itemgetter(1,2,3))
    
    print('\n'.join([t[0] for t in teams]))

Submission status: Accepted

Solution code:
while True:
    n = int(raw_input())
    if n == 0: break
    ans = [[0]*4 for i in range(n)]
    for i in range(n):
        line = raw_input().split()
        ans[i][2] = -i
        ans[i][3] = line[0]
        for c in line[1:]:
            if c == '0':
                ans[i][0] += 1
            elif c == '1':
                ans[i][1] -= 1
    for a in sorted(ans, key=lambda x: (x[0], x[1], x[2]), reverse=True):
        print a[3]

Submission status: Accepted

Solution code:
def parse(i):
    l = input().strip().split()
    m = l.pop(0)
    return (-l.count('0'), l.count('1'), i, m)


n = int(input())

while n:
    print('\n'.join(str(t[3]) for t in sorted([parse(_) for _ in range(n)])))
    n = int(input())

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    d = []
    for i in range(n):
        tmp = input().split()
        t = tmp[0]
        w = sum(1 for c in tmp[1:] if not int(c))
        l = sum(1 for c in tmp[1:] if int(c) == 2)
        d.append([t, w, l, n-i])
    ans = sorted(d, key=lambda x: (x[1], x[2], x[3]))
    for a in ans[::-1]:
        print(a[0])

Submission status: Accepted

Solution code:
# Aizu Problem 0196: Baseball Championship
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")



while True:
    n = int(input())
    if n == 0:
        break
    teams = []
    for i in range(n):
        rec = input().split()
        name = rec[0]
        rec = [int(_) for _ in rec[1:]]
        wins = rec.count(0)
        losses = rec.count(1)
        draws = rec.count(2)
        teams.append((i, name, wins, draws, losses))
    teams = sorted(teams, key=lambda x: (-x[2], x[4], x[0]))
    for team in teams:
        print(team[1])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0196

"""
import sys
from sys import stdin
from heapq import heappop, heappush
input = stdin.readline


def main(args):
    while True:
        n = int(input())
        if n == 0:
            break

        pq = []
        for seq in range(n):
            temp = input().split()
            team_name = temp[0]
            temp = temp[1:]
            win_count = temp.count('0')
            lose_count = temp.count('1')
            heappush(pq, (-win_count, lose_count, seq, team_name))

        while pq:
            _, _, _, team_name = heappop(pq)
            print(team_name)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
WIN = "0"
LOSE = "1"

while True:

    input_count = int(input())

    if input_count == 0:
        break

    input_list = [input().split(" ") for _ in range(input_count)]

    count_list = [(item[0], item.count(WIN), item.count(LOSE)) for item in input_list]
    count_list.sort(key=lambda item: (-item[1], item[2]))

    output = [item[0] for item in count_list]
    print("\n".join(output))

Submission status: Accepted

Solution code:
# AOJ 0196 Baseball Championship
# Python3 2018.6.21 bal4u

while 1:
	n = int(input())
	if n == 0: break
	team = []
	for i in range(n):
		r = list(input())
		t = r.pop(0)
		w = l = 0
		for p in r:
			if p.isdigit() == False: pass
			elif int(p) == 0: w += 1      # 勝ち数
			elif int(p) == 1: l += 1      # 負け数
		team.append((t, i, w, l))
	for i in sorted(team, key=lambda x:(-x[2],x[3],x[1])): print(*i[0])

Submission status: Accepted

Solution code:
# AOJ 0196 Baseball Championship
# Python3 2018.6.21 bal4u

while 1:
	n = int(input())
	if n == 0: break
	team = []
	for i in range(n):
		r = list(input().split())
		t = r.pop(0)
		w = l = 0
		for p in r:
			if int(p) == 0: w += 1      # 勝ち数
			elif int(p) == 1: l += 1    # 負け数
		team.append((t, i, w, l))
	for i in sorted(team, key=lambda x:(-x[2],x[3],x[1])): print(*i[0])

Submission status: Accepted

Solution code:
while True:
  n = int(input())
  if n == 0:
    break
  result = []
  for _ in range(n):
    lst = input().split()
    result.append(((lst[1:].count("0"), lst[1:].count("2"), lst[1:].count("1")), lst[0]))
  result.sort(key=lambda x:-(x[0][0] * 10 + x[0][1]))
  for r in result:
    print(r[1])

Submission status: Accepted

Solution code:
from collections import defaultdict
while True:
    n=input()
    if n==0:break
    dic=defaultdict(list)
    for i in range(n):
        L=raw_input().split()
        name,rslt=L[0],L[1:]
        dic[(rslt.count("0"),-rslt.count("1"),rslt.count("2"))].append(name)

    for i in  reversed(sorted(dic.keys())):
        for j in  dic[i]:
	    print j

Submission status: Accepted

Solution code:
A = [1,-0.1,0]
while True:
    n = input()
    if n == 0:
        break
    L =sorted((raw_input().split() for _ in xrange(n)),
              key = lambda a:(a.count("0"),-a.count("1"),a.count("2")),
              reverse = True)
    print "\n".join(zip(*L)[0])

Submission status: Accepted

Solution code:
while 1:
	n = input()
	if n == 0: break
	rslt = [raw_input().replace("2","").split() for i in range(n)]
	rslt = sorted(sorted(rslt, key = lambda x:x.count("1")), key = lambda x:x.count("0"), reverse = True)
	for i in range(n):
		print rslt[i][0]

Submission status: Accepted

Solution code:
while 1:
	n = input()
	if n == 0: break
	rslt = sorted(sorted([raw_input().replace("2","") for i in range(n)], key = lambda x:x.count("1")), key = lambda x:x.count("0"), reverse = 1)
	for i in range(n): print rslt[i][0]

Submission status: Accepted

Solution code:
while 1:
	n = input()
	if n == 0:break
	r = sorted(sorted([raw_input().replace("2","") for i in range(n)],key = lambda x:x.count("1")),key = lambda x:x.count("0"),reverse = 1)
	for i in r:print i[0]

Submission status: Accepted

Solution code:
while True:
    L = []
    n = int(input())
    if n == 0:
        break
    for i in range(n):
        r =  list(input().split())
        t = r.pop(0)
        w = l = 0
        for a in r:
            if int(a) == 0:
                w += 1
            elif int(a) == 1:
                l += 1
        L.append((t,w,l,i))

    for i in sorted(L, key = lambda x:(-x[1],x[2],x[3])):
        print(i[0])

Submission status: Accepted

Solution code:
def solve():
    from sys import stdin
    f_i = stdin
    
    ans = ""
    
    while True:
        n = int(f_i.readline())
        
        if n == 0:
            break
        
        result = []
        
        for i in range(n):
            score = f_i.readline()
            
            t = score[0]
            win = score.count('0')
            lose = score.count('1')
            
            result.append((-win, lose, i, t))
        
        result.sort()
        
        ans += '\n'.join(map(lambda x: x[-1], result))
        ans += '\n'
    
    print(ans, end='')

solve()

Submission status: Accepted

Solution code:
from collections import Counter

while True:
    n=int(input())
    if n==0:
        break

    scores=[]
    for i in range(n):
        s=input().split(" ")
        team=s[0]
        score=s[1:]
        c=Counter(score)
        c=dict(c)
        result=[team,c.get('0',0),c.get('1',0),c.get('2',0)]
        scores.append(result)
    scores=sorted(scores,key=lambda w: w[1]*100+w[3],reverse=True)
    for s in scores:
        print(s[0])

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    ls = []
    for i in range(n):
        v = input().split()
        sum = 0
        for j in range(1,n):
            v[j] = int(v[j])
            if v[j] == 0:
                sum += 100
            if v[j] == 1:
                sum -= 1
        ls.append([sum,-i,v[0]])
    ls = sorted(ls)
    ls.reverse()
    for i in range(n):
        print(ls[i][2])

Submission status: Accepted

Solution code:
while 1:
    N = int(input())
    if N == 0:
        break
    L = []
    for i in range(N):
        t, *rs = input().split()
        w = 0; l = 0
        for e in map(int, rs):
            if e == 0:
                w += 1
            elif e == 1:
                l += 1
        L.append((-w, l, i, t))
    L.sort()
    for _, _, _, t in L:
        print(t)

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    sets = []
    for i in range(n):
        hokan = input().split()
        sets.append([hokan.count("0"),hokan.count("2"),n-i,hokan[0]])
    sets.sort(reverse = True)
    for i in range(n):
        print(sets[i][3])

Submission status: Accepted

Solution code:
while True:
    num = int(input())
    if num==0:
        break
    L = []
    for i in range(num):
        n, *k = input().split()
        L.append( (n, k.count("0"), k.count("1"), i) )
    L.sort(key=lambda x:(-x[1],x[2],x[3]) )
    
    for x in L:
        print(x[0])

Submission status: Accepted

Solution code:
if __name__ == '__main__':

	while True:
		try:
			n = int(input())

			if n == 0:
				break

			A = []

			for _ in range(n):
				cmd = input().split()
				A.append((cmd.count("0"),cmd.count("1"),cmd.count("2"),cmd[0]))

			B = sorted(A,key=lambda x:(-x[0],x[1],x[2]))

			for y in B:
				print(y[3])

		except EOFError:
			break

Submission status: Accepted

Solution code:

class team:
    def __init__(self,name,result):
        self.name = name
        self.win = 0
        self.lose = 0
        for i in range(len(result)):
            if result[i] == 0:
                self.win += 1
            elif result[i] == 1:
                self.lose +=1
            else:
                pass

while True:
    results = []
    n = int(input())

    if n==0:
        break

    for i in range(n):
        lst = input().split()
        name = lst[0]
        lst = lst[1:]
        result = list(map(int,lst))

        results.append(team(name,result))

    results = sorted(results,key = lambda w:w.lose)
    results = sorted(results,key = lambda w:w.win, reverse = True)

    for i in range(n):
        print(results[i].name)

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    if N == 0: return -1
    score = []
    for i in range(N):
        s = input().split()
        win, draw = 0, 0
        for j in range(1, len(s)):
            if s[j] == '1': continue
            elif s[j] == '0': win += 1 
            else : draw += 1
        score.append((s[0], win, draw))
    res = sorted(score, reverse=True, key=lambda x:(x[1], x[2]))
    for s in res: print(s[0])

while True:
    if solve() != None: break

Submission status: Accepted

Solution code:
while 1:
    n = int(input())
    if n == 0:
        break

    result = []
    for i in range(n):
        data = list(map(str, input().split()))
        name = data.pop(0)
        win = data.count("0")
        lose = data.count("1")
        result.append([name, win, lose])

    result = sorted(result, key=lambda x: (-x[1], x[2]))

    for r in result:
        print(r[0])

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0: break
    result = [input().split() for _ in range(n)]
    result = [[r[0], r.count('0'), r.count('1')] for r in result]
    result.sort(key=lambda x: [-x[1], x[2]])
    result = [r[0] for r in result]
    print('\n'.join(result))

Submission status: Runtime Error

Solution code:
import sys
f = sys.stdin

while True:
    n = int(f.readline())
    if n == 0:
        break
    teams = [f.readline().split() for i in range(n)]
    
    teams = [(t[0], -t.count('0'), t.count('1'), i) for i, t in enumerate(teams)]
    
    teams.sort(key=operator.itemgetter(1,2,3))
    
    print('\n'.join([t[0] for t in teams]))