#include "books.h"
#include<bits/stdc++.h>
using ll = long long;
using namespace std;
const int maxn=1e6+10;
int n;
int mini[maxn];
int maxi[maxn];
bool bio[maxn];
int dsu[maxn],siz[maxn];
int cale(int a){return a==dsu[a] ? a:cale(dsu[a]);}
void join(int a,int b){
a=cale(a); b=cale(b);
if(a==b) return;
if(siz[a]<siz[b]) swap(a,b);
dsu[b]=a;
siz[a]+=siz[b];
maxi[a]=max(maxi[a],maxi[b]);
mini[a]=min(mini[a],mini[b]);
}
void init(){
for(int i=0;i<n;i++){dsu[i]=i; siz[i]=1; maxi[i]=i; mini[i]=i;}
}
ll minimum_walk(vector<int> p, int s) {
n=p.size();
init();
ll ret=0;
int imal=n,imar=-1;
for(int i=0;i<n;i++){
if(p[i]!=i) imal=min(imal,i);
if(p[i]!=i) imar=i;
ret+=abs(p[i]-i);
if(bio[i]) continue;
int tren=i;
while(!bio[tren]){
bio[tren]=true;
join(tren,p[tren]);
tren=p[tren];
}
}
imal=min(imal,s);
imar=max(imar,s);
int l=s,r=s;
int tl=mini[cale(s)],tr=maxi[cale(s)];
int dodaol=0,dodaor=0;
while(l!=imal || r!=imar){
bool pl=false,pr=false;
if(l!=tl || r!=tr){
if(l!=tl){
l--;
tl=min(tl,mini[cale(l)]);
tr=max(tr,maxi[cale(l)]);
}
if(r!=tr){
r++;
tl=min(tl,mini[cale(r)]);
tr=max(tr,maxi[cale(r)]);
}
continue;
}
while(l>imal && maxi[cale(l-1)]<=r){
l--;
if(l<tl){
ret+=2;
dodaol++;
}
tl=min(tl,mini[cale(l)]);
}
while(r<imar && mini[cale(r+1)]>=l){
r++;
if(r>tr){
ret+=2;
dodaor++;
}
tr=max(tr,maxi[cale(r)]);
}
if(l==imal && r==imar) break;
ret-=2*max(dodaol-int(r>=tr),dodaor-int(l<=tl));
l--;
tl=min(tl,mini[cale(l)]);
tr=max(tr,maxi[cale(l)]);
dodaol=0;
dodaor=0;
}
return ret;
}
Compilation message
books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:52:8: warning: unused variable 'pl' [-Wunused-variable]
52 | bool pl=false,pr=false;
| ^~
books.cpp:52:17: warning: unused variable 'pr' [-Wunused-variable]
52 | bool pl=false,pr=false;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
179 ms |
24704 KB |
Output is correct |
31 |
Correct |
174 ms |
24708 KB |
Output is correct |
32 |
Correct |
91 ms |
24696 KB |
Output is correct |
33 |
Correct |
94 ms |
24696 KB |
Output is correct |
34 |
Correct |
94 ms |
24692 KB |
Output is correct |
35 |
Correct |
99 ms |
24696 KB |
Output is correct |
36 |
Correct |
99 ms |
24752 KB |
Output is correct |
37 |
Correct |
94 ms |
24688 KB |
Output is correct |
38 |
Correct |
95 ms |
24692 KB |
Output is correct |
39 |
Correct |
109 ms |
24708 KB |
Output is correct |
40 |
Correct |
99 ms |
24700 KB |
Output is correct |
41 |
Correct |
120 ms |
24700 KB |
Output is correct |
42 |
Correct |
110 ms |
24696 KB |
Output is correct |
43 |
Correct |
102 ms |
24692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
179 ms |
24704 KB |
Output is correct |
31 |
Correct |
174 ms |
24708 KB |
Output is correct |
32 |
Correct |
91 ms |
24696 KB |
Output is correct |
33 |
Correct |
94 ms |
24696 KB |
Output is correct |
34 |
Correct |
94 ms |
24692 KB |
Output is correct |
35 |
Correct |
99 ms |
24696 KB |
Output is correct |
36 |
Correct |
99 ms |
24752 KB |
Output is correct |
37 |
Correct |
94 ms |
24688 KB |
Output is correct |
38 |
Correct |
95 ms |
24692 KB |
Output is correct |
39 |
Correct |
109 ms |
24708 KB |
Output is correct |
40 |
Correct |
99 ms |
24700 KB |
Output is correct |
41 |
Correct |
120 ms |
24700 KB |
Output is correct |
42 |
Correct |
110 ms |
24696 KB |
Output is correct |
43 |
Correct |
102 ms |
24692 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
312 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
96 ms |
31468 KB |
Output is correct |
65 |
Correct |
92 ms |
31464 KB |
Output is correct |
66 |
Correct |
99 ms |
31464 KB |
Output is correct |
67 |
Correct |
95 ms |
31472 KB |
Output is correct |
68 |
Correct |
10 ms |
3256 KB |
Output is correct |
69 |
Correct |
9 ms |
3284 KB |
Output is correct |
70 |
Correct |
9 ms |
3284 KB |
Output is correct |
71 |
Correct |
12 ms |
3284 KB |
Output is correct |
72 |
Correct |
9 ms |
3336 KB |
Output is correct |
73 |
Correct |
10 ms |
3252 KB |
Output is correct |
74 |
Correct |
92 ms |
31472 KB |
Output is correct |
75 |
Correct |
92 ms |
31460 KB |
Output is correct |
76 |
Correct |
101 ms |
31476 KB |
Output is correct |
77 |
Correct |
94 ms |
31468 KB |
Output is correct |
78 |
Correct |
95 ms |
31476 KB |
Output is correct |
79 |
Correct |
94 ms |
31464 KB |
Output is correct |
80 |
Correct |
80 ms |
31464 KB |
Output is correct |
81 |
Correct |
95 ms |
31464 KB |
Output is correct |
82 |
Correct |
85 ms |
31484 KB |
Output is correct |
83 |
Correct |
94 ms |
31400 KB |
Output is correct |
84 |
Correct |
95 ms |
31480 KB |
Output is correct |
85 |
Correct |
94 ms |
31464 KB |
Output is correct |
86 |
Correct |
91 ms |
31592 KB |
Output is correct |
87 |
Correct |
89 ms |
31472 KB |
Output is correct |
88 |
Correct |
92 ms |
31464 KB |
Output is correct |
89 |
Correct |
114 ms |
31464 KB |
Output is correct |
90 |
Correct |
106 ms |
31436 KB |
Output is correct |
91 |
Correct |
93 ms |
31460 KB |
Output is correct |
92 |
Correct |
94 ms |
31464 KB |
Output is correct |