제출 #413313

#제출 시각아이디문제언어결과실행 시간메모리
413313atoiz버섯 세기 (IOI20_mushrooms)C++14
100 / 100
12 ms332 KiB
#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 timeMemoryGrader output
Fetching results...