#include "mushrooms.h"
#include <vector>
#include <iostream>
#include <array>
#include <cassert>
#include <algorithm>
using namespace std;
int count_mushrooms(int n) {
const int SPLIT = 96;
if (n < 200) {
int res = n;
for (int i = 1; i < n; ++i) res -= use_machine({i, 0});
return res;
}
array<vector<int>, 2> a;
a[0] = {0}, a[1] = {};
array<int, 2> e = {0, 0};
int m = 0;
bool rev = false;
auto reorder = [&]() { if (a[0].size() < a[1].size()) a[0].swap(a[1]), swap(e[0], e[1]), rev = !rev; };
auto identify_1 = [&](int b) {
a[use_machine({a[0][0], b})].push_back(b);
};
auto identify_2 = [&](int c, int b) {
int x = use_machine({a[0][0], c, a[0][1], b});
a[x & 1].push_back(b);
a[x / 2].push_back(c);
};
auto identify_5 = [&](int f, int b, int e, int d, int c) {
int y = use_machine({a[0][0], f, a[0][1], b, a[0][2], e});
a[y & 1].push_back(e);
y >>= 1;
if (y == 0 || y == 2) {
a[y / 2].push_back(f);
a[y / 2].push_back(b);
return false;
}
int x = use_machine({a[1][0], b, a[1][1], a[0][0], f, a[0][1], c, a[0][2], d}) - 3;
a[x & 1].push_back(d);
x >>= 1;
a[(x + y) & 1].push_back(c);
if ((x + y) & 1) --x;
a[(x + y) / 2].push_back(f);
a[(y - x) / 2].push_back(b);
return true;
};
while ((int) a[0].size() < 2) {
++m;
identify_1(m);
reorder();
}
while ((int) a[0].size() < 3) {
m += 2;
identify_2(m - 1, m);
reorder();
}
vector<int> wait;
while ((int) a[0].size() < SPLIT && (int) a[1].size() < 2) {
if (wait.empty()) {
m += 3;
int x = use_machine({a[0][0], m, a[0][1], m - 1, a[0][2], m - 2});
a[x & 1].push_back(m - 2);
x /= 2;
if (x == 1) {
wait.push_back(m - 1);
wait.push_back(m);
} else {
assert(x == 0 || x == 2);
a[x / 2].push_back(m - 1);
a[x / 2].push_back(m);
}
} else {
m += 2;
int x = use_machine({a[0][0], m, a[0][1], m - 1, a[0][2], wait[0]});
a[x & 1].push_back(wait[0]);
a[!(x & 1)].push_back(wait[1]);
wait.clear();
x /= 2;
if (x == 1) {
wait.push_back(m - 1);
wait.push_back(m);
} else {
assert(x == 0 || x == 2);
a[x / 2].push_back(m - 1);
a[x / 2].push_back(m);
}
}
reorder();
}
if ((int) a[0].size() < SPLIT && !wait.empty()) {
m += 4;
if (!identify_5(wait[0], m - 2, m - 3, m, m - 1)) m -= 2;
if (find(a[0].begin(), a[0].end(), wait[0]) == a[0].end()) a[0].push_back(wait[1]);
else a[1].push_back(wait[1]);
wait.clear();
reorder();
}
if (!wait.empty()) {
++e[0], ++e[1];
wait.clear();
}
while ((int) a[0].size() < SPLIT) {
m += 5;
if (!identify_5(m - 4, m - 2, m - 3, m - 1, m)) m -= 2;
}
// for (int i = 0; i <= m; ++i) cerr << (rev ^ (find(a[0].begin(), a[0].end(), i) == a[0].end())) << ' ';
// cerr << endl;
while (m + 1 < n) {
vector<int> vec;
for (int i = 0; i < (int) a[0].size() && m + 1 < n; ++i) {
vec.push_back(a[0][i]);
vec.push_back(++m);
}
int k = use_machine(vec);
a[k & 1].push_back(vec.back());
e[0] += ((int) vec.size() / 2) - 1 - k / 2, e[1] += k / 2;
reorder();
}
if (rev) return a[1].size() + e[1];
else return a[0].size() + e[0];
assert(false);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
200 KB |
Output is correct |
2 |
Correct |
1 ms |
200 KB |
Output is correct |
3 |
Correct |
1 ms |
200 KB |
Output is correct |
4 |
Correct |
1 ms |
200 KB |
Output is correct |
5 |
Correct |
2 ms |
200 KB |
Output is correct |
6 |
Correct |
2 ms |
200 KB |
Output is correct |
7 |
Correct |
12 ms |
200 KB |
Output is correct |
8 |
Correct |
8 ms |
200 KB |
Output is correct |
9 |
Correct |
7 ms |
200 KB |
Output is correct |
10 |
Correct |
9 ms |
316 KB |
Output is correct |
11 |
Correct |
9 ms |
328 KB |
Output is correct |
12 |
Correct |
9 ms |
200 KB |
Output is correct |
13 |
Correct |
7 ms |
200 KB |
Output is correct |
14 |
Correct |
5 ms |
200 KB |
Output is correct |
15 |
Correct |
9 ms |
200 KB |
Output is correct |
16 |
Correct |
9 ms |
200 KB |
Output is correct |
17 |
Correct |
4 ms |
332 KB |
Output is correct |
18 |
Correct |
8 ms |
200 KB |
Output is correct |
19 |
Correct |
8 ms |
304 KB |
Output is correct |
20 |
Correct |
6 ms |
316 KB |
Output is correct |
21 |
Correct |
8 ms |
320 KB |
Output is correct |
22 |
Correct |
10 ms |
200 KB |
Output is correct |
23 |
Correct |
8 ms |
200 KB |
Output is correct |
24 |
Correct |
5 ms |
296 KB |
Output is correct |
25 |
Correct |
10 ms |
312 KB |
Output is correct |
26 |
Correct |
8 ms |
200 KB |
Output is correct |
27 |
Correct |
11 ms |
200 KB |
Output is correct |
28 |
Correct |
11 ms |
312 KB |
Output is correct |
29 |
Correct |
8 ms |
200 KB |
Output is correct |
30 |
Correct |
11 ms |
308 KB |
Output is correct |
31 |
Correct |
9 ms |
296 KB |
Output is correct |
32 |
Correct |
9 ms |
200 KB |
Output is correct |
33 |
Correct |
7 ms |
324 KB |
Output is correct |
34 |
Correct |
8 ms |
200 KB |
Output is correct |
35 |
Correct |
8 ms |
316 KB |
Output is correct |
36 |
Correct |
9 ms |
200 KB |
Output is correct |
37 |
Correct |
8 ms |
308 KB |
Output is correct |
38 |
Correct |
8 ms |
200 KB |
Output is correct |
39 |
Correct |
8 ms |
200 KB |
Output is correct |
40 |
Correct |
9 ms |
200 KB |
Output is correct |
41 |
Correct |
7 ms |
200 KB |
Output is correct |
42 |
Correct |
8 ms |
320 KB |
Output is correct |
43 |
Correct |
7 ms |
200 KB |
Output is correct |
44 |
Correct |
11 ms |
332 KB |
Output is correct |
45 |
Correct |
8 ms |
200 KB |
Output is correct |
46 |
Correct |
8 ms |
200 KB |
Output is correct |
47 |
Correct |
8 ms |
200 KB |
Output is correct |
48 |
Correct |
8 ms |
200 KB |
Output is correct |
49 |
Correct |
8 ms |
200 KB |
Output is correct |
50 |
Correct |
9 ms |
200 KB |
Output is correct |
51 |
Correct |
11 ms |
200 KB |
Output is correct |
52 |
Correct |
7 ms |
328 KB |
Output is correct |
53 |
Correct |
9 ms |
200 KB |
Output is correct |
54 |
Correct |
11 ms |
312 KB |
Output is correct |
55 |
Correct |
8 ms |
200 KB |
Output is correct |
56 |
Correct |
8 ms |
308 KB |
Output is correct |
57 |
Correct |
9 ms |
200 KB |
Output is correct |
58 |
Correct |
9 ms |
200 KB |
Output is correct |
59 |
Correct |
8 ms |
200 KB |
Output is correct |
60 |
Correct |
9 ms |
308 KB |
Output is correct |
61 |
Correct |
6 ms |
320 KB |
Output is correct |
62 |
Correct |
1 ms |
200 KB |
Output is correct |
63 |
Correct |
1 ms |
200 KB |
Output is correct |
64 |
Correct |
1 ms |
200 KB |
Output is correct |
65 |
Correct |
1 ms |
200 KB |
Output is correct |
66 |
Correct |
1 ms |
200 KB |
Output is correct |
67 |
Correct |
1 ms |
200 KB |
Output is correct |
68 |
Correct |
1 ms |
200 KB |
Output is correct |
69 |
Correct |
1 ms |
200 KB |
Output is correct |
70 |
Correct |
1 ms |
200 KB |
Output is correct |
71 |
Correct |
1 ms |
200 KB |
Output is correct |
72 |
Correct |
1 ms |
200 KB |
Output is correct |
73 |
Correct |
1 ms |
200 KB |
Output is correct |
74 |
Correct |
2 ms |
200 KB |
Output is correct |
75 |
Correct |
1 ms |
200 KB |
Output is correct |
76 |
Correct |
1 ms |
200 KB |
Output is correct |
77 |
Correct |
1 ms |
200 KB |
Output is correct |
78 |
Correct |
2 ms |
200 KB |
Output is correct |
79 |
Correct |
1 ms |
200 KB |
Output is correct |
80 |
Correct |
1 ms |
200 KB |
Output is correct |
81 |
Correct |
1 ms |
200 KB |
Output is correct |
82 |
Correct |
1 ms |
200 KB |
Output is correct |
83 |
Correct |
1 ms |
200 KB |
Output is correct |
84 |
Correct |
1 ms |
200 KB |
Output is correct |
85 |
Correct |
1 ms |
200 KB |
Output is correct |
86 |
Correct |
1 ms |
200 KB |
Output is correct |
87 |
Correct |
1 ms |
200 KB |
Output is correct |
88 |
Correct |
1 ms |
200 KB |
Output is correct |
89 |
Correct |
1 ms |
200 KB |
Output is correct |
90 |
Correct |
1 ms |
200 KB |
Output is correct |
91 |
Correct |
1 ms |
200 KB |
Output is correct |