UVA10142 Australian Voting序列处理

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10142 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
Sample Output
John Doe

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

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

/* UVA10142 Australian Voting */

#include <bits/stdc++.h>

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 t;
    scanf("%d", &t);
    while(t--) {
        int n;
        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]);
        }
        if (t) printf("\\n");
    }

    return 0;
}

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

HAVENT原创Australian Business Number (ABN) 验证

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

SQL删除存在值的行和具有该值的后续列

LeetCode | 双指针

试图从父实体java类中删除记录

如何在 moshi (kotlin) 中解析 LinkedHashMap