思路&题解

PC/UVA 110108/10142
模拟。

//author: CHC
//First Edit Time: 2014-01-14 14:37
//Last Edit Time: 2014-01-15 22:03
//Filename:1.cpp
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int n,t,ns;
char s[30][100];
char ts[1000];
vector <int> vote[1010];
vector <int> ::iterator it;
int ha[30];
void getnum(int kn)
{
int i=0;
while(ts[i])
{
while(ts[i]==' '&&ts[i])i++;
int tt=0;
while(ts[i]!=' '&&ts[i]){
tt=tt*10+ts[i]-'0';
++i;
}
if(tt!=0)vote[kn].push_back(tt);
}
}
void voted()
{
for(int i=0;i<ns;i++){
ha[vote[i][0]]++;
}
while(1){
int ping=1,min=1010,first=-1;
for(int i=0;i<n;i++){
if(ha[i+1]>ns/2){
printf("%s\n",s[i]);
return ;
}
/*
if(ha[i+1]>=0&&first==-1)first=ha[i+1];
if(first>=0&&ha[i+1]>=0&&ha[i+1]!=first)ping=0;
if(ha[i+1]>=0&&ha[i+1]<min)min=ha[i+1];
*/
///*
if(ha[i+1]>=0){
if(min!=1010&&min!=ha[i+1])ping=0;
if(min>ha[i+1]) min=ha[i+1];
}
//*/
}
if(ping){
for(int i=0;i<n;i++)
if(ha[i+1]>=0)
printf("%s\n",s[i]);
return ;
}
for(int i=0;i<ns;i++){
int flag= (min==ha[vote[i][0]]);
for(it=vote[i].begin();it!=vote[i].end();it++)
if(ha[*it]==min) {
vote[i].erase(it);
it--;
}
if(flag)
ha[vote[i][0]]++;
}
for(int i=0;i<n;i++)
if(ha[i+1]==min)ha[i+1]=-1;
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
getchar();

for(int i=0;i<1010;i++)vote[i].clear();
memset(ha,0,sizeof(ha));

for(int i=0;i<n;i++){
gets(s[i]);
}
ns=0;
while(gets(ts)!=NULL&&strcmp(ts,"")!=0){
getnum(ns);
++ns;
}
voted();
if(t!=0)puts("");
}
return 0;
}

Australian Voting

Australian ballots require that voters rank all the candidates in order of choice. Initially only the first choices are counted, and if one candidate receives more than 50% of the vote then that candidate is elected. However, 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 favor of their highest-ranked non-eliminated candidate. This process of eliminating the weakest candidates and counting their ballots in favor of the preferred non-eliminated candidate continues until one candidate receives more than 50% of the vote, or until all remaining candidates are tied.

Input

The input begins with a single positive integer on a line by itself indicating the number of cases following, each as described below. This line is followed by a blank line. There is also a blank line between two consecutive inputs.
The first line of each case is an integer n20 indicating the number of candidates. The next n lines consist of the names of the candidates in order, each up to 80 characters in length and containing any printable characters. Up to 1,000 lines follow, each containing the contents of a ballot. Each ballot 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

The output of each test case consists of either a single line containing the name of the winner or several lines containing the names of all candidates who are tied. The output of each two consecutive cases are separated by a blank line.

Sample Input

1  
3  
John Doe  
Jane Smith  
Jane Austen  
1 2 3  
2 1 3  
2 3 1  
1 2 3  
3 1 2  

Sample Output

John Doe