이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
#include "prize.h"
int find_best(int n) {
map <int, vector <int>> mem;
int cnt = 0;
auto my_ask = [&] (int i) {
if (!mem.count(i)) {
++cnt;
assert(cnt <= 10000);
mem[i] = ask(i);
}
return mem[i];
};
function <int(int, int)> rec = [&] (int l, int r) {
if (l > r) return -1;
auto f = my_ask(l);
if (f[0] + f[1] == 0) return l;
auto s = my_ask(r);
if (s[0] + s[1] == 0) return r;
bool fl = false;
if (f[0] + f[1] != s[0] + s[1]) fl = true;
else if (f[1] - s[1] > 0) fl = true;
if (fl) {
int m = (l + r) >> 1;
int x = rec(l, m);
if (x != -1) return x;
x = rec(m, r);
return x;
} else {
return -1;
}
};
int ans = rec(0, n - 1);
return ans;
}
#ifdef LOCAL
const int max_q = 10000;
int n;
int query_count = 0;
vector<int> g;
vector<vector<int> > rank_count;
vector<int> ask(int i) {
query_count++;
if(query_count > max_q) {
cerr << "Query limit exceeded" << endl;
exit(0);
}
if(i < 0 || i >= n) {
cerr << "Bad index: " << i << endl;
exit(0);
}
vector<int> res(2);
res[0] = rank_count[g[i] - 1][i + 1];
res[1] = rank_count[g[i] - 1][n] - res[0];
return res;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
cin >> n;
g.resize(n);
for(int i = 0; i < n; i++) {
cin >> g[i];
if(g[i] < 1) {
cerr << "Invalid rank " << g[i] << " at index " << i << endl;
exit(0);
}
}
int max_rank = *max_element(g.begin(), g.end());
rank_count.resize(max_rank + 1, vector <int> (n + 1, 0));
for(int r = 0; r <= max_rank; r++) {
for(int i = 1; i <= n; i++) {
rank_count[r][i] = rank_count[r][i - 1];
if(g[i - 1] == r)
rank_count[r][i]++;
}
}
for(int i = 0; i <= n; i++)
for(int r = 1; r <= max_rank; r++)
rank_count[r][i] += rank_count[r - 1][i];
int res = find_best(n);
cout << res << endl << "Query count: " << query_count << endl;
return 0;
}
#endif
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |