-
Notifications
You must be signed in to change notification settings - Fork 0
/
HP18.cpp
95 lines (86 loc) · 2.31 KB
/
HP18.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
// https://www.codechef.com/JAN19B/problems/HP18
#include <iostream>
int arr[200007];
void winBob(bool won) {
if (won)
std::cout << "BOB" << "\n";
else
std::cout << "ALICE" << "\n";
}
int main(void) {
int T;
std::cin >> T;
while (T--) {
int N, a, b;
int temp;
std::cin >> N >> a >> b;
for (int i = 0; i < N; i++) {
std::cin >> temp;
arr[i] = temp;
arr[i+1] = 0;
}
int div_bob = 0;
int div_alice = 0;
int div_ba = 0;
// Count bob items
for (int i=0; i < N; i++) {
// std::cout << arr[i] << "\n";
if (arr[i] % a == 0) {
div_bob++;
arr[i] = -1*arr[i]; // Mark as visited
}
}
for (int i=0; i < N; i++) {
// std::cout << arr[i] << "\n";
if (arr[i] % b == 0) {
if (arr[i] < 0) {
div_ba++;
} else {
div_alice++;
}
}
}
// std::cout << div_bob << " " << div_alice << "\n";
// If Alice has some elements left Alice removes them
// If Bob still has some elements, bob wins
// Else Alice win
// Else Bob wins
// if (a==b) {
// // If a==b bob removes everything, wins the round
// winBob(true);
// continue;
// } else {
// Turn: 1 : Bob
if (div_bob <= 0) {
// bob has none
// alice wins
winBob(false);
continue;
}
if (div_ba > 0) {
// If there are common elements
// bob removes them first
// Optimal for bob
div_bob -= div_ba;
} else {
div_bob--;
}
// Turn 2: Alice
if (div_alice <= 0) {
// After the commons are removed,
// Alice has zero => bob wins
winBob(true);
continue;
}
// }
// So presently alice has not zero elems
// If alice has more she wins
if (div_alice > div_bob) {
winBob(false);
continue;
} else {
winBob(true);
continue;
}
}
}