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 <bits/stdc++.h>
#include "prize.h"
using namespace std;
typedef pair<int, int> pii;
int sq, mxcnt, ans;
map<int, int> mp[2];
pii myask(int pos)
{
if(mp[0].count(pos))
return {mp[0][pos], mp[1][pos]};
auto v = ask(pos);
mp[0][pos] = v[0];
mp[1][pos] = v[1];
return {v[0], v[1]};
}
void solve(int st, int dr)
{
if(st > dr) return;
if(ans != -1) return;
auto askst = myask(st);
auto askdr = myask(dr);
int sumst = askst.first + askst.second;
int sumdr = askdr.first + askdr.second;
if(sumst == 0) { ans = st; return; }
if(sumdr == 0) { ans = dr; return; }
if(askst.second == 0 || askdr.first == 0) return;
if(dr - st <= 1) return;
if(sumst != mxcnt)
{
solve(st + 1, dr);
return;
}
if(sumdr != mxcnt)
{
solve(st, dr - 1);
return;
}
int cnt = askdr.first - askst.first;
if(cnt == 0) return;
int lgt = dr - st - 1;
int add = lgt / (cnt + 1);
add = max(add, 1);
solve(st, st + add);
solve(st + add, dr);
}
int find_best(int N)
{
ans = -1;
sq = sqrt(N) + 1;
sq = min(sq, N);
mxcnt = 0;
for(int i = 0; i < sq; i++)
{
auto x = myask(i);
int sum = x.first + x.second;
if(sum == 0) ans = i;
mxcnt = max(mxcnt, sum);
}
solve(0, N - 1);
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |