UVa Problem Solution: 10142 - Australian Voting
2008-10-24 15:15
513 查看
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;
}
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;
}
相关文章推荐
- UVa Problem Solution: 10267 - Graphical Editor
- UVa Problem Solution: 10252 - Common Permutation
- UVa Problem Solution: 10138 - CDVII
- UVa Problem Solution: 10018 - Reverse and Add
- UVa Problem Solution: 10213 - How Many Pieces of Land?
- UVa Problem Solution: 10033 - Interpreter
- UVa Problem Solution: 10149 - Yahtzee
- UVa Problem Solution: 850 - Crypt Kicker II
- UVa Problem Solution: 120 - Stacks of Flapjacks
- UVa Problem Solution: 701 - The Archaeologist's Dilemma
- UVa Problem Solution: 10198 - Counting
- UVa Problem Solution: 10247 - Complete Tree Labeling
- UVa Problem Solution: 10139 - Factovisors
- UVa Problem Solution: 861 - Little Bishops
- UVa Problem Solution: 10196 - Check The Check
- UVa Problem Solution: 10082 - WERTYU
- UVa Problem Solution: 10188 - Automated Judge Script
- UVa Problem Solution: 10037 - Bridge
- UVa Problem Solution: 10152 - ShellSort
- UVa Problem Solution: 10127 - Ones