POJ2692 Australian Voting序列处理

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2692 Australian Voting序列处理相关的知识,希望对你有一定的参考价值。

Australian Voting
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1828 Accepted: 436

Description

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 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

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

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

Source

Waterloo local 1998.10.17

问题链接POJ2692 Australian Voting
问题简述:(略)
问题分析:投票问题,数据结构略为复杂,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ2692 Australian Voting */

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 20 + 1;
const int N2 = 80;
const int N3 = 1000;
char name[N + 1][N2 + 1];
int vote[N3][N];
int out[N], cnt[N];
char s[1024];

int main()
{
    int n;
    while(~scanf("%d%*c", &n)) {
        for(int i = 1; i <= n; i++) gets(name[i]);

        int num = 0;    // 投票人数
        while(gets(s) && s[0]) {
            int a = 0, cnt = 0;
            for(int i = 0; s[i]; i++)
                if(isdigit(s[i])) {
                    a *= 10;
                    a += s[i] - '0';
                } else {
                    vote[num][cnt++] = a;
                    a = 0;
                }
            vote[num++][cnt++] = a;
        }

        memset(out, 0, sizeof out);
        int flag = 0, res = n, tot = 0;
        while (res > 1) {
            tot = 0;
            memset(cnt, 0, sizeof cnt);
            for (int i = 0; i < num; i++)
                for (int j = 0; j < n; j++)
                    if (out[vote[i][j]] == 0) {
                        cnt[vote[i][j]]++, tot++;
                        break;
                    }
            for (int i = 1; i <= n; i++)
                if (cnt[i] * 2 > tot && out[i] == 0) {
                    printf("%s\\n", name[i]);
                    flag = 1;
                    break;
                }
            if (flag) break;
            int minc = N3 + 1, maxc = 0;
            for (int i = 1; i <= n; i++)
                if (out[i] == 0)
                    minc = min(minc, cnt[i]), maxc = max(maxc, cnt[i]);
            if (minc == maxc) break;
            for (int i = 1; i <= n; i++)
                if (cnt[i] == minc) out[i] = 1, res--;
        }

        if (!flag) {
            for (int i = 1; i <= n; i++)
                if (out[i] == 0) printf("%s\\n", name[i]);
        }
    }

    return 0;
}

以上是关于POJ2692 Australian Voting序列处理的主要内容,如果未能解决你的问题,请参考以下文章

HAVENT原创Australian Business Number (ABN) 验证

P2692 覆盖

2692亿背后,京东智联云以技术守护京东618,助力消费再创新高

CODE[VS] 2692 小明过生日

英文报道:China challenged Australian warships in South China Sea, reports say

2692 假币问题(枚举——数学模型中包含多个变量)