Problem:


Look for the Winner!

The citizens of TKB City are famous for their deep love in elections and vote counting.
Today they hold an election for the next chairperson of the electoral commission.
Now the voting has just been closed and the counting is going to start.
The TKB citizens have strong desire to know the winner as early as possible during vote counting.

The election candidate receiving the most votes shall be the next chairperson.
Suppose for instance that we have three candidates A, B, and C and ten votes.
Suppose also that we have already counted six of the ten votes and the vote counts of A, B, and C are four, one, and one, respectively.
At this moment, every candidate has a chance to receive four more votes and so everyone can still be the winner.
However, if the next vote counted is cast for A, A is ensured to be the winner since A already has five votes and B or C can have at most four votes at the end.
In this example, therefore, the TKB citizens can know the winner just when the seventh vote is counted. 

Your mission is to write a program that receives every vote counted, one by one, identifies the winner, and determines when the winner gets ensured.

Input

The input consists of at most 1500 datasets, each consisting of two lines in the following format.

n

c1 c2 &hellip; cn

n in the first line represents the number of votes, and is a positive integer no greater than 100.
The second line represents the n votes, separated by a space.
Each ci (1 ≤ i ≤ n) is a single uppercase letter, i.e. one of 'A' through 'Z'.
This represents the election candidate for which the i-th vote was cast.
Counting shall be done in the given order from c1 to cn.

You should assume that at least two stand as candidates even when all the votes are cast for one candidate.

The end of the input is indicated by a line containing a zero.

Output

For each dataset, unless the election ends in a tie, output a single line containing an uppercase letter c and an integer d separated by a space:
c should represent the election winner and d should represent after counting how many votes the winner is identified.
Otherwise, that is, if the election ends in a tie, output a single line containing `TIE'.

Sample Input

1
A
4
A A B B
5
L M N L N
6
K K K K K K
6
X X X Y Z X
10
A A A B A C A C C B
10
U U U U U V V W W W
0

Output for the Sample Input

A 1
TIE
TIE
K 4
X 5
A 7
U 8



Sample input:
1
A
4
A A B B
5
L M N L N
6
K K K K K K
6
X X X Y Z X
10
A A A B A C A C C B
10
U U U U U V V W W W
0


Sample output:
A 1
TIE
TIE
K 4
X 5
A 7
U 8

Submission status: Accepted

Solution code:
while True:
    vote_n = int(input())
    if vote_n == 0:
        break
    vote_lst = [v for v in input().split()]
    
    vote_cnt = {}
    for i, v in enumerate(vote_lst):
        if not v in vote_cnt:
            vote_cnt[v] = 1
        else:
            vote_cnt[v] += 1
        
        first = 0
        second = 0
        first_key = None
        for k in vote_cnt:
            if first < vote_cnt[k]:
                second = first
                first = vote_cnt[k]
                first_key = k
            elif second < vote_cnt[k]:
                second = vote_cnt[k]
        
        if first - second > vote_n - (i + 1):
            print(first_key, i + 1)
            break
    if first == second:    
        print('TIE')

Submission status: Accepted

Solution code:
import sys


from inspect import currentframe


def pri(*args):
    names = {id(v): k for k, v in currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg), '???')+' = '+repr(arg) for arg in args))


def solve(n, c):
    flat = ord("A")
    vote_count = [[i, 0] for i in range(26)]
    for i in range(n):
        vote_str = c[i]
        vote_count[ord(vote_str) - flat][1] += 1

        vote_count.sort(key=lambda x: x[1])
        if vote_count[-1][1] > vote_count[-2][1] + (n - i - 1):
            print(chr(vote_count[-1][0] + ord("A")) + " " + str(i+1))
            return
        vote_count.sort()
    print("TIE")
    return


if __name__ == '__main__':
    while True:
        n_input = int(input())
        if n_input == 0:
            break
        c_imput = list(map(str, input().split()))

        solve(n_input, c_imput)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import Counter
while True:
    n = int(input())
    if n == 0:
        break
    c = input().split()
    cnt = Counter()

    for i, x in enumerate(c):
        cnt[x] += 1
        m1 = ('', 0)
        m2 = ('', 0)
        for k, v in cnt.items():
            if m1[1] < v:
                m1 = (k, v)
        for k, v in cnt.items():
            if k != m1[0] and m2[1] < v:
                m2 = (k, v)
        if m1[1] - m2[1] >= n - i:
            print(m1[0], i + 1)
            break
    else:
        print('TIE')

Submission status: Accepted

Solution code:
from collections import defaultdict

# -*- coding: utf-8 -*-
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    N = int(input())
    if N == 0:
        exit()
    votes = input().split()
    remain = N
    d = dict()
    for a in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
        d[a] = 0
    for v in votes:
        d[v] += 1
        remain -= 1
        # maxを探す
        # max>other + remain -> fin
        max_key = max((v, k) for (k, v) in d.items())[1]
        if all(val+remain < d[max_key] for key, val in d.items() if key != max_key):
            print(max_key, N-remain)
            return
    print("TIE")


while True:
    solve()

Submission status: Accepted

Solution code:
def main():
    while True:
        votesum = int(input())
        #print("----------")
        halfsum = votesum // 2
        if votesum == 0: break

        votedata = []
        votedata = list(map(str, input().split()))
        #print("votedata::")
        #print(votedata)

        nowdata = dict()
        nowdata['o'] = 0
        #[[候補者,得票数],[候補者,得票数]...]

        for i in range(len(votedata)):
            cand = votedata[i]

            voteremn = votesum - (i + 1)

            if newcand(votedata, cand, i): nowdata[cand] = 1
            else: nowdata[cand] += 1

            nowdata = sorted(nowdata.items(), key = lambda x:x[1], reverse = True)
            #print(nowdata)
            #print("残り" + str(voteremn))
            if nowdata[0][1] > halfsum:
                print(str(nowdata[0][0] + " " +  str(i + 1)))
                break
            elif nowdata[0][1] > nowdata[1][1] + voteremn:
                print(str(nowdata[0][0] + " " + str(i + 1)))
                break

            nowdata = dict(nowdata)

            if i == len(votedata) - 1:
                print("TIE")
                break
            
        


def newcand(data, cand, now):
    for i in range(now):
        item = data[i]
        if item == cand: return False
    return True
main()

Submission status: Accepted

Solution code:
n=int(input())
large=list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
while n!=0:
    vote=list(input().split())
    vote_number=0
    stack=[0]*26
    flag=False
    for i in vote:
        vote_number+=1
        stack[large.index(i)]+=1
        second_max=[i for i in stack]
        second_max[stack.index(max(stack))]=-1
        if max(stack)>max(second_max)+len(vote)-vote_number:
            print(large[stack.index(max(stack))],vote_number)
            flag=True
            break
    if flag==False:
        print("TIE")
    n=int(input())

Submission status: Accepted

Solution code:
import collections

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

    data = list(input().split())
    vote = [0] * 26
    alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    alpha = list(alpha)

    for i, v in enumerate(data):
        num = alpha.index(v)
        vote[num] += 1
        tmp = zip(vote, alpha)
        tmp = sorted(tmp, key=lambda x: -x[0])
        vote, alpha = zip(*tmp)
        vote = list(vote)
        alpha = list(alpha)

        if vote[0] > (n-i-1) + vote[1]:
            print(alpha[0], i+1)
            break
    else:
        print("TIE")

Submission status: Accepted

Solution code:
while 1:
        n=int(input())
        if not n:break
        vote={"":0}
        cnt=0
        for c in input().split(" "):
            cnt+=1
            if c in vote:vote[c]+=1
            else:vote[c]=1
            v=sorted(vote.items(), key=lambda x: x[1])
            if v[-1][-1]>v[-2][-1]+n-cnt:
                print(*[v[-1][-2],cnt])
                break
            elif n==cnt:print("TIE")

Submission status: Accepted

Solution code:
while 1:
    n=int(input())
    if n==0:
        break
    _dic={}
    dic={}
    s=input().split()
    for p in s:
        if p in _dic:
            _dic[p]+=1
        else:
            _dic[p]=1
            dic[p]=0
    data=sorted(_dic.items(), key=lambda x:-x[1])
    if len(data)==1:
        print(data[0][0],n//2+1)
        continue
    if data[0][1]==data[1][1]:
        print('TIE')
        continue
    i=1
    for p in s:
        dic[p]+=1
        d=sorted(dic.items(), key=lambda x:-x[1])
        if d[0][1]>d[1][1]+(n-i):
            print(d[0][0],i)
            break
        i+=1