#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200009;
int n, r, crit;
int a[2*MAXN];
struct State{
int pl, ze, mi;
State operator+(const State &s) const{
return (State){pl+s.pl, ze+s.ze, mi+s.mi};
}
} init[MAXN];
int simulate1(void){
vector<State> pot(5*n);
State pq = {0, 0, 0};
int i, j;
for(i=0; i<n; i++){
pot[i] = init[i];
}
for(i=n; i<5*n; i++)
pot[i] = {0, 0, 0};
int cnt = 0, last = -1;
int lwin = (init[0].pl ? 1 : (init[0].ze ? 0 : -1));
for(i=0; i<4*n; i++){
pq = pq + pot[i];
int oppb;
//printf("i=%d pq= +%d 0%d -%d\n", i, pq.pl, pq.ze, pq.mi);
if(pq.pl >= 2){
oppb = 1;
pot[i+n].pl++;
pq.pl--;
}
else if(pq.pl == 1){
if(lwin == 1){
if(pq.ze){
oppb = 0;
pot[i+n].ze++;
pq.ze--;
}
else{
oppb = -1;
pot[i+n].mi++;
pq.mi--;
}
}
else{
oppb = 1;
pot[i+n].pl++;
pq.pl--;
}
}
else if(pq.ze && lwin){
oppb = 0;
pot[i+n].ze++;
pq.ze--;
}
else{
oppb = -1;
pot[i+n].mi++;
pq.mi--;
}
if(!lwin || !oppb)
last = i;
if(i==r)
break;
if(!oppb && lwin == 1)
cnt++;
else if(!lwin && oppb == 1)
cnt++;
lwin = max(lwin, oppb);
}
//printf("last=%d cnt=%d\n", last, cnt);
int ret = last - r - n*cnt;
return ret;
}
int simulate2(int pos){
State pq = {0, 0, 0};
int i, ans = pos;
for(i=2*n; i>=1; i--){
int cur = i%n;
pq = pq + init[cur];
//printf("i=%d pq.pl=%d ze=%d mi=%d\n", i,pq.pl, pq.ze, pq.mi);
init[cur].pl = init[cur].mi = init[cur].ze = 0;
if(cur){
if(pq.mi){
if(pq.ze)
ans--;
init[cur].mi = 1;
pq.mi--;
}
else if(pq.ze){
init[cur].ze = 1;
pq.ze--;
}
}
else{
if(pq.ze)
ans--;
}
}
return ans;
}
int simulate(int p){
int i, j=0;
for(i=0; i<n; i++){
init[i].pl = init[i].ze = init[i].mi = 0;
if(p == i)
init[i].ze++;
else{
if(a[j] < crit)
init[i].pl++;
else
init[i].mi++;
j++;
}
if(a[j] < crit)
init[i].pl++;
else
init[i].mi++;
j++;
}
if(crit <= n+1)
return simulate1();
return simulate2(p);
}
int main(){
int i;
scanf("%d %d", &n, &r);
r = 3*n + r%n;
scanf("%d", &crit);
for(i=0; i<2*n-1; i++){
scanf("%d", &a[i]);
}
if(crit==1){
printf("%d\n", n);
return 0;
}
pair<int, int> ans = make_pair(n, -1); //(result, -start)
for(int t=-5; t<=1; t++){
int goal = t*n, lo = 0, hi = n-1, mid;
while(lo < hi){
mid = (lo+hi)/2;
if(simulate(mid) >= goal)
hi = mid;
else
lo = mid+1;
}
int crit = simulate(lo);
//printf("lo=%d crit=%d\n", lo, crit);
hi = n-1;
while(lo < hi){
mid = (lo+hi+1)/2;
if(simulate(mid) == crit)
lo = mid;
else
hi = mid-1;
}
//printf("lo=%d\n", lo);
ans = min(ans, make_pair((crit%n + n)%n, -lo-1));
}
printf("%d\n", -ans.second);
return 0;
}
Compilation message
archery.cpp: In function 'int simulate1()':
archery.cpp:15:9: warning: unused variable 'j' [-Wunused-variable]
15 | int i, j;
| ^
archery.cpp: In function 'int main()':
archery.cpp:127:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | scanf("%d %d", &n, &r);
| ~~~~~^~~~~~~~~~~~~~~~~
archery.cpp:129:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | scanf("%d", &crit);
| ~~~~~^~~~~~~~~~~~~
archery.cpp:131:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
131 | scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
5 ms |
616 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
564 KB |
Output is correct |
4 |
Correct |
40 ms |
1628 KB |
Output is correct |
5 |
Correct |
633 ms |
13984 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
8 |
Incorrect |
29 ms |
1652 KB |
Output isn't correct |
9 |
Correct |
46 ms |
3988 KB |
Output is correct |
10 |
Incorrect |
2 ms |
344 KB |
Output isn't correct |
11 |
Correct |
51 ms |
4084 KB |
Output is correct |
12 |
Correct |
4 ms |
604 KB |
Output is correct |
13 |
Correct |
311 ms |
10844 KB |
Output is correct |
14 |
Correct |
10 ms |
816 KB |
Output is correct |
15 |
Correct |
77 ms |
4748 KB |
Output is correct |
16 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
17 |
Correct |
2 ms |
552 KB |
Output is correct |
18 |
Correct |
3 ms |
348 KB |
Output is correct |
19 |
Correct |
6 ms |
732 KB |
Output is correct |
20 |
Correct |
9 ms |
820 KB |
Output is correct |
21 |
Incorrect |
41 ms |
3968 KB |
Output isn't correct |
22 |
Correct |
62 ms |
4700 KB |
Output is correct |
23 |
Correct |
421 ms |
14628 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
6 ms |
348 KB |
Output is correct |
27 |
Correct |
48 ms |
2652 KB |
Output is correct |
28 |
Correct |
408 ms |
3420 KB |
Output is correct |
29 |
Correct |
2 ms |
344 KB |
Output is correct |
30 |
Correct |
5 ms |
348 KB |
Output is correct |
31 |
Correct |
25 ms |
2836 KB |
Output is correct |
32 |
Correct |
297 ms |
3928 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
3 ms |
348 KB |
Output is correct |
36 |
Correct |
4 ms |
348 KB |
Output is correct |
37 |
Correct |
21 ms |
2808 KB |
Output is correct |
38 |
Correct |
31 ms |
2648 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
3 ms |
348 KB |
Output is correct |
42 |
Correct |
3 ms |
348 KB |
Output is correct |
43 |
Correct |
6 ms |
544 KB |
Output is correct |
44 |
Correct |
12 ms |
640 KB |
Output is correct |
45 |
Correct |
25 ms |
2648 KB |
Output is correct |
46 |
Correct |
28 ms |
2652 KB |
Output is correct |
47 |
Correct |
304 ms |
4284 KB |
Output is correct |