#include "mushrooms.h"
#include <bits/stdc++.h>
using namespace std;
bool swapped = false;
int i = 1;
int just_count_A = 0;
int just_count_B = 0;
vector<int> A = { 0 };
vector<int> B = { };
int calc_pivots_size(int n) {
return 1 + (3*sqrt(n)/4);
}
void make_swap() {
swapped = not swapped;
swap(just_count_A, just_count_B);
swap(A, B);
}
bool decide_swap() {
if (A.size() < B.size()) {
make_swap();
}
return true;
}
int handle_parity(int parity) {
(parity == 0 ? A : B).push_back(i);
return 1;
}
int handle_pair(int raw_info) {
int even024 = raw_info - (raw_info%2);
if (even024 == 2) {
throw invalid_argument("conflict: need more info to handle this case");
}
(even024 == 0 ? A : B).push_back(i);
(even024 == 0 ? A : B).push_back(i+1);
return 2;
}
int handle_conflict_pair(int raw_info) {
int flag3b = raw_info - 1;
(flag3b & 0b100 ? A : B).push_back(i);
(flag3b & 0b100 ? B : A).push_back(i+1);
(flag3b & 0b010 ? B : A).push_back(i+2);
(flag3b & 0b001 ? B : A).push_back(i+3);
return 4;
}
void get_pivots(int n) {
int info;
int size = calc_pivots_size(n);
while (decide_swap() and (int)A.size() < size and i+5 < n) {
if (A.size() < 2) {
i += handle_parity(use_machine({ i, A[0] }));
} else if (A.size() < 3 or B.size() < 2) {
info = use_machine({ i, A[0], i+1, A[1] });
i += handle_parity(info%2);
i += handle_parity(info/2);
} else {
info = use_machine({ i, A[0], i+1, A[1], i+2, A[2] });
i += handle_parity(info%2);
try {
i += handle_pair(info);
} catch (const invalid_argument &) {
info = use_machine({ B[0], i, B[1], A[0], i+1, A[1], i+2, A[2], i+3 });
i += handle_conflict_pair(info);
}
}
}
}
vector<int> make_sample(int size) {
vector<int> sample = { };
for (int j=0; j<size; j++) {
sample.insert(sample.end(), { i+j, A[j] });
}
return sample;
}
void count_the_rest(int n) {
while (decide_swap() and i < n) {
int test_size = min((int)A.size(), n-i);
int info = use_machine(make_sample(test_size));
i += handle_parity(info%2);
i += test_size-1;
just_count_A += (test_size-1) - (info/2);
just_count_B += info/2;
}
}
int count_mushrooms(int n) {
get_pivots(n);
count_the_rest(n);
if (swapped) {
make_swap();
}
return A.size() + just_count_A;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
7 ms |
384 KB |
Output is correct |
8 |
Correct |
8 ms |
256 KB |
Output is correct |
9 |
Correct |
9 ms |
384 KB |
Output is correct |
10 |
Correct |
9 ms |
384 KB |
Output is correct |
11 |
Correct |
9 ms |
384 KB |
Output is correct |
12 |
Correct |
11 ms |
384 KB |
Output is correct |
13 |
Correct |
8 ms |
384 KB |
Output is correct |
14 |
Correct |
7 ms |
416 KB |
Output is correct |
15 |
Correct |
9 ms |
384 KB |
Output is correct |
16 |
Correct |
9 ms |
384 KB |
Output is correct |
17 |
Correct |
6 ms |
384 KB |
Output is correct |
18 |
Correct |
10 ms |
384 KB |
Output is correct |
19 |
Correct |
10 ms |
384 KB |
Output is correct |
20 |
Correct |
10 ms |
384 KB |
Output is correct |
21 |
Correct |
9 ms |
384 KB |
Output is correct |
22 |
Correct |
9 ms |
384 KB |
Output is correct |
23 |
Correct |
8 ms |
384 KB |
Output is correct |
24 |
Correct |
6 ms |
384 KB |
Output is correct |
25 |
Correct |
9 ms |
384 KB |
Output is correct |
26 |
Correct |
11 ms |
384 KB |
Output is correct |
27 |
Correct |
11 ms |
384 KB |
Output is correct |
28 |
Correct |
9 ms |
376 KB |
Output is correct |
29 |
Correct |
10 ms |
432 KB |
Output is correct |
30 |
Correct |
9 ms |
384 KB |
Output is correct |
31 |
Correct |
10 ms |
384 KB |
Output is correct |
32 |
Correct |
9 ms |
384 KB |
Output is correct |
33 |
Correct |
10 ms |
384 KB |
Output is correct |
34 |
Correct |
11 ms |
384 KB |
Output is correct |
35 |
Correct |
9 ms |
384 KB |
Output is correct |
36 |
Correct |
10 ms |
384 KB |
Output is correct |
37 |
Correct |
11 ms |
384 KB |
Output is correct |
38 |
Correct |
8 ms |
376 KB |
Output is correct |
39 |
Correct |
11 ms |
384 KB |
Output is correct |
40 |
Correct |
9 ms |
384 KB |
Output is correct |
41 |
Correct |
11 ms |
384 KB |
Output is correct |
42 |
Correct |
9 ms |
384 KB |
Output is correct |
43 |
Correct |
10 ms |
384 KB |
Output is correct |
44 |
Correct |
10 ms |
384 KB |
Output is correct |
45 |
Correct |
10 ms |
384 KB |
Output is correct |
46 |
Correct |
10 ms |
384 KB |
Output is correct |
47 |
Correct |
9 ms |
384 KB |
Output is correct |
48 |
Correct |
9 ms |
384 KB |
Output is correct |
49 |
Correct |
10 ms |
384 KB |
Output is correct |
50 |
Correct |
9 ms |
384 KB |
Output is correct |
51 |
Correct |
10 ms |
384 KB |
Output is correct |
52 |
Correct |
9 ms |
384 KB |
Output is correct |
53 |
Correct |
9 ms |
384 KB |
Output is correct |
54 |
Correct |
9 ms |
384 KB |
Output is correct |
55 |
Correct |
9 ms |
384 KB |
Output is correct |
56 |
Correct |
10 ms |
512 KB |
Output is correct |
57 |
Correct |
10 ms |
384 KB |
Output is correct |
58 |
Correct |
9 ms |
384 KB |
Output is correct |
59 |
Correct |
9 ms |
384 KB |
Output is correct |
60 |
Correct |
11 ms |
384 KB |
Output is correct |
61 |
Correct |
10 ms |
384 KB |
Output is correct |
62 |
Correct |
1 ms |
256 KB |
Output is correct |
63 |
Correct |
0 ms |
256 KB |
Output is correct |
64 |
Correct |
0 ms |
256 KB |
Output is correct |
65 |
Correct |
0 ms |
256 KB |
Output is correct |
66 |
Correct |
0 ms |
256 KB |
Output is correct |
67 |
Correct |
1 ms |
256 KB |
Output is correct |
68 |
Correct |
0 ms |
256 KB |
Output is correct |
69 |
Correct |
0 ms |
256 KB |
Output is correct |
70 |
Correct |
0 ms |
256 KB |
Output is correct |
71 |
Correct |
0 ms |
256 KB |
Output is correct |
72 |
Correct |
0 ms |
256 KB |
Output is correct |
73 |
Correct |
0 ms |
256 KB |
Output is correct |
74 |
Correct |
1 ms |
256 KB |
Output is correct |
75 |
Correct |
1 ms |
256 KB |
Output is correct |
76 |
Correct |
1 ms |
256 KB |
Output is correct |
77 |
Correct |
0 ms |
256 KB |
Output is correct |
78 |
Correct |
1 ms |
256 KB |
Output is correct |
79 |
Correct |
0 ms |
256 KB |
Output is correct |
80 |
Correct |
0 ms |
256 KB |
Output is correct |
81 |
Correct |
0 ms |
256 KB |
Output is correct |
82 |
Correct |
0 ms |
256 KB |
Output is correct |
83 |
Correct |
0 ms |
256 KB |
Output is correct |
84 |
Correct |
0 ms |
256 KB |
Output is correct |
85 |
Correct |
0 ms |
256 KB |
Output is correct |
86 |
Correct |
0 ms |
256 KB |
Output is correct |
87 |
Correct |
0 ms |
256 KB |
Output is correct |
88 |
Correct |
0 ms |
256 KB |
Output is correct |
89 |
Correct |
0 ms |
256 KB |
Output is correct |
90 |
Correct |
0 ms |
256 KB |
Output is correct |
91 |
Correct |
0 ms |
256 KB |
Output is correct |