Submission #876343

#TimeUsernameProblemLanguageResultExecution timeMemory
876343KanonCounting Mushrooms (IOI20_mushrooms)C++14
91.87 / 100
5 ms880 KiB
#include <bits/stdc++.h>
#include "mushrooms.h"

using namespace std;

const int magic = 100;
const int bound = 226;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// generate random number between l, r : uniform_int_distribution<long long>(l, r)(rng)
// random shuffle : shuffle(.begin(), .end(), rng)

int count_mushrooms(int n) {

  if (n <= bound + 1) {
    int ret = 1;
    for (int i = 1; i < n; i++) {
      ret += use_machine({0, i}) ^ 1;
    }
    return ret;
  }

  vector<int> a(n, -1);
  a[0] = 0;

  vector<int> order(magic);
  iota(order.begin(), order.end(), 1);
  shuffle(order.begin(), order.end(), rng);
  int cnt = magic + 1;

  vector<int> ONE, ZERO;
  ZERO.push_back(0);
  for (int i = 0; i < 2; i++) {
    a[cnt] = (use_machine({0, cnt}) & 1) ^ a[0];
    cnt++;
    if (a[cnt - 1] == 0) ZERO.push_back(cnt - 1);
    else ONE.push_back(cnt - 1);
  }
  int p1, p2;
  if (ZERO.size() >= 2) {
    p1 = ZERO[0];
    p2 = ZERO[1];
  } else {
    p1 = ONE[0];
    p2 = ONE[1];
  }

  auto solve_one = [&](int p) {
    int st = use_machine({p1, p, p2, cnt++});
    a[cnt - 1] = a[p1] ^ (st & 1);
    st -= a[cnt - 1] ^ a[p2];
    a[p] = a[p1] ^ (st > 0);
  };

  int ite = 1;
  while (a[ite] == -1) {
    solve_one(ite);
    ite++;
  }

  int ret = 0;
  vector<int> zeros, ones;
  for (int i = 0; i < cnt; i++) {
    if (a[i] == 0) zeros.push_back(i);
    else ones.push_back(i);
  }
  ret += zeros.size();

  int it = 0;
  while (cnt < n) {
    it++;
    int len = min(n - cnt, (int) max(zeros.size(), ones.size()));
    vector<int> ids = zeros.size() >= ones.size() ? zeros : ones;
    vector<int> que;
    for (int i = 0; i < len; i++) {
      que.push_back(ids[i]);
      que.push_back(cnt++);
    }

    int st = use_machine(que);

    if (ids == ones) {

      a[cnt - 1] = 1 ^ (st & 1);
      ret += a[cnt - 1] == 0;
      st -= a[cnt - 1] == 0;

      if (a[cnt - 1] == 0) zeros.push_back(cnt - 1);
      else ones.push_back(cnt - 1);

      assert(st % 2 == 0);
      ret += st >> 1;

    } else {

      a[cnt - 1] = 0 ^ (st & 1);
      ret += a[cnt - 1] == 0;
      st -= a[cnt - 1] == 1;

      if (a[cnt - 1] == 0) zeros.push_back(cnt - 1);
      else ones.push_back(cnt - 1);

      assert(st % 2 == 0);
      ret += (len - 1) - (st >> 1);

    }
  }
  return ret;
}
#Verdict Execution timeMemoryGrader output
Fetching results...