#include "prize.h"
#include<bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand_int(int l,int r){
return l+(int)(rng()%(r-l+1));
}
const int B = 512;
const int maxn = 2e5+5;
vector<int> res[maxn];
vector<int> get(int x){
if(!res[x].empty()) return res[x];
res[x]=ask(x);res[x][1]+=res[x][0];
return res[x];
}
int N,bit[maxn],Max;
void update(int x){
for(int i=x+1;i<=N;i+=(i&(-i))) bit[i]++;
}
int query(int x){
int ans=0;
for(int i=x+1;i>=1;i-=(i&(-i))) ans+=bit[i];
return ans;
}
int find_best(int n){
N=n;rng.seed(998244353);
if(rand_int(0,1)){
for(int i=0;i<400;i++){
int pos=rand_int(0,n-1);
vector<int> cc=get(pos);
if(cc[1]==0) return pos;
Max=max(Max,cc[1]);
}
}else{
for(int i=0;i<480;i++){
vector<int> cc=get(i);
if(cc[1]==0) return i;
Max=max(Max,cc[1]);
}
}
for(int i=0;i<n;i+=B){
vector<int> p;
for(int j=i;j<min(i+B,n);j++) p.push_back(j);
int cnt=0;
while(!p.empty()){
int x=p.back();p.pop_back();
vector<int> cc=get(x);
if(cc[1]==0) return x;
if(cc[1]<Max) update(x);
else{
cnt=cc[0]-query(x);
break;
}
}
while(cnt){
int l=0,r=(int)p.size()-1;
bool check=false;
while(l<=r){
int mid=(l+r)>>1;
vector<int> cc=get(p[mid]);
if(cc[1]==0) return p[mid];
if(cc[1]<Max){
update(p[mid]);
p.erase(p.begin()+mid);
check=true;
break;
}
if(cc[0]-query(p[mid])) r=mid-1;
else l=mid+1;
}
cnt--;
}
}
return -1;
}
Compilation message
prize.cpp: In function 'int find_best(int)':
prize.cpp:60:18: warning: variable 'check' set but not used [-Wunused-but-set-variable]
60 | bool check=false;
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6264 KB |
Output is correct |
2 |
Correct |
5 ms |
6268 KB |
Output is correct |
3 |
Correct |
8 ms |
5756 KB |
Output is correct |
4 |
Correct |
7 ms |
5772 KB |
Output is correct |
5 |
Correct |
6 ms |
6020 KB |
Output is correct |
6 |
Correct |
2 ms |
5740 KB |
Output is correct |
7 |
Correct |
5 ms |
6024 KB |
Output is correct |
8 |
Correct |
5 ms |
6016 KB |
Output is correct |
9 |
Correct |
6 ms |
6012 KB |
Output is correct |
10 |
Correct |
7 ms |
6536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6264 KB |
Output is correct |
2 |
Correct |
7 ms |
6008 KB |
Output is correct |
3 |
Correct |
4 ms |
6020 KB |
Output is correct |
4 |
Correct |
6 ms |
6008 KB |
Output is correct |
5 |
Correct |
6 ms |
6012 KB |
Output is correct |
6 |
Correct |
2 ms |
5720 KB |
Output is correct |
7 |
Correct |
5 ms |
6008 KB |
Output is correct |
8 |
Correct |
5 ms |
6240 KB |
Output is correct |
9 |
Correct |
5 ms |
6004 KB |
Output is correct |
10 |
Correct |
6 ms |
5752 KB |
Output is correct |
11 |
Correct |
5 ms |
6080 KB |
Output is correct |
12 |
Correct |
3 ms |
6008 KB |
Output is correct |
13 |
Correct |
9 ms |
6004 KB |
Output is correct |
14 |
Correct |
4 ms |
6268 KB |
Output is correct |
15 |
Correct |
6 ms |
6308 KB |
Output is correct |
16 |
Correct |
21 ms |
6748 KB |
Output is correct |
17 |
Correct |
3 ms |
6012 KB |
Output is correct |
18 |
Correct |
26 ms |
6856 KB |
Output is correct |
19 |
Correct |
4 ms |
6008 KB |
Output is correct |
20 |
Correct |
7 ms |
6604 KB |
Output is correct |
21 |
Correct |
12 ms |
6740 KB |
Output is correct |
22 |
Correct |
5 ms |
6048 KB |
Output is correct |
23 |
Correct |
5 ms |
6008 KB |
Output is correct |
24 |
Correct |
4 ms |
6008 KB |
Output is correct |
25 |
Correct |
12 ms |
6252 KB |
Output is correct |
26 |
Correct |
14 ms |
6664 KB |
Output is correct |
27 |
Correct |
4 ms |
5772 KB |
Output is correct |
28 |
Correct |
29 ms |
6228 KB |
Output is correct |
29 |
Correct |
16 ms |
6488 KB |
Output is correct |
30 |
Correct |
29 ms |
6276 KB |
Output is correct |
31 |
Correct |
4 ms |
6012 KB |
Output is correct |
32 |
Correct |
8 ms |
6012 KB |
Output is correct |
33 |
Correct |
2 ms |
5720 KB |
Output is correct |
34 |
Correct |
9 ms |
6832 KB |
Output is correct |
35 |
Correct |
5 ms |
6024 KB |
Output is correct |
36 |
Correct |
7 ms |
6560 KB |
Output is correct |
37 |
Correct |
5 ms |
6016 KB |
Output is correct |
38 |
Correct |
4 ms |
6016 KB |
Output is correct |
39 |
Correct |
11 ms |
6372 KB |
Output is correct |
40 |
Correct |
22 ms |
6884 KB |
Output is correct |
41 |
Correct |
16 ms |
6672 KB |
Output is correct |
42 |
Correct |
14 ms |
6476 KB |
Output is correct |
43 |
Correct |
11 ms |
6132 KB |
Output is correct |
44 |
Correct |
12 ms |
6956 KB |
Output is correct |
45 |
Correct |
10 ms |
6300 KB |
Output is correct |
46 |
Correct |
4 ms |
6012 KB |
Output is correct |
47 |
Correct |
10 ms |
6916 KB |
Output is correct |
48 |
Correct |
21 ms |
6004 KB |
Output is correct |
49 |
Correct |
5 ms |
6292 KB |
Output is correct |
50 |
Correct |
22 ms |
6652 KB |
Output is correct |
51 |
Correct |
13 ms |
6208 KB |
Output is correct |
52 |
Correct |
4 ms |
6260 KB |
Output is correct |
53 |
Correct |
7 ms |
6268 KB |
Output is correct |
54 |
Correct |
13 ms |
6904 KB |
Output is correct |
55 |
Correct |
4 ms |
5760 KB |
Output is correct |
56 |
Correct |
22 ms |
7656 KB |
Output is correct |
57 |
Correct |
15 ms |
6304 KB |
Output is correct |
58 |
Correct |
19 ms |
6504 KB |
Output is correct |
59 |
Correct |
14 ms |
6672 KB |
Output is correct |
60 |
Correct |
11 ms |
6492 KB |
Output is correct |
61 |
Correct |
5 ms |
6028 KB |
Output is correct |
62 |
Correct |
4 ms |
5756 KB |
Output is correct |
63 |
Correct |
7 ms |
6008 KB |
Output is correct |
64 |
Correct |
5 ms |
6004 KB |
Output is correct |
65 |
Correct |
7 ms |
5956 KB |
Output is correct |
66 |
Correct |
7 ms |
6008 KB |
Output is correct |
67 |
Correct |
7 ms |
6024 KB |
Output is correct |
68 |
Correct |
4 ms |
6012 KB |
Output is correct |
69 |
Correct |
6 ms |
6024 KB |
Output is correct |
70 |
Correct |
6 ms |
6292 KB |
Output is correct |
71 |
Partially correct |
23 ms |
6716 KB |
Partially correct - number of queries: 5037 |
72 |
Correct |
8 ms |
6012 KB |
Output is correct |
73 |
Correct |
36 ms |
6304 KB |
Output is correct |
74 |
Partially correct |
35 ms |
6536 KB |
Partially correct - number of queries: 5005 |
75 |
Correct |
6 ms |
5780 KB |
Output is correct |
76 |
Correct |
22 ms |
7012 KB |
Output is correct |
77 |
Correct |
25 ms |
6752 KB |
Output is correct |
78 |
Correct |
7 ms |
6264 KB |
Output is correct |
79 |
Correct |
14 ms |
6676 KB |
Output is correct |
80 |
Correct |
25 ms |
6676 KB |
Output is correct |
81 |
Correct |
33 ms |
6408 KB |
Output is correct |
82 |
Correct |
28 ms |
6772 KB |
Output is correct |
83 |
Correct |
5 ms |
6008 KB |
Output is correct |
84 |
Correct |
21 ms |
7104 KB |
Output is correct |
85 |
Correct |
23 ms |
6724 KB |
Output is correct |
86 |
Correct |
6 ms |
6260 KB |
Output is correct |
87 |
Correct |
7 ms |
6044 KB |
Output is correct |
88 |
Correct |
7 ms |
6520 KB |
Output is correct |
89 |
Correct |
6 ms |
5752 KB |
Output is correct |
90 |
Correct |
7 ms |
6012 KB |
Output is correct |
91 |
Correct |
8 ms |
6264 KB |
Output is correct |
92 |
Correct |
7 ms |
6276 KB |
Output is correct |
93 |
Correct |
6 ms |
6524 KB |
Output is correct |
94 |
Correct |
7 ms |
6056 KB |
Output is correct |
95 |
Correct |
8 ms |
6584 KB |
Output is correct |
96 |
Correct |
7 ms |
6540 KB |
Output is correct |
97 |
Correct |
5 ms |
6044 KB |
Output is correct |