#include "mushrooms.h"
#include <bits/stdc++.h>
std::vector<int> a[2];
int b[2];
int id, n;
void query_1(){
if(id >= n) return;
std::vector<int> m {0, id};
int c = use_machine(m);
a[c].push_back(id);
id++;
}
void query_2a(){
if(id >= n) return;
if(a[0].size() <= 1 && a[1].size() <= 1 || id == n - 1){ query_1(); return; }
int majority = a[0].size() >= a[1].size() ? 0 : 1;
std::vector<int> m = {a[majority][0], id, a[majority][1], id + 1};
int c = use_machine(m);
id += 2;
a[majority ^ (c / 2)].push_back(m[1]);
a[majority ^ (c % 2)].push_back(m[3]);
}
void query_2b(){ // 7 / 3 bits per query
if(id >= n) return;
if(a[0].size() <= 3 && a[1].size() <= 3 || n - id <= 7){ query_2a(); return; }
int majority = a[0].size() >= a[1].size() ? 0 : 1;
std::vector<int> x {id + 3, id + 4, id + 5, id + 6};
std::vector<int> m1{id + 0, a[majority][0], x[0], a[majority][1], x[1], a[majority][2], x[2], a[majority][3]};
std::vector<int> m2{id + 1, a[majority][0], x[0], a[majority][1], x[1], a[majority][2], x[3], a[majority][3]};
std::vector<int> m3{id + 2, a[majority][0], x[0], a[majority][1], x[2], a[majority][2], x[3], a[majority][3]};
int c1 = use_machine(m1), c2 = use_machine(m2), c3 = use_machine(m3);
a[majority ^ (c1 % 2)].push_back(id + 0);
a[majority ^ (c2 % 2)].push_back(id + 1);
a[majority ^ (c3 % 2)].push_back(id + 2);
for(int mask = 0; mask < 16; mask++){
std::vector<int> c;
for(int j = 0; j < 4; j++){
c.push_back((mask & (1 << j)) > 0);
}
if(c[0] + c[1] + c[2] == c1 / 2 && c[0] + c[1] + c[3] == c2 / 2 && c[0] + c[2] + c[3] == c3 / 2){
for(int j = 0; j < 4; j++){
a[majority ^ c[j]].push_back(x[j]);
}
}
}
id += 7;
}
void query_2c(){ // 5 / 2 bits per query
if(id >= n) return;
int majority = a[0].size() >= a[1].size() ? 0 : 1;
if(a[majority].size() < 3 || a[!majority].size() < 2 || n - id <= 5){ query_2a(); return; }
std::vector<int> x {id + 2, id + 3, id + 4};
std::vector<int> m1{id + 0, a[majority][0], x[0], a[majority][1], x[1], a[majority][2], a[!majority][0], x[2], a[!majority][1]};
std::vector<int> m2{id + 1, a[majority][0], x[0], a[majority][1], x[2], a[majority][2]};
int c1 = use_machine(m1), c2 = use_machine(m2); --c1;
a[majority ^ (c1 % 2)].push_back(id + 0);
a[majority ^ (c2 % 2)].push_back(id + 1);
c1 /= 2, c2 /= 2;
std::vector<int> c;
if(c1 == 3) c = {1, 1, 0};
else if(c1 == 2) {
if(c2 == 2) c = {1, 1, 1};
else if(c2 == 1) c = {1, 0, 0};
else c = {0, 1, 0};
}else if(c1 == 1){
if(c2 == 2) c = {1, 0, 1};
else if(c2 == 1) c = {0, 1, 1};
else c = {0, 0, 0};
}else{
c = {0, 0, 1};
}
for(int i = 0; i < 3; i++){
a[majority ^ c[i]].push_back(x[i]);
}
id += 5;
}
void query_3(){
if(id >= n) return;
if(a[0].size() <= 1 && a[1].size() <= 1){ query_1(); return; }
int majority = a[0].size() >= a[1].size() ? 0 : 1;
int k = a[majority].size(); k = std::min(k, n - id);
std::vector<int> m;
for(int i = id; i < id + k; i++){
m.push_back(a[majority][i - id]); m.push_back(i);
}
int c = use_machine(m);
id += k;
a[majority ^ (c % 2)].push_back(m.back());
b[majority ^ 1] += c / 2;
b[majority] += (k - 1) - c / 2;
}
int count_mushrooms(int N) {
a[0].clear(); a[1].clear(); b[0] = b[1] = 0;
n = N; a[0].push_back(0); id = 1;
if(n <= 1000){
while(id < n) query_3();
return a[0].size() + b[0];
}
const int SQ = 196;
while(id < SQ) query_2c();
while(id < n) query_3();
return a[0].size() + b[0];
}
Compilation message
mushrooms.cpp: In function 'void query_2a()':
mushrooms.cpp:18:22: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
18 | if(a[0].size() <= 1 && a[1].size() <= 1 || id == n - 1){ query_1(); return; }
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
mushrooms.cpp: In function 'void query_2b()':
mushrooms.cpp:29:22: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
29 | if(a[0].size() <= 3 && a[1].size() <= 3 || n - id <= 7){ query_2a(); return; }
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
2 ms |
312 KB |
Output is correct |
7 |
Correct |
6 ms |
208 KB |
Output is correct |
8 |
Correct |
5 ms |
312 KB |
Output is correct |
9 |
Correct |
6 ms |
208 KB |
Output is correct |
10 |
Correct |
6 ms |
336 KB |
Output is correct |
11 |
Correct |
7 ms |
316 KB |
Output is correct |
12 |
Correct |
6 ms |
208 KB |
Output is correct |
13 |
Correct |
4 ms |
316 KB |
Output is correct |
14 |
Correct |
3 ms |
312 KB |
Output is correct |
15 |
Correct |
5 ms |
208 KB |
Output is correct |
16 |
Correct |
5 ms |
208 KB |
Output is correct |
17 |
Correct |
3 ms |
208 KB |
Output is correct |
18 |
Correct |
5 ms |
292 KB |
Output is correct |
19 |
Correct |
5 ms |
312 KB |
Output is correct |
20 |
Correct |
5 ms |
208 KB |
Output is correct |
21 |
Correct |
5 ms |
208 KB |
Output is correct |
22 |
Correct |
5 ms |
312 KB |
Output is correct |
23 |
Correct |
4 ms |
208 KB |
Output is correct |
24 |
Correct |
3 ms |
344 KB |
Output is correct |
25 |
Correct |
5 ms |
208 KB |
Output is correct |
26 |
Correct |
5 ms |
304 KB |
Output is correct |
27 |
Correct |
5 ms |
208 KB |
Output is correct |
28 |
Correct |
5 ms |
328 KB |
Output is correct |
29 |
Correct |
4 ms |
208 KB |
Output is correct |
30 |
Correct |
5 ms |
208 KB |
Output is correct |
31 |
Correct |
6 ms |
312 KB |
Output is correct |
32 |
Correct |
5 ms |
320 KB |
Output is correct |
33 |
Correct |
6 ms |
208 KB |
Output is correct |
34 |
Correct |
5 ms |
208 KB |
Output is correct |
35 |
Correct |
5 ms |
208 KB |
Output is correct |
36 |
Correct |
5 ms |
208 KB |
Output is correct |
37 |
Correct |
5 ms |
208 KB |
Output is correct |
38 |
Correct |
5 ms |
208 KB |
Output is correct |
39 |
Correct |
5 ms |
208 KB |
Output is correct |
40 |
Correct |
7 ms |
208 KB |
Output is correct |
41 |
Correct |
6 ms |
208 KB |
Output is correct |
42 |
Correct |
5 ms |
208 KB |
Output is correct |
43 |
Correct |
6 ms |
336 KB |
Output is correct |
44 |
Correct |
5 ms |
316 KB |
Output is correct |
45 |
Correct |
5 ms |
208 KB |
Output is correct |
46 |
Correct |
4 ms |
328 KB |
Output is correct |
47 |
Correct |
6 ms |
208 KB |
Output is correct |
48 |
Correct |
5 ms |
324 KB |
Output is correct |
49 |
Correct |
6 ms |
320 KB |
Output is correct |
50 |
Correct |
5 ms |
308 KB |
Output is correct |
51 |
Correct |
5 ms |
208 KB |
Output is correct |
52 |
Correct |
7 ms |
208 KB |
Output is correct |
53 |
Correct |
5 ms |
208 KB |
Output is correct |
54 |
Correct |
7 ms |
312 KB |
Output is correct |
55 |
Correct |
5 ms |
308 KB |
Output is correct |
56 |
Correct |
4 ms |
324 KB |
Output is correct |
57 |
Correct |
4 ms |
324 KB |
Output is correct |
58 |
Correct |
5 ms |
208 KB |
Output is correct |
59 |
Correct |
5 ms |
316 KB |
Output is correct |
60 |
Correct |
7 ms |
320 KB |
Output is correct |
61 |
Correct |
6 ms |
208 KB |
Output is correct |
62 |
Correct |
0 ms |
208 KB |
Output is correct |
63 |
Correct |
1 ms |
296 KB |
Output is correct |
64 |
Correct |
0 ms |
208 KB |
Output is correct |
65 |
Correct |
0 ms |
208 KB |
Output is correct |
66 |
Correct |
0 ms |
208 KB |
Output is correct |
67 |
Correct |
0 ms |
208 KB |
Output is correct |
68 |
Correct |
0 ms |
208 KB |
Output is correct |
69 |
Correct |
1 ms |
208 KB |
Output is correct |
70 |
Correct |
0 ms |
208 KB |
Output is correct |
71 |
Correct |
0 ms |
208 KB |
Output is correct |
72 |
Correct |
0 ms |
208 KB |
Output is correct |
73 |
Correct |
0 ms |
208 KB |
Output is correct |
74 |
Correct |
0 ms |
208 KB |
Output is correct |
75 |
Correct |
1 ms |
208 KB |
Output is correct |
76 |
Correct |
0 ms |
208 KB |
Output is correct |
77 |
Correct |
0 ms |
208 KB |
Output is correct |
78 |
Correct |
0 ms |
208 KB |
Output is correct |
79 |
Correct |
0 ms |
208 KB |
Output is correct |
80 |
Correct |
1 ms |
208 KB |
Output is correct |
81 |
Correct |
0 ms |
208 KB |
Output is correct |
82 |
Correct |
0 ms |
208 KB |
Output is correct |
83 |
Correct |
0 ms |
208 KB |
Output is correct |
84 |
Correct |
0 ms |
208 KB |
Output is correct |
85 |
Correct |
0 ms |
208 KB |
Output is correct |
86 |
Correct |
1 ms |
208 KB |
Output is correct |
87 |
Correct |
0 ms |
208 KB |
Output is correct |
88 |
Correct |
0 ms |
208 KB |
Output is correct |
89 |
Correct |
1 ms |
208 KB |
Output is correct |
90 |
Correct |
0 ms |
208 KB |
Output is correct |
91 |
Correct |
0 ms |
208 KB |
Output is correct |