-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10142.cpp
109 lines (97 loc) · 2.97 KB
/
10142.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#define Vote int
struct People {
Vote v[20], *p;
};
void parse_vote(People &p, std::string line)
{
int x = 0, i = 0, j = 1;
while (j < line.size()) {
if (line[j] == ' ') {
p.v[x++] = std::stoi(line.substr(i, j - i)) - 1;
i = j + 1;
j = i + 1;
}
else {
++j;
}
}
p.v[x++] = std::stoi(line.substr(i, j - i)) - 1;
p.p = p.v;
}
void judge(Vote *sum, bool *eliminated, int num_candidate, int num_people,
int *max_vote, int *min_vote)
{
*min_vote = num_people;
*max_vote = 0;
for (int i = 0; i < num_candidate; ++i) {
if (!eliminated[i]) {
*min_vote = std::min(*min_vote, sum[i]);
*max_vote = std::max(*max_vote, sum[i]);
}
}
}
void update_sum(Vote *sum, bool *eliminated, People *people, int num_people)
{
for (int i = 0; i < num_people; ++i)
if (eliminated[*people[i].p]) {
--sum[*people[i].p];
while (eliminated[*people[i].p])
++people[i].p;
++sum[*people[i].p];
}
}
int main(void)
{
int num_cases, num_candidate, num_people, min_vote, max_vote;
std::string buffer, candidates[20];
People people[1000];
Vote sum[20];
bool eliminated[20];
std::getline(std::cin, buffer);
num_cases = std::stoi(buffer);
std::getline(std::cin, buffer);
while (num_cases--) {
std::getline(std::cin, buffer);
num_candidate = std::stoi(buffer);
for (int i = 0; i < num_candidate; ++i) {
std::getline(std::cin, candidates[i]);
}
num_people = 0;
while (1) {
std::getline(std::cin, buffer);
if (!buffer.size()) break;
parse_vote(people[num_people++], buffer);
}
memset(eliminated, false, sizeof(eliminated));
memset(sum, 0, sizeof(sum));
for (int i = 0; i < num_people; ++i)
++sum[*people[i].p];
for (int k = 0; k < num_candidate; ++k) {
judge(sum, eliminated, num_candidate, num_people, &max_vote,
&min_vote);
if (max_vote * 2 > num_people || min_vote == max_vote) break;
for (int i = 0; i < num_candidate; ++i)
if (sum[i] == min_vote) eliminated[i] = true;
update_sum(sum, eliminated, people, num_people);
}
if (min_vote != max_vote) {
for (int i = 0; i < num_candidate; ++i)
if (sum[i] == max_vote) {
std::cout << candidates[i] << std::endl;
break;
}
}
else {
for (int i = 0; i < num_candidate; ++i)
if (sum[i] == min_vote) {
std::cout << candidates[i] << std::endl;
}
}
if (num_cases) std::cout << std::endl;
}
return 0;
}