UVA 10142 Australian Voting

38 篇文章 0 订阅

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
Sample Output
John Doe

题意:
澳大利亚进行选举,有n个候选人,每个公民对每个候选人有一个期望的优先级, 选举时,先按第一优先级分配选票,得票最少的候选人淘汰,对那些将第一名投给得票最少的候选人的选票,将按选票上的优先级投给剩余候选人,直到某人获得50%或以上的选票,或者剩下的人得票相同,输出选举结果。


#include <iostream>
#include <sstream>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
typedef queue<int> ballot;
vector <ballot> ballotlist;
vector<string> nameList;
bool eliminated[20];
int numberOfVotes[20],highest,lowest;
void initial()
{
    nameList.clear();
    ballotlist.clear();
    memset(eliminated,false,sizeof(eliminated));
    memset(numberOfVotes,0,sizeof(numberOfVotes));
}
void readNames()
{
    string name;
    int numberOfNames;
    cin>>numberOfNames;
    getline(cin,name);//getchar();
    for(int i=0;i<numberOfNames;i++)
    {
        getline(cin,name);
        nameList.push_back(name);
    }
}
void readBallots()
{
    int id;
    string line;
    ballot b;
    getline(cin,line);
    while(line!="")
    {
        //getchar();
        stringstream ss(line);
        while(!b.empty())
        {
            b.pop();
        }
        while(ss>>id)
        {
            b.push(id-1);//b.push(id)
        }
        ballotlist.push_back(b);
        getline(cin,line);
    }
}
bool hasWinner()
{
    return 2*highest > ballotlist.size();
}
void Eliminated()
{
    for(int i=0;i<20;i++)
    {
        if(numberOfVotes[i]==lowest)
            eliminated[i]=true;
    }
}
void computing()
{
    int id;
    highest=0;
    lowest=ballotlist.size();
    for(int i=0;i<ballotlist.size();i++)
    {
        id=ballotlist[i].front();//id=ballotlist[i].top();
        numberOfVotes[id]++;
        if(numberOfVotes[id]>highest)
            highest=numberOfVotes[id];
        if(numberOfVotes[id]<lowest)
            lowest=numberOfVotes[id];
    }
    while(lowest<highest)
    {
        if(hasWinner())
            break;
        else
        {
            Eliminated();
            for(int i=0;i<ballotlist.size();i++)
            {
                id=ballotlist[i].front();
                if(eliminated[id])
                {
                    while(eliminated[id])
                    {
                        ballotlist[i].pop();
                        id = ballotlist[i].front();
                    }
                    numberOfVotes[id]++;
                    if (numberOfVotes[id] > highest)
                    {
                        highest = numberOfVotes[id];
                    }
                }
            }
            lowest=ballotlist.size();
            for(int i=0;i<ballotlist.size();i++)
            {
                id=ballotlist[i].front();
                if((!eliminated[id])&&numberOfVotes[id]<lowest)
                    lowest=numberOfVotes[id];
            }
        }
    }
}
void outResult()
{
    for(int i=0;i<nameList.size();i++)
    {
        if(numberOfVotes[i]==highest)
            cout<<nameList[i]<<endl;
    }
}
int main()
{
    int Cases;
    cin >> Cases;
    for (int i = 0; i < Cases; i++)
    {
        initial();
        readNames();
        readBallots();
        computing();
        if (i > 0)
        {
            cout << endl;
        }
        outResult();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值