# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
413241 | atoiz | Counting Mushrooms (IOI20_mushrooms) | C++14 | 14 ms | 328 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "mushrooms.h"
#include <vector>
#include <iostream>
#include <array>
#include <cassert>
#include <algorithm>
using namespace std;
int count_mushrooms(int n) {
const int SPLIT = 89;
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 c, int d, int e) {
int x = use_machine({a[1][0], b, a[1][1], a[0][0], f, a[0][1], c, a[0][2], d}) - 3;
int y = use_machine({a[0][0], f, a[0][1], b, a[0][2], e});
a[x & 1].push_back(d);
a[y & 1].push_back(e);
x >>= 1, y >>= 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);
};
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;
identify_5(wait[0], m, m - 1, m - 2, m - 3);
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;
identify_5(m - 1, m - 2, m - 3, m - 4, m);
}
// 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 |
---|---|---|---|---|
Fetching results... |