24h購物| | PChome| 登入
2012-09-13 15:20:19| 人氣954| 回應0 | 上一篇 | 下一篇

[UVA][模擬] 10142 - Australian Voting

推薦 0 收藏 0 轉貼0 訂閱站台

Problem D - Australian Voting

Australian ballots require that the voter rank the candidates in order of choice. Initially only the first choices are counted and if one candidate receives more than 50% of the vote, that candidate is elected. If no candidate receives more than 50%, all candidates tied for the lowest number of votes are eliminated. Ballots ranking these candidates first are recounted in favour of their highest ranked candidate who has not been eliminated. This process continues [that is, the lowest candidate is eliminated and each ballot is counted in favour of its ranked non-eliminated candidate] until one candidate receives more than 50% of the vote or until all candidates are tied.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

The first line of input is an integer n <= 20 indicating the number of candidates. The next n lines consist of the names of the candidates in order. Names may be up to 80 characters in length and may contain any printable characters. Up to 1000 lines follow; each contains the contents of a ballot. That is, each contains the numbers from 1 to n in some order. The first number indicates the candidate of first choice; the second number indicates candidate of second choice, and so on.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

The Output consists of either a single line containing the name of the winner or several lines containing the names of the candidates who tied.

Sample Input

1

3
John Doe
Jane Smith
Sirhan Sirhan
1 2 3
2 1 3
2 3 1
1 2 3
3 1 2

Output for Sample Input

John Doe



#include <stdio.h>
#include <iostream>
#include <sstream>
using namespace std;
int main() {
int t,n, i, j;
scanf("%d", &t);
getchar();
while(t--) {
scanf("%d", &n);
getchar();
string name[25], line;
int vote[1005][25];
for(i = 1; i <= n; i++)
getline(cin, name[i]);
int m = 0;
while(getline(cin, line)) {
if(line == "") break;
stringstream sin;
sin << line;
int idx = 0;
while(sin >> vote[m][idx])
idx++;
m++;
}
int Idx[1005] = {}, fire[25] = {};
while(1) {
int p[25] = {}, tot = 0;
for(i = 0; i < m; i++) {
while(Idx[i] < n && fire[vote[i][Idx[i]]])
Idx[i]++;
if(Idx[i] < n) {
p[vote[i][Idx[i]]]++;
tot++;
}
}
int half = tot/2 + tot%2;
int mx = 0, mi = 0xffff;
for(i = 1; i <= n; i++) {
if(!fire[i]) {
if(p[i] > mx)
mx = p[i];
if(p[i] < mi)
mi = p[i];
}
}
if(mx == mi || mx >= half) {
for(i = 1; i <= n; i++)
if(p[i] == mx)
cout << name[i] << endl;
break;
}
for(i = 1; i <= n; i++)
if(p[i] == mi)
fire[i] = 1;
}
if(t)
puts("");
}
return 0;
}

台長: Morris
人氣(954) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][骰子翻轉比對] 253 - Cube painting
此分類上一篇:[UVA][硬幣問題, dp背包解] 166 - Making Change

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文