UV 10142 running Alian voting (simulation)

Source: Internet
Author: User

The Australian voting system requires voters to sort all candidates by the degree of willingness to choose. One vote is a sort. The preferences of each vote are counted at the beginning. If a candidate wins more than 50% votes, he will directly win. Otherwise, all candidates tied for the lowest position will be out, however, those votes that rank the most out-of-office candidates in the first place will be re-counted as the highest-ranking non-out-of-office candidates. This screening process continues until a candidate receives more than 50% of the votes, or all candidates receive the same votes.

# Include <cstdio> # include <cstring> # include <iostream> # include <cmath> # include <algorithm> using namespace STD; const int n = 50; # define INF 0x3fffffffint vote [1050] [N]; int out [N], CNT [N]; char name [N] [100]; int num; int main () {int t, n, I, j; char STR [1000]; scanf ("% d", & T); While (t --) {scanf ("% d ", & N); getchar (); for (I = 1; I <= N; I ++) gets (name [I]); num = 0; while (gets (STR )! = NULL) {If (! Strcmp (STR, "") break; int Len = strlen (STR); int S = 0, K = 0; for (I = 0; I <Len; I ++) {If (STR [I]> = '0' & STR [I] <= '9 ') S = S * 10 + STR [I]-'0'; else {vote [num] [k ++] = s; S = 0 ;}} vote [num] [k ++] = s; num ++;} memset (Out, 0, sizeof (out); int flag = 0, rest = n, total = 0; while (rest> 1) // The number of unelected candidates exceeds 1 {Total = 0; // total number of votes memset (CNT, 0, sizeof (CNT )); for (I = 0; I <num; I ++) {for (j = 0; j <N; j ++) {If (! Out [vote [I] [J]) {CNT [vote [I] [J] ++; Total ++; break ;}}for (I = 1; I <= N; I ++) if (CNT [I] * 2> total &&! Out [I]) {printf ("% s \ n", name [I]); flag = 1; break;} If (FLAG) break; int mmin = inf, mMax = 0; for (I = 1; I <= N; I ++) {If (! Out [I]) {mmin = min (mmin, CNT [I]); mMax = max (mMax, CNT [I]) ;}} if (mmin = mMax) break; for (I = 1; I <= N; I ++) if (CNT [I] = mmin) {out [I] = 1; rest --;}} if (! Flag) {for (I = 1; I <= N; I ++) if (! Out [I]) printf ("% s \ n", name [I]);} If (T> 0) printf ("\ n") ;}return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.