Submission #390880

#TimeUsernameProblemLanguageResultExecution timeMemory
390880AlexPop28Counting Mushrooms (IOI20_mushrooms)C++14
80.14 / 100
13 ms384 KiB
#include "mushrooms.h"
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL_DEFINE
const int K = 4;
#else
const int K = 141; // de ce puii mei baga duplicate pt 141 da pt 100 merge perfect
#endif

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

int count_mushrooms(int n) {
  if (n <= 2 * 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);
  vector<int> pos_a, pos_b;

  auto Get = [&]() {
    int pos = rng() % n;
    while (val[pos] >= 0) pos = rng() % n;
    return pos;
  };

  auto A = [&](int x) { 
    assert(val[x] == -1); 
    pos_a.emplace_back(x); 
    val[x] = 0;
  };

  auto B = [&](int x) {
    assert(val[x] == -1);
    pos_b.emplace_back(x); 
    val[x] = 1;
  };

  A(0);
  while((int)max(pos_a.size(), pos_b.size()) < 2) {
    int pos = Get();
    vector<int> curr = {0, pos};
    if (use_machine(curr) == 0) {
      A(pos);
    } else {
      B(pos);
    }
  }

  if (pos_a.size() > pos_b.size()) { // am 2 A
    while((int)max(pos_a.size(), pos_b.size()) < K) {
      int pos0 = Get(), pos1 = Get();
      vector<int> curr = {pos_a[0], pos0, pos_a[1], pos1};
      int val = use_machine(curr);
      if (val == 0) {
        A(pos0); A(pos1);
      } else if (val == 1) {
        A(pos0); B(pos1);    
      } else if (val == 2) {
        B(pos0); A(pos1); 
      } else {
        B(pos0); B(pos1);
      }
    }
  } else { // am 2 B
    while((int)max(pos_a.size(), pos_b.size()) < K) {
      int pos0 = Get(), pos1 = Get();  
      vector<int> curr = {pos_b[0], pos0, pos_b[1], pos1};
      int val = use_machine(curr);
      if (val == 0) {
        B(pos0); B(pos1);
      } else if (val == 1) {
        B(pos0); A(pos1);    
      } else if (val == 2) {
        A(pos0); B(pos1); 
      } else {
        A(pos0); A(pos1);
      }
    }
  }

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

  int ans = pos_a.size();
  while ((int)pos.size() > K) {
    pos.pop_back();
  }

  sort(pos.begin(), pos.end());
  assert(unique(pos.begin(), pos.end()) == pos.end());

  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...