1859. Long March Voting

单点时限: 2.0 sec

内存限制: 256 MB

Instant run-off voting is a system for selecting the most preferred candidate in an election. At the beginning of the process, each voter ranks the candidates from most preferred to least preferred. A series of automated voting rounds are then held to determine the overall winner.

In each round, each voter casts a single vote for his most preferred remaining candidate. If a candidate receives strictly more than 50% of the votes cast in that round, that candidate is declared the winner of the election. Otherwise, the candidate with the fewest votes in that round is eliminated, and another round is held. If multiple candidates are tied for the least number of votes, they are all eliminated. If all the candidates are eliminated, the election ends without a winner.

You are given the preferences of the voters in an election, and you must determine the outcome. There are M candidates numbered 0 to M-1, inclusive. The preferences are given in N lines, where each element describes the preferences of a single voter. This is a permutation of the digits 0 to M-1 in decreasing order of preference. In other words, the first digit is the voter’s most preferred candidate, the second digit is his second most preferred candidate, and so on.

输入格式

There are several test cases,each test case begins with a integer N(1<=N<=50),means there are N voters.The next N lines,each contains a string with the same lenth M(1<=M<=10).Each element of a voter will be a permutation of the digits between 0 and M-1. There is a blank line between each test case.

输出格式

For each test case,output the number of the candidate who wins the election, or -1 if the election ends without a winner.

样例

Input
5
120
102
210
021
012
8
3120
3012
1032
3120
2031
2103
1230
1230
Output
1
-1
Hint:
Case 1:
Nobody gets an absolute majority in the first round and candidate 2 is eliminated. Candidate 1 then receives 3 votes in the next round, giving an absolute majority.
Case 2:
Candidate 0 is eliminated in the first round of voting. Candidate 2 is eliminated in the second round. In the third round, candidates 1 and 3 get 4 votes each. Neither candidate receives an absolute majority, and they are both eliminated for having the least number of votes, so the election ends without a winner.

8 人解决,30 人已尝试。

9 份提交通过,共有 85 份提交。

8.1 EMB 奖励。

创建: 16 年前.

修改: 6 年,7 月前.

最后提交: 3 年,4 月前.

来源: N/A

题目标签