#include "books.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<pair<int,int>> segs;
vector<int> vis;
pair<int,int> getNext(int x, bool r){
pair<int,int> bound = {x, x};
if(r) {
for(int i = x; i <= bound.second; ++i){
bound.first = min(bound.first,segs[vis[i]].first);
bound.second = max(bound.second,segs[vis[i]].second);
}
} else {
for(int i = x; i >= bound.first; --i){
bound.first = min(bound.first,segs[vis[i]].first);
bound.second = max(bound.second,segs[vis[i]].second);
}
}
return bound;
}
ll minimum_walk(vector<int> p, int s) {
int n = p.size(), mx = 0, cn = 0;
ll ans = 0;
segs.resize(n+1,{n,0});
vis.resize(n,0);
for(int i = 0; i < n; ++i) {
if(!vis[i]) {
cn++;
while(!vis[i]) {
ans+=abs(i-p[i]);
vis[i] = cn;
i = p[i]; // goes back to original i
}
}
}
for(int i = 0; i < n; ++i) {
segs[vis[i]].first = min(segs[vis[i]].first,i);
segs[vis[i]].second = max(segs[vis[i]].second,i);
}
queue<int> Q;
Q.push(s);
int l = s, r = s;
while(Q.size()){
auto x = Q.front();
Q.pop();
for(int i = segs[vis[x]].first; i < l; ++i){
Q.push(i);
}
for(int i = segs[vis[x]].second; i > r; --i){
Q.push(i);
}
l = min(l,segs[vis[x]].first);
r = max(r,segs[vis[x]].second);
}
int cnt = 0;
int cua = 0, cub = 0;
while(l || r < n-1){
cnt++;
pair<int,int> a = {r,r}, b = {l,l};
if(r < n-1)a = getNext(r+1,1);
if(l)b = getNext(l-1,0);
if(a.first == a.second)cua++;
else cua = 0;
if(b.first == b.second)cub++;
else cub = 0;
if(a.first <= b.second){
ans+=2*cnt;
cnt = 0;
cua = cub = 0;
}
pair<int,int> nbound = {min(a.first,b.first),max(a.second,b.second)};
for(int i = nbound.first; i < l; ++i){
Q.push(i);
}
for(int i = nbound.second; i > r; --i){
Q.push(i);
}
while(Q.size()){
auto x = Q.front();
Q.pop();
for(int i = segs[vis[x]].first; i < l; ++i){
Q.push(i);
}
for(int i = segs[vis[x]].second; i > r; --i){
Q.push(i);
}
l = min(l,segs[vis[x]].first);
r = max(r,segs[vis[x]].second);
}
}
ans+=4*cnt-2*(cua+cub);
return ans;
}
Compilation message
books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:28:20: warning: unused variable 'mx' [-Wunused-variable]
28 | int n = p.size(), mx = 0, cn = 0;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
0 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
296 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
0 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
296 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
296 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
300 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
304 KB |
Output is correct |
29 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
0 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
296 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
296 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
300 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
304 KB |
Output is correct |
29 |
Correct |
1 ms |
308 KB |
Output is correct |
30 |
Correct |
183 ms |
30692 KB |
Output is correct |
31 |
Correct |
167 ms |
30728 KB |
Output is correct |
32 |
Correct |
142 ms |
26596 KB |
Output is correct |
33 |
Correct |
138 ms |
26588 KB |
Output is correct |
34 |
Correct |
133 ms |
26588 KB |
Output is correct |
35 |
Correct |
139 ms |
26588 KB |
Output is correct |
36 |
Correct |
137 ms |
26468 KB |
Output is correct |
37 |
Correct |
118 ms |
26588 KB |
Output is correct |
38 |
Correct |
125 ms |
26588 KB |
Output is correct |
39 |
Correct |
133 ms |
26584 KB |
Output is correct |
40 |
Correct |
129 ms |
26980 KB |
Output is correct |
41 |
Correct |
136 ms |
30688 KB |
Output is correct |
42 |
Correct |
148 ms |
29780 KB |
Output is correct |
43 |
Correct |
124 ms |
30696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
248 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 |
304 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
0 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
296 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
296 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
300 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
304 KB |
Output is correct |
29 |
Correct |
1 ms |
308 KB |
Output is correct |
30 |
Correct |
183 ms |
30692 KB |
Output is correct |
31 |
Correct |
167 ms |
30728 KB |
Output is correct |
32 |
Correct |
142 ms |
26596 KB |
Output is correct |
33 |
Correct |
138 ms |
26588 KB |
Output is correct |
34 |
Correct |
133 ms |
26588 KB |
Output is correct |
35 |
Correct |
139 ms |
26588 KB |
Output is correct |
36 |
Correct |
137 ms |
26468 KB |
Output is correct |
37 |
Correct |
118 ms |
26588 KB |
Output is correct |
38 |
Correct |
125 ms |
26588 KB |
Output is correct |
39 |
Correct |
133 ms |
26584 KB |
Output is correct |
40 |
Correct |
129 ms |
26980 KB |
Output is correct |
41 |
Correct |
136 ms |
30688 KB |
Output is correct |
42 |
Correct |
148 ms |
29780 KB |
Output is correct |
43 |
Correct |
124 ms |
30696 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
248 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
1 ms |
304 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
304 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
1 ms |
212 KB |
Output is correct |
62 |
Correct |
1 ms |
212 KB |
Output is correct |
63 |
Correct |
1 ms |
212 KB |
Output is correct |
64 |
Correct |
127 ms |
33796 KB |
Output is correct |
65 |
Correct |
126 ms |
32360 KB |
Output is correct |
66 |
Correct |
124 ms |
27596 KB |
Output is correct |
67 |
Correct |
129 ms |
30444 KB |
Output is correct |
68 |
Correct |
13 ms |
3540 KB |
Output is correct |
69 |
Correct |
13 ms |
3028 KB |
Output is correct |
70 |
Correct |
14 ms |
3512 KB |
Output is correct |
71 |
Correct |
13 ms |
3412 KB |
Output is correct |
72 |
Correct |
13 ms |
2860 KB |
Output is correct |
73 |
Correct |
13 ms |
3256 KB |
Output is correct |
74 |
Correct |
133 ms |
26616 KB |
Output is correct |
75 |
Correct |
149 ms |
26612 KB |
Output is correct |
76 |
Correct |
133 ms |
34804 KB |
Output is correct |
77 |
Correct |
126 ms |
34804 KB |
Output is correct |
78 |
Correct |
135 ms |
28912 KB |
Output is correct |
79 |
Correct |
150 ms |
28776 KB |
Output is correct |
80 |
Correct |
128 ms |
26600 KB |
Output is correct |
81 |
Correct |
121 ms |
27236 KB |
Output is correct |
82 |
Correct |
124 ms |
27112 KB |
Output is correct |
83 |
Correct |
126 ms |
26596 KB |
Output is correct |
84 |
Correct |
131 ms |
26652 KB |
Output is correct |
85 |
Correct |
130 ms |
26604 KB |
Output is correct |
86 |
Correct |
129 ms |
26608 KB |
Output is correct |
87 |
Correct |
130 ms |
26596 KB |
Output is correct |
88 |
Correct |
134 ms |
26612 KB |
Output is correct |
89 |
Correct |
136 ms |
26732 KB |
Output is correct |
90 |
Correct |
124 ms |
26612 KB |
Output is correct |
91 |
Correct |
122 ms |
26648 KB |
Output is correct |
92 |
Correct |
131 ms |
26856 KB |
Output is correct |