#ifdef t9unkubj
#include"debug.cpp"
//#include"template_no_debug.h"
#else
#include"prize.h"
#define dbg(...) 199958
#endif
#undef _GLIBCXX_DEBUG
#pragma GCC optimize("O3")
using namespace std;
#include<bits/stdc++.h>
using ll=long long;
using ull=unsigned long long;
template<class T>using vc=vector<T>;
template<class T>using vvc=vc<vc<T>>;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,j,n) for(ll i=(j);i<(ll)(n);i++)
#define DREP(i,n,m) for(ll i=(n);i>=(m);i--)
#define drep(i,n) for(ll i=((n)-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
template<class T,class F>
bool chmin(T &x, F y){
if(x>y){
x=y;
return true;
}
return false;
}
template<class T, class F>
bool chmax(T &x, F y){
if(x<y){
x=y;
return true;
}
return false;
}
int find_best(int n);
namespace judge{
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;
}
void run(int n_,vc<int>a){
n=n_;
g=a;
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 ;
}
};
//using namespace judge;
/*
二分探索
"上"の階層のものがわかるので頑張る
*/
int cnt=0;
vc<int> Ask(int x){
static map<int,vc<int>>memo;
if(memo.count(x))return memo[x];
assert(++cnt<1e4);
return memo[x]=ask(x);
}
int find_best(int n) {
vc<int>is;
rep(i,n)is.push_back(i);
int low_cnt=0;
constexpr int B=448;
rep(i,min(B,n)){
chmax(low_cnt,Ask(i)[0]+Ask(i)[1]);
}
while(is.size()>1){
vc<int>nxt_is;
auto dfs=[&](auto&dfs,int l,int r)->void{
if(l==r)return;
if(l+1==r){
nxt_is.push_back(is[l]);
return;
}
int mid=l+r>>1;
auto res=Ask(is[mid]);
if(res[0]+res[1]<low_cnt){
nxt_is.push_back(is[mid]);
}
{
if(res[0]){
dfs(dfs,l,mid);
}
if(res[1]){
dfs(dfs,mid+1,r);
}
}
};
dfs(dfs,0,is.size());
sort(all(nxt_is));
dbg(is);
low_cnt=0;
for(auto&i:nxt_is){
chmax(low_cnt,Ask(i)[0]+Ask(i)[1]);
}
is=move(nxt_is);
dbg(is);
}
return is[0];
}
/*int main(){
int n;
cin>>n;
n=2e5;
vc<int>a(n);
rep(i,n)cin>>a[i];
run(n,a);
}*/
/*
8
3 2 3 1 3 3 2 3
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |