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"
#define pii pair<int, int>
#define F first
#define S second
using namespace std;
//vector<int> ask(int p) {
// // cout << p << endl;
// int a = 0, b = 0;
// int v[] = {3, 2, 3, 1, 3, 3, 2, 3};
// for(int i = 0; i < p; i ++) a += (v[i] < v[p]);
// for(int i = p + 1; i < 8; i ++) b += (v[i] < v[p]);
// return {a, b};
//}
unordered_map<int, pii> mp;
pii query(int p) {
if(mp.find(p) != mp.end()) return mp[p];
vector<int> v = ask(p);
return mp[p] = {v[0], v[1]};
}
vector<pii> ans;
void dfs(int l, int r, pii a, pii b, int mx) {
// cout << l << ' ' << r << endl;
if(l == r) {
if(b.F + b.S != mx) ans.push_back({b.F + b.S, l});
return;
}
if(a == b) return;
int m = l + r >> 1;
pii v = query(m);
if(v.F + v.S != mx) {
ans.push_back({v.F + v.S, m});
if(m != l) dfs(l, m - 1, a, query(m - 1), mx);
if(m != r) dfs(m + 1, r, query(m + 1), b, mx);
}
else {
if(v == a) return dfs(m + 1, r, v, b, mx);
if(v == b) return dfs( l, m, a, v, mx);
dfs( l, m, a, v, mx);
dfs(m + 1, r, v, b, mx);
}
}
int find_best(int n) {
srand(time(0));
int mx = 0;
for(int i = 0; i < 1000; i ++) {
pii tmp = query(rand() % n);
mx = max(mx, tmp.F + tmp.S);
}
dfs(0, n - 1, query(0), query(n - 1), mx);
pii mn = {mx, -1};
// for(auto it: ans) cout << it.S << ' '; cout << endl;
for(auto it: ans) mn = min(mn, it);
return mn.S;
}
//main () {
// cout << find_best(8);
//}
Compilation message (stderr)
prize.cpp: In function 'void dfs(int, int, std::pair<int, int>, std::pair<int, int>, int)':
prize.cpp:29:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = l + r >> 1;
~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |