#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a) {
os << '{';
for(int i=0;i<sz(a);i++)
{
if(i>0&&i<sz(a)-1)
os << ", ";
os << a[i];
}
os << '}';
return os;
}
static const int max_q = 10000;
static int n;
static int query_count = 0;
static vector<int> g;
static 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;
}
/*
#include <bits/stdc++.h>
#include "prize.h"
using namespace std;
*/
int doit(int l,int r)
{
int m=(l+r)>>1;
vector<int> tr=ask(m);
if(tr[0]+tr[1]==0)
return m;
if(l==r)
return -1;
if(tr[0]>tr[1])
{
int tr=doit(l,m-1);
if(tr!=-1)
return tr;
return doit(m+1,r);
}
else
{
int tr=doit(m+1,r);
if(tr!=-1)
return tr;
return doit(l,m-1);
}
}
int find_best(int n) {
int l=0,r=n-1;
while(true)
{
int m=(l+r)>>1;
vector<int> tr=ask(m);
if(tr[0]==1)
{
r=m-1;
}
else
{
if(tr[1]==1)
{
l=m+1;
}
else
{
return m;
}
}
}
/*for(int i = 0; i < n; i++) {
std::vector<int> res = ask(i);
if(res[0] + res[1] == 0)
return i;
}
return 0;*/
}
int main() {
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;
}
Compilation message
/tmp/ccxit0ce.o: In function `ask(int)':
grader.cpp:(.text+0x0): multiple definition of `ask(int)'
/tmp/ccObiDsE.o:prize.cpp:(.text+0x0): first defined here
/tmp/ccxit0ce.o: In function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/ccObiDsE.o:prize.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status