제출 #1344585

#제출 시각아이디문제언어결과실행 시간메모리
1344585limitsCounting Mushrooms (IOI20_mushrooms)C++20
75.59 / 100
3 ms440 KiB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3,unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define f0r(i, n) for (auto i = 0; i < (n); ++i)
#define fnr(i, n, k) for (auto i = (n); i < (k); ++i)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define F first
#define S second
#define ctn(x) cout << x << '\n'
#define forl(a, l) for (auto a : l)
#define ctl(l) for (auto &a : (l)) cout << a << ' '; cout << endl;
#define lb(v, x) (lower_bound(all(v), x) - begin(v))
#define ub(v, x) (upper_bound(all(v), x) - begin(v))
#define pq priority_queue

template <class T>
using V = vector<T>;
using ll = long long;
using vi = V<int>;
using vl = V<ll>;
using pi = pair<int, int>;
using ti = tuple<int, int, int>;
using Adj = V<vi>;
using vvi = V<vi>;

#include "mushrooms.h"

const int M = 100;

int count_mushrooms(int n) {
	if (n == 2) return 1 + !use_machine({0, 1});

	int c1 = use_machine({0, 1});
	int c2 = use_machine({0, 2});

	int cntA = 1, cntB = 0;
	vi A{0}, B;

	if (c1 == 0) { // 1 - A
		cntA++;
		A.pb(1);
	} else {
		cntB++;
		B.pb(1);
	}

	if (c2 == 0) { // 2 - A
		cntA++;
		A.pb(2);
	} else {
		cntB++;
		B.pb(2);
	}

	bool ta = 0;
	vi pr;
	if (cntA > cntB) {
		ta = 1;
		pr = {A[0], A[1]};
	} else {
		ta = 0;
		pr = {B[0], B[1]};
	}

	int j = 3;
	while (j < n && max(cntA, cntB) <= M) {
		if (j == n-1) {
			int c = use_machine({0, j});
			if (c == 1) {
				cntB++;
				B.pb(j);
			} else {
				cntA++;
				A.pb(j);
			}
			++j;
			continue;
		}
		if (ta) {
			int c = use_machine({A[0], j, A[1], j+1});
			if (c & 1) {
				cntB++;
				B.pb(j+1);
			} else {
				cntA++;
				A.pb(j+1);
			}
			if (c >= 2) {
				cntB++;
				B.pb(j);
			} else {
				cntA++;
				A.pb(j);
			}
		} else {
			int c = use_machine({B[0], j, B[1], j+1});
			if (c & 1) {
				cntA++;
				A.pb(j+1);
			} else {
				cntB++;
				B.pb(j+1);
			}
			if (c >= 2) {
				cntA++;
				A.pb(j);
			} else {
				cntB++;
				B.pb(j);
			}
		}
		j += 2;
	}

	if (j >= n) return cntA;
	int cnt = cntA;
	for (; j < n; j += M) {
		vi v;
		int r = min(j+M, n);
		if (cntA > cntB) {
			f0r(i, r-j) {
				v.pb(A[i]);
				v.pb(j+i);
			}
			v.pb(A[r-j]);
			cnt += r-j - use_machine(v)/2;
		} else {
			f0r(i, r-j) {
				v.pb(B[i]);
				v.pb(j+i);
			}
			v.pb(B[r-j]);
			cnt += use_machine(v)/2;
		}
	}
	return cnt;
	//int cnt = 1;
	//V<pi> ps;
	//for (int i = 1; i < n; i+=2) {
	//	if (i == n-1) {
	//		cnt += !use_machine({0, i});
	//	} else {
	//		int x = use_machine({0, i, i+1});
	//		if (x == 0) cnt += 2;
	//		else if (x == 2) cnt += 1;
	//		else {
	//			ps.pb({i, i+1});
	//		}
	//	}
	//}
	//if (ps.size()) {
	//	auto [a, b] = ps.back();
	//	int c = use_machine({a, b});
	//	cnt += c;
	//	ps.pop_back();
	//	vi v = {0, a, b};
	//	for (auto &[x, y]: ps) {
	//		v.pb(x);
	//		v.pb(y);
	//	}
	//	cnt += (use_machine(v)-1)/2;
	//}
	//return cnt;
}
#Verdict Execution timeMemoryGrader output
Fetching results...