#include<bits/stdc++.h>
using namespace std;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int n,R;cin >> n >> R;R=R%n+2*n;
vector<int> b(2*n),a(2*n);
vector<vector<int>> cnt(n,vector<int>(3,0));
for(int i=0;i<2*n;i++) cin >> b[i];
int s=b[0];
auto g = [&](int x){
return (x>s)+(x>=s);
};
auto f = [&](){
if(s==1) return 0;
cnt.assign(n,vector<int>(3,0));
if(s>n+1){
int pos=0;
for(int i=0;i<n;i++){
cnt[i][0]=(a[i]>s)+(a[n+i]>s);
cnt[i][1]=(a[i]==s || a[n+i]==s);
}
int x=0,y=0,rt=0;
for(int it=0;it<3;it++){
int i=0;
do{
auto &d=cnt[i];
x+=d[0];y+=d[1];
if(x+y>1){
if(i){
d[0]=1;
d[1]=0;
x--;
}
else{
if(y>0){
d[1]=1;
d[0]=0;
y--;
}
else{
d[1]=0;
d[0]=1;
x--;
}
}
}
else{
if(i){
d[0]=x,d[1]=y;
x=y=0;
}
else{
if(y>0) rt++;
d[0]=d[1]=0;
}
}
i=(i+n-1)%n;
}while(i!=0);
}
for(int i=0;i<n;i++) if(cnt[i][1]) return i-rt*n;
}
else{
for(int i=0;i<n;i++) cnt[i][g(a[i])]++,cnt[i][g(a[n+i])]++;
int x=0,rt=0;
while(!cnt[0][x]) x++;
cnt[0][x]--;
vector<int> num(3,0);
for(int i=1;i<=3*n;i++){
for(int j=0;j<3;j++) num[j]+=cnt[(i-1)%n][j],cnt[(i-1)%n][j]=0;
int y=0;
while(!num[y]) y++;
num[y]--;
if(x>y) swap(x,y);
cnt[(i-1)%n][y]++;
if(y==1){
rt++;
if(i>2*n){
if(R<i) rt--;
return (n-1-R+i)%n-rt*n;
}
}
}
}
assert(false);
return -1;
};
auto get = [&](int x){
vector<int> c=b;
rotate(c.begin(),c.begin()+1,c.begin()+2*x+1);
for(int i=0;i<n;i++) a[i]=c[i<<1],a[n+i]=c[i<<1|1];
return f();
};
int cc=get(n-1),l=0,r=n-1;
while(l<r){
int m=(l+r)>>1;
if(get(m)>=cc-(cc%n+n)%n) r=m;
else l=m+1;
}
int val=get(l);
l=0,r=n-1;
while(l<r){
int m=(l+r+1)>>1;
if(get(m)<=val) l=m;
else r=m-1;
}
cout << l+1 << '\n';
}
Compilation message
archery.cpp: In lambda function:
archery.cpp:21:17: warning: unused variable 'pos' [-Wunused-variable]
21 | int pos=0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
8 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
456 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Correct |
55 ms |
1628 KB |
Output is correct |
5 |
Correct |
660 ms |
16216 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
46 ms |
1628 KB |
Output is correct |
9 |
Correct |
73 ms |
2312 KB |
Output is correct |
10 |
Correct |
3 ms |
344 KB |
Output is correct |
11 |
Correct |
82 ms |
2228 KB |
Output is correct |
12 |
Correct |
9 ms |
692 KB |
Output is correct |
13 |
Correct |
575 ms |
11640 KB |
Output is correct |
14 |
Correct |
15 ms |
856 KB |
Output is correct |
15 |
Correct |
115 ms |
3356 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
4 ms |
348 KB |
Output is correct |
18 |
Correct |
6 ms |
636 KB |
Output is correct |
19 |
Correct |
10 ms |
604 KB |
Output is correct |
20 |
Correct |
15 ms |
860 KB |
Output is correct |
21 |
Correct |
71 ms |
2380 KB |
Output is correct |
22 |
Correct |
99 ms |
3176 KB |
Output is correct |
23 |
Correct |
765 ms |
16852 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
4 ms |
1116 KB |
Output is correct |
27 |
Correct |
20 ms |
2228 KB |
Output is correct |
28 |
Correct |
118 ms |
12028 KB |
Output is correct |
29 |
Correct |
2 ms |
348 KB |
Output is correct |
30 |
Correct |
4 ms |
860 KB |
Output is correct |
31 |
Correct |
18 ms |
2204 KB |
Output is correct |
32 |
Correct |
168 ms |
16448 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
2 ms |
624 KB |
Output is correct |
36 |
Correct |
2 ms |
604 KB |
Output is correct |
37 |
Correct |
14 ms |
2004 KB |
Output is correct |
38 |
Correct |
20 ms |
2652 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
2 ms |
600 KB |
Output is correct |
42 |
Correct |
2 ms |
604 KB |
Output is correct |
43 |
Correct |
4 ms |
860 KB |
Output is correct |
44 |
Correct |
8 ms |
1128 KB |
Output is correct |
45 |
Correct |
16 ms |
2236 KB |
Output is correct |
46 |
Correct |
18 ms |
2396 KB |
Output is correct |
47 |
Correct |
193 ms |
18516 KB |
Output is correct |