#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
#define maxn 1000005
int n,cl[maxn],cr[maxn],c[maxn];
void extend(int &l,int &r){
//printf("extend: %d %d\n",l,r);
int farl=min(cl[c[l]],cl[c[r]]);
int farr=max(cr[c[l]],cr[c[r]]);
while(true){
if(farl<l){
--l;
farl=min(farl,cl[c[l]]);
farr=max(farr,cr[c[l]]);
}
else if(r<farr){
++r;
farl=min(farl,cl[c[r]]);
farr=max(farr,cr[c[r]]);
}
else break;
}
//printf("res: %d %d\n",l,r);
}
ll minimum_walk(vector<int> p,int s){
n=p.size();
int lbound=s,rbound=s;
ll ans=0;int cnt=1;
for(int i=0;i<n;++i){
if(c[i]!=0)continue;
int x=i;
cl[cnt]=cr[cnt]=i;
while(true){
c[x]=cnt;
cl[cnt]=min(cl[cnt],x);
cr[cnt]=max(cr[cnt],x);
ans+=abs(x-p[x]);
x=p[x];
if(x==i)break;
}
if(x!=p[x]){
lbound=min(lbound,cl[cnt]);
rbound=max(rbound,cr[cnt]);
}
++cnt;
}
int l=s,r=s;
while(l!=lbound||r!=rbound){
//printf("%d %d\n",l,r);
extend(l,r);
bool can=false;
int newl=n,newr=0;
int tmpl=l,tmpr=r;
int lcost=0;
while(true){
if(tmpl<=lbound)break;//done
--tmpl;lcost+=2;
extend(tmpl,tmpr);
if(tmpr>r){
can=true;
break;
}
}
tmpl=l,tmpr=r;
int rcost=0;
while(true){
if(tmpr>=rbound)break;//done
++tmpr;rcost+=2;
extend(tmpl,tmpr);
if(tmpl<l){
can=true;
break;
}
}
if(can)ans+=min(lcost,rcost);
else ans+=lcost+rcost;
l=tmpl,r=tmpr;
}
return ans;
}
Compilation message
books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:60:7: warning: unused variable 'newl' [-Wunused-variable]
60 | int newl=n,newr=0;
| ^~~~
books.cpp:60:14: warning: unused variable 'newr' [-Wunused-variable]
60 | int newl=n,newr=0;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
304 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
308 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
304 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
308 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
304 KB |
Output is correct |
30 |
Correct |
173 ms |
18660 KB |
Output is correct |
31 |
Correct |
150 ms |
18660 KB |
Output is correct |
32 |
Correct |
124 ms |
26572 KB |
Output is correct |
33 |
Correct |
155 ms |
24160 KB |
Output is correct |
34 |
Correct |
134 ms |
24208 KB |
Output is correct |
35 |
Correct |
138 ms |
23008 KB |
Output is correct |
36 |
Correct |
134 ms |
20704 KB |
Output is correct |
37 |
Correct |
127 ms |
18916 KB |
Output is correct |
38 |
Correct |
153 ms |
18788 KB |
Output is correct |
39 |
Correct |
133 ms |
18800 KB |
Output is correct |
40 |
Correct |
135 ms |
18660 KB |
Output is correct |
41 |
Correct |
161 ms |
18664 KB |
Output is correct |
42 |
Correct |
151 ms |
18852 KB |
Output is correct |
43 |
Correct |
132 ms |
24512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Execution timed out |
2087 ms |
340 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
304 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
308 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
304 KB |
Output is correct |
30 |
Correct |
173 ms |
18660 KB |
Output is correct |
31 |
Correct |
150 ms |
18660 KB |
Output is correct |
32 |
Correct |
124 ms |
26572 KB |
Output is correct |
33 |
Correct |
155 ms |
24160 KB |
Output is correct |
34 |
Correct |
134 ms |
24208 KB |
Output is correct |
35 |
Correct |
138 ms |
23008 KB |
Output is correct |
36 |
Correct |
134 ms |
20704 KB |
Output is correct |
37 |
Correct |
127 ms |
18916 KB |
Output is correct |
38 |
Correct |
153 ms |
18788 KB |
Output is correct |
39 |
Correct |
133 ms |
18800 KB |
Output is correct |
40 |
Correct |
135 ms |
18660 KB |
Output is correct |
41 |
Correct |
161 ms |
18664 KB |
Output is correct |
42 |
Correct |
151 ms |
18852 KB |
Output is correct |
43 |
Correct |
132 ms |
24512 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
304 KB |
Output is correct |
47 |
Execution timed out |
2087 ms |
340 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |