Submission #390855

#TimeUsernameProblemLanguageResultExecution timeMemory
390855AlexPop28Counting Mushrooms (IOI20_mushrooms)C++14
56.64 / 100
14 ms456 KiB
#include "mushrooms.h"
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL_DEFINE
const int K = 2;
#else
const int K = 100;
#endif

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int count_mushrooms(int n) {
  if (n <= K) {
    int ans = 0;
    for (int i = 1; i < n; ++i) {
      vector<int> curr = {0, i};
      ans += use_machine(curr);
    }
    return n - ans;
  }

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

  vector<int> pos_a, pos_b;
  while((int)max(pos_a.size(), pos_b.size()) < K) {
    int pos = rng() % n;
    while (val[pos] >= 0) pos = rng() % n;
    
    vector<int> curr = {0, pos};
    if (use_machine(curr) == 0) {
      pos_a.emplace_back(pos);
      val[pos] = 0;
    } else {
      pos_b.emplace_back(pos);
      val[pos] = 1;
    }
  }

  bool type = false;
  auto pos = pos_a;
  if (pos_b.size() > pos_a.size()) {
    type = true;
    pos = pos_b;
  }

  int ans = pos_a.size() + 1;
  int i = 1;
  while (i < n) {
    vector<int> curr;
    for (int j = 0; j < K; ++j) {
      curr.emplace_back(pos[j]);
      while (i < n && val[i] >= 0) ++i;
      if (i == n) break;
      if (j + 1 < K) curr.emplace_back(i++);
    }
    int total = (curr.size() - 1) / 2;
    int ret = use_machine(curr) / 2;
    if (!type) ret = total - ret;
    ans += ret;
  }
  return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...