#include <bits/stdc++.h>
#include "books.h"
using namespace std;
const long long inf = 1e18;
long long bfs(int from,
int to,
const int &n,
const vector<pair<int, int> > &range){
vector<bool> used(n, false);
vector<long long> dist(n);
deque<int> q;
for(int i = 0; i < n; ++i)
dist[i] = inf;
q.push_back(from);
dist[from] = 0;
int l = from, r = from;
while(!q.empty()){
int u = q.front();
q.pop_front();
if(u == to)
return dist[to];
if(used[u])
continue;
used[u] = true;
r = max(r, u);
l = min(l, u);
if(range[u].first < l){
for(int i = range[u].first; i < l; ++i){
if(used[i])
continue;
q.push_front(i);
dist[i] = dist[u];
}
l = range[u].first;
}
if(range[u].second > r){
for(int i = r + 1; i <= range[u].second; ++i){
if(used[i])
continue;
q.push_front(i);
dist[i] = dist[u];
}
r = range[u].second;
}
if(u + 1 < n && dist[u + 1] > dist[u] + 1){
dist[u + 1] = dist[u] + 1;
q.push_back(u + 1);
}
if(range[u].first - 1 >= 0 && dist[u - 1] > dist[u] + 1){
dist[u - 1] = dist[u] + 1;
q.push_back(u - 1);
}
}
return inf;
}
long long minimum_walk(vector<int> p, int s) {
int n = (int)p.size();
vector<int> rev(n);
vector<pair<int, int> > range(n);
long long ans = 0;
for(int i = 0; i < n; ++i){
rev[p[i]] = i;
range[i] = {i, i};
}
int l = s, r = s;
for(int i = 0; i < n; ++i){
if(p[i] != i){
l = min(l, i);
r = max(r, i);
}
}
for(int i = 0; i < n; ++i){
if(p[i] != i){
vector<int> cycle;
int v = i;
do{
ans += abs(p[v] - v);
cycle.push_back(v);
int prev = v;
v = p[v];
p[prev] = prev;
}
while(v != i);
int l_cycle = cycle[0], r_cycle = cycle[0];
for(int x: cycle){
l_cycle = min(l_cycle, x);
r_cycle = max(r_cycle, x);
}
for(int x: cycle)
range[x] = {l_cycle, r_cycle};
}
}
long long l_ans = bfs(s, l, n, range);
long long r_ans = bfs(s, r, n, range);
ans += r_ans + l_ans + bfs(l, r, n, range);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
252 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
368 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
252 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
368 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
3 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
252 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
368 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
3 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
295 ms |
34152 KB |
Output is correct |
31 |
Correct |
292 ms |
34120 KB |
Output is correct |
32 |
Correct |
159 ms |
30072 KB |
Output is correct |
33 |
Correct |
241 ms |
33660 KB |
Output is correct |
34 |
Correct |
234 ms |
32380 KB |
Output is correct |
35 |
Correct |
243 ms |
33656 KB |
Output is correct |
36 |
Correct |
237 ms |
33528 KB |
Output is correct |
37 |
Correct |
243 ms |
32544 KB |
Output is correct |
38 |
Correct |
223 ms |
33120 KB |
Output is correct |
39 |
Correct |
220 ms |
32632 KB |
Output is correct |
40 |
Correct |
241 ms |
33944 KB |
Output is correct |
41 |
Correct |
285 ms |
36336 KB |
Output is correct |
42 |
Correct |
282 ms |
35256 KB |
Output is correct |
43 |
Correct |
229 ms |
38240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
356 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
380 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
504 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
252 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
368 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
3 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
295 ms |
34152 KB |
Output is correct |
31 |
Correct |
292 ms |
34120 KB |
Output is correct |
32 |
Correct |
159 ms |
30072 KB |
Output is correct |
33 |
Correct |
241 ms |
33660 KB |
Output is correct |
34 |
Correct |
234 ms |
32380 KB |
Output is correct |
35 |
Correct |
243 ms |
33656 KB |
Output is correct |
36 |
Correct |
237 ms |
33528 KB |
Output is correct |
37 |
Correct |
243 ms |
32544 KB |
Output is correct |
38 |
Correct |
223 ms |
33120 KB |
Output is correct |
39 |
Correct |
220 ms |
32632 KB |
Output is correct |
40 |
Correct |
241 ms |
33944 KB |
Output is correct |
41 |
Correct |
285 ms |
36336 KB |
Output is correct |
42 |
Correct |
282 ms |
35256 KB |
Output is correct |
43 |
Correct |
229 ms |
38240 KB |
Output is correct |
44 |
Correct |
2 ms |
376 KB |
Output is correct |
45 |
Correct |
2 ms |
504 KB |
Output is correct |
46 |
Correct |
2 ms |
376 KB |
Output is correct |
47 |
Correct |
3 ms |
376 KB |
Output is correct |
48 |
Correct |
3 ms |
376 KB |
Output is correct |
49 |
Correct |
3 ms |
376 KB |
Output is correct |
50 |
Correct |
2 ms |
356 KB |
Output is correct |
51 |
Correct |
3 ms |
376 KB |
Output is correct |
52 |
Correct |
2 ms |
376 KB |
Output is correct |
53 |
Correct |
2 ms |
376 KB |
Output is correct |
54 |
Correct |
2 ms |
380 KB |
Output is correct |
55 |
Correct |
2 ms |
376 KB |
Output is correct |
56 |
Correct |
2 ms |
376 KB |
Output is correct |
57 |
Correct |
3 ms |
376 KB |
Output is correct |
58 |
Correct |
2 ms |
376 KB |
Output is correct |
59 |
Correct |
2 ms |
376 KB |
Output is correct |
60 |
Correct |
2 ms |
376 KB |
Output is correct |
61 |
Correct |
4 ms |
504 KB |
Output is correct |
62 |
Correct |
2 ms |
376 KB |
Output is correct |
63 |
Correct |
2 ms |
376 KB |
Output is correct |
64 |
Correct |
226 ms |
38780 KB |
Output is correct |
65 |
Correct |
227 ms |
38676 KB |
Output is correct |
66 |
Correct |
232 ms |
38772 KB |
Output is correct |
67 |
Correct |
234 ms |
38820 KB |
Output is correct |
68 |
Correct |
23 ms |
4088 KB |
Output is correct |
69 |
Correct |
24 ms |
4072 KB |
Output is correct |
70 |
Correct |
23 ms |
4088 KB |
Output is correct |
71 |
Correct |
23 ms |
4016 KB |
Output is correct |
72 |
Correct |
24 ms |
4068 KB |
Output is correct |
73 |
Correct |
25 ms |
4140 KB |
Output is correct |
74 |
Correct |
252 ms |
34752 KB |
Output is correct |
75 |
Correct |
254 ms |
34656 KB |
Output is correct |
76 |
Correct |
239 ms |
38744 KB |
Output is correct |
77 |
Correct |
241 ms |
38736 KB |
Output is correct |
78 |
Correct |
258 ms |
37928 KB |
Output is correct |
79 |
Correct |
250 ms |
38152 KB |
Output is correct |
80 |
Correct |
159 ms |
34720 KB |
Output is correct |
81 |
Correct |
257 ms |
38748 KB |
Output is correct |
82 |
Correct |
258 ms |
38632 KB |
Output is correct |
83 |
Correct |
271 ms |
38768 KB |
Output is correct |
84 |
Correct |
266 ms |
38848 KB |
Output is correct |
85 |
Correct |
239 ms |
38756 KB |
Output is correct |
86 |
Correct |
235 ms |
38744 KB |
Output is correct |
87 |
Correct |
254 ms |
38752 KB |
Output is correct |
88 |
Correct |
251 ms |
38752 KB |
Output is correct |
89 |
Correct |
257 ms |
38768 KB |
Output is correct |
90 |
Correct |
233 ms |
38860 KB |
Output is correct |
91 |
Correct |
233 ms |
38640 KB |
Output is correct |
92 |
Correct |
234 ms |
38720 KB |
Output is correct |