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;
int mx_cnt;
int answer = -1;
map<int, array<int, 2>> memo;
map<int, set<int>> found;
array<int, 2> askme(int i) {
array<int, 2> ans;
if (memo.find(i) != memo.end()) {
ans[0] = memo[i][0];
ans[1] = memo[i][1];
} else {
auto v = ask(i);
ans[0] = v[0];
ans[1] = v[1];
}
memo[i] = {ans[0], ans[1]};
found[ans[0] + ans[1]].insert(i);
if (ans[0] + ans[1] == 0) answer = i;
return ans;
}
int count(int i) { return askme(i)[0] + askme(i)[1]; }
bool makes_sense(int l, int r) {
for (auto& ss : found) {
if (ss.first == mx_cnt) continue;
auto& s = ss.second;
auto il = s.lower_bound(l);
auto ir = s.lower_bound(r);
if (il == s.begin()) continue;
if (ir == s.end()) continue;
il = prev(il);
if (askme(*ir)[0] - askme(*il)[0] == 0) return false;
}
return true;
}
void findall(int l, int r) {
for (; count(l) != mx_cnt; ++l) if (answer != -1) return;
for (; count(r) != mx_cnt; --r) if (answer != -1) return;
if (!makes_sense(l, r)) return;
if (l >= r) return;
int cnt = askme(r)[0] - askme(l)[0];
if (cnt <= 0) return;
if (answer != -1) return;
int m = (l + r) / 2;
findall(l, m);
findall(m, r);
}
int find_best(int n) {
srand(time(NULL));
for (int i = 0; i < 500 && i < n; ++i) mx_cnt = max(mx_cnt, count(i));
int l = 0; while (count(l) != mx_cnt) ++l;
int r = n-1; while (count(r) != mx_cnt) --r;
findall(l, r);
return answer;
}
#ifdef LORENZO
int main() {
cout << "insert n: "; fflush(stdout);
int n; cin >> n;
cout << "answer: " << find_best(n) << endl;
}
#endif
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |