1.6.8 澳大利亚投票 Australian Voting

该博客主要介绍了如何模拟澳大利亚的投票过程,包括理解PC/UVA 110108/10142问题的背景和解决策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 n$ \le$20 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值