Submission #361540

#TimeUsernameProblemLanguageResultExecution timeMemory
361540KoDThe Big Prize (IOI17_prize)C++17
0 / 100
9 ms4972 KiB
#include "prize.h"
#include <vector>
#include <utility>
#include <algorithm>
#include <queue>

template <class T>
using Vec = std::vector<T>;

int find_best(int N) {
  int max = 0;
  const auto L = std::min(N, 475);
  Vec<Vec<int>> memo(N);
  const auto query = [&](const int i) -> const Vec<int>& {
    if (memo[i].empty()) {
      memo[i] = ask(i);
    }
    return memo[i];
  };
  for (int i = 0; i < L; ++i) {
    max = std::max(max, query(i)[0] + query(i)[1]);
  }
  Vec<int> pos;
  for (int i = 0; i < L; ++i) {
    if (query(i)[0] + query(i)[1] < max) {
      pos.push_back(i);
    }
  }
  std::queue<std::pair<int, int>> que;
  que.emplace(L, N - 1);
  const auto left_size = (int) pos.size();
  const auto count = [&](const int l, const int r) {
    return (r == N - 1 ? max : query(r)[0]) - (l == L ? left_size : query(l)[0]);
  };
  while (!que.empty() && (int) pos.size() < max) {
    const auto [l, r] = que.front();
    que.pop();
    if (l > r) {
      continue;
    }
    if (count(l, r) == 0) {
      continue;
    }
    const auto mid = (l + r) / 2;
    auto nr = mid - 1;
    auto nl = mid;
    while (nl <= r) {
      if (query(nl)[0] + query(nl)[1] == max) {
        break;
      }
      else {
        pos.push_back(nl);
        nl += 1;
      }
    }
    que.emplace(nl + 1, r);
    while (nr >= l) {
      if (query(nr)[0] + query(nr)[1] == max) {
        break;
      }
      else {
        pos.push_back(nr);
        nr -= 1;
      }
    }
    que.emplace(l, nr - 1);
  } 
  for (const auto x: pos) {
    if (query(x)[0] + query(x)[1] == 0) {
      return x;
    }
  }
  return -1;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...