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"
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define sz(x) (int)(x).size()
vi queries;
vi ans;
int query(int i)
{
queries.push_back(i);
cout << "? " << i << endl;
int a;
cin >> a;
ans.push_back(a);
return a;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N;
cin >> N;
int maxPot = 0;
while (1 << (maxPot + 1) < N - 1)
++maxPot;
// was ist mit kleinen n?
int tryLen = 0;
int centerpiece = 1 << (maxPot - 1);
for (int i = maxPot - 2; i >= 0; i -= 2)
if ((1 << i) & (N - 1))
centerpiece += 1 << i;
++centerpiece;
query(centerpiece);
tryLen = 1 << (maxPot - 1);
int a = query(centerpiece + tryLen);
if (!a)
// have to get bigger
tryLen = 1 << maxPot;
else
tryLen /= 2;
int len = 0;
for (; tryLen > 0; tryLen /= 2)
{
int nextTry;
if (queries.back() < centerpiece)
nextTry = queries.back() + len + tryLen;
else
nextTry = queries.back() - len - tryLen;
if (nextTry < 1 || nextTry > N)
continue;
if (nextTry != centerpiece)
{
a = query(nextTry);
if (!a)
len += tryLen;
}
else
len += tryLen;
}
int answer = abs(queries.back() - queries[sz(queries) - 2]);
if (!ans.back())
cout << answer + 1 << endl;
else
cout << answer << endl;
return 0;
};
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |