UVa Problem Solution: 10142 - Australian Voting


I just simulate the electing process. The RT is very slow.

Code:
  1. /*************************************************************************
  2.  * Copyright (C) 2008 by liukaipeng                                      *
  3.  * liukaipeng at gmail dot com                                           *
  4.  *************************************************************************/
  5. /* @JUDGE_ID 00000 10142 C "Australian Voting" */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <strings.h>
  10.                
  11. void elect(int ballots[][20], int n, int p, char names[][81])
  12. {
  13.   int i, j;
  14.   int left = p;
  15.   int eliminates[21] = {0};
  16.   while (left > 1) {  
  17.     int votes[21] = {0};
  18.     int allvotes = 0;
  19.     int half;
  20.     int min = 2000;
  21.     int max = 0;
  22.     for (i = 0; i < n; ++i) {
  23.       for (j = 0; j < p; ++j) {
  24.         int v = ballots[i][j];
  25.         if (!eliminates[v]) {
  26.           ++votes[v];
  27.           ++allvotes;
  28.           break;
  29.         }
  30.       }
  31.     }
  32.     half = allvotes / 2;
  33.     for (j = 1; j <= p; ++j)
  34.       if (votes[j] > half) {
  35.         printf("%s/n", names[j]);
  36.         return;
  37.       }
  38.     for (j = 1; j <= p; ++j) {
  39.       if (!eliminates[j] && votes[j] < min)
  40.         min = votes[j];
  41.       if (!eliminates[j] && votes[j] > max)
  42.         max = votes[j];
  43.     }
  44.     if (min == max)
  45.       break;
  46.     for (j = 1; j <= p; ++j) {
  47.       if (votes[j] == min) {
  48.         eliminates[j] = 1;
  49.         --left;
  50.       }
  51.     }
  52.   }
  53.   for (j = 1; j <= p; ++j)
  54.     if (!eliminates[j])
  55.       printf("%s/n", names[j]);
  56. }
  57. int main(int argc, char *argv[])
  58. {
  59. #ifndef ONLINE_JUDGE
  60.   char in[256];
  61.   char out[256];
  62.   strcpy(in, argv[0]);
  63.   strcat(in, ".in");
  64.   freopen(in, "r", stdin);
  65.   strcpy(out, argv[0]);
  66.   strcat(out, ".out");
  67.   freopen(out, "w", stdout);
  68. #endif
  69.   int cases;
  70.   scanf("%d/n", &cases);
  71.   while (cases-- > 0) {
  72.     int persons;
  73.     char names[21][81];
  74.     int ballots[1000][20];
  75.     char buf[100];
  76.     char *tmp;
  77.     int i, j;
  78.     scanf("%d/n", &persons);
  79.     for (i = 1; i <= persons; ++i)
  80.       scanf("%[^/n]/n", names[i]);
  81.     for (i = 0; fgets(buf, 100, stdin) != NULL && buf[0] != '/n'; ++i)
  82.       for (j = 0, tmp = buf; j < persons; ++j, tmp = strchr(tmp, ' ') + 1)
  83.         sscanf(tmp, "%d", &ballots[i][j]);
  84.     elect(ballots, i, persons, names);
  85.     if (cases > 0)
  86.       putchar('/n');
  87.   }
  88.   return 0;
  89. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值