This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "prize.h"
#include <random>
using namespace std;
int find_best(int n)
{
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dist(0, n - 1);
vector<vector<int>> mem(n);
int prev = -1;
int sum = 0;
for (int w = 0; w < min(400, n); ++w)
{
int j = dist(gen);
if (mem[j].empty()) mem[j] = ask(j);
vector<int> tmp = mem[j];
if (tmp[0] + tmp[1] == 0) return j;
sum = max(sum, tmp[0] + tmp[1]);
}
for (int i = 1;; ++i)
{
int l = 0, r = n - 1;
//first with not max level
while (l < r)
{
int m = (l + r) >> 1;
if (mem[m].empty()) mem[m] = ask(m);
if (mem[m][0] + mem[m][1] == 0) return m;
if ((sum == mem[m][0] + mem[m][1] && mem[m][0] < i) || m <= prev) l = m + 1;
else r = m;
}
prev = l;
if (mem[prev].empty()) mem[prev] = ask(prev);
if (mem[prev][0] + mem[prev][1] == 0) return prev;
}
}
//1 2 5 26 677 - mins
//1 4 21 446 200000 - maxs
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |