Look for the Winner!
Time limit2sMemory limit512 MB
Given votes counted in order, report the earliest prefix after which one candidate's lead cannot be overtaken from remaining votes, or TIE.
- Level
Easy3 of 10
- Topics
- Array, Simulation, Greedy
- Solved
- No attempts yet
Problem
The citizens of TKB City love elections and vote counting. Today they hold an election for the next chairperson of the electoral commission. Voting has just closed and counting is about to start. The citizens want to know the winner as early as possible while the votes are counted.
The candidate with the most votes becomes the next chairperson. Suppose there are three candidates A, B, and C and ten votes. Suppose also that six of the ten votes have been counted and the counts of A, B, and C are four, one, and one. At this moment every candidate can still receive four more votes, so anyone can still win. If the seventh counted vote is cast for A, then A is certain to win: A already has five votes and B or C can end with at most four. In this example the citizens know the winner as soon as the seventh vote is counted.
Write a program that reads the counted votes one by one, finds the winner, and reports after how many votes the winner becomes certain.
Input
The input has at most 1500 datasets. Each dataset is two lines in this format.
n
c1 c2 ... cn
The first line holds , the number of votes, a positive integer no greater than 100. The second line holds the votes separated by single spaces. Each () is one uppercase letter from A to Z and names the candidate who received the -th vote. Counting runs in the given order from to .
Assume that at least two candidates stand, even when every vote is cast for one candidate.
A line containing a single zero marks the end of the input.
Output
For each dataset print one line. If the election does not end in a tie, print the uppercase letter of the winner and an integer separated by one space, where is the number of counted votes after which the winner is certain. If the largest number of votes is shared by two or more candidates, print TIE instead.
The winner is certain once no distribution of the remaining votes lets another candidate reach the leader's count. Since at least two candidates stand, a candidate with no votes so far can still receive the remaining votes.