I just simulate the electing process. The RT is very slow.
Code:
- /*************************************************************************
- * Copyright (C) 2008 by liukaipeng *
- * liukaipeng at gmail dot com *
- *************************************************************************/
- /* @JUDGE_ID 00000 10142 C "Australian Voting" */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- void elect(int ballots[][20], int n, int p, char names[][81])
- {
- int i, j;
- int left = p;
- int eliminates[21] = {0};
- while (left > 1) {
- int votes[21] = {0};
- int allvotes = 0;
- int half;
- int min = 2000;
- int max = 0;
- for (i = 0; i < n; ++i) {
- for (j = 0; j < p; ++j) {
- int v = ballots[i][j];
- if (!eliminates[v]) {
- ++votes[v];
- ++allvotes;
- break;
- }
- }
- }
- half = allvotes / 2;
- for (j = 1; j <= p; ++j)
- if (votes[j] > half) {
- printf("%s/n", names[j]);
- return;
- }
- for (j = 1; j <= p; ++j) {
- if (!eliminates[j] && votes[j] < min)
- min = votes[j];
- if (!eliminates[j] && votes[j] > max)
- max = votes[j];
- }
- if (min == max)
- break;
- for (j = 1; j <= p; ++j) {
- if (votes[j] == min) {
- eliminates[j] = 1;
- --left;
- }
- }
- }
- for (j = 1; j <= p; ++j)
- if (!eliminates[j])
- printf("%s/n", names[j]);
- }
- int main(int argc, char *argv[])
- {
- #ifndef ONLINE_JUDGE
- char in[256];
- char out[256];
- strcpy(in, argv[0]);
- strcat(in, ".in");
- freopen(in, "r", stdin);
- strcpy(out, argv[0]);
- strcat(out, ".out");
- freopen(out, "w", stdout);
- #endif
- int cases;
- scanf("%d/n", &cases);
- while (cases-- > 0) {
- int persons;
- char names[21][81];
- int ballots[1000][20];
- char buf[100];
- char *tmp;
- int i, j;
- scanf("%d/n", &persons);
- for (i = 1; i <= persons; ++i)
- scanf("%[^/n]/n", names[i]);
- for (i = 0; fgets(buf, 100, stdin) != NULL && buf[0] != '/n'; ++i)
- for (j = 0, tmp = buf; j < persons; ++j, tmp = strchr(tmp, ' ') + 1)
- sscanf(tmp, "%d", &ballots[i][j]);
- elect(ballots, i, persons, names);
- if (cases > 0)
- putchar('/n');
- }
- return 0;
- }