#include "books.h"
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
using namespace std;
using ll = long long int;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
const int inf = 1.05e9;
const ll INF = 1e18;
ll minimum_walk(vector<int> p, int s) {
ll ans = 0, mx = 0, mx1 = 0, mx2 = 0;
int n = p.size(), l = s, r = s;
ll d[n], g[n];
memset(d, -1, sizeof d);
memset(g, -1, sizeof g);
d[s] = 0;
queue<int> q[2];
q[0].push(s);
while(true){
while(!q[0].empty()){
int v = q[0].front();
q[0].pop();
for(int i = p[v]; d[i] == -1; i = p[i]){
d[i] = d[v];
q[0].push(i);
}
while(l > v){
l--;
if(d[l] == -1){
d[l] = d[v];
q[0].push(l);
}
}
while(r < v){
r++;
if(d[r] == -1){
d[r] = d[v];
q[0].push(r);
}
}
if(v && g[v - 1] == -1){
g[v - 1] = d[v] + 1;
q[1].push(v - 1);
}
if(v + 1 < n && g[v + 1] == -1){
g[v + 1] = d[v] + 1;
q[1].push(v + 1);
}
}
if(q[1].empty()) break;
int v = q[1].front();
q[1].pop();
if(d[v] != -1) continue;
d[v] = g[v];
for(int i = p[v]; d[i] == -1; i = p[i]){
d[i] = d[v];
q[0].push(i);
}
while(l > v){
l--;
if(d[l] == -1){
d[l] = d[v];
q[0].push(l);
}
}
while(r < v){
r++;
if(d[r] == -1){
d[r] = d[v];
q[0].push(r);
}
}
if(v && g[v - 1] == -1){
g[v - 1] = d[v] + 1;
q[1].push(v - 1);
}
if(v + 1 < n && g[v + 1] == -1){
g[v + 1] = d[v] + 1;
q[1].push(v + 1);
}
}
for(int i = 0; i < n; i++){
ans += abs(i - p[i]);
if(i <= s && p[i] >= s) mx = max(mx, d[i]);
if(i <= s && i != p[i]) mx1 = max(mx1, d[i]);
if(i >= s && i != p[i]) mx2 = max(mx2, d[i]);
}
return ans + 2 * (mx1 + mx2 - mx);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
1 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 |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
1 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 |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 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 |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
304 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
296 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
1 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 |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 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 |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
304 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
296 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
307 ms |
29112 KB |
Output is correct |
31 |
Correct |
211 ms |
29100 KB |
Output is correct |
32 |
Correct |
129 ms |
23968 KB |
Output is correct |
33 |
Correct |
154 ms |
23976 KB |
Output is correct |
34 |
Correct |
175 ms |
23864 KB |
Output is correct |
35 |
Correct |
140 ms |
23872 KB |
Output is correct |
36 |
Correct |
144 ms |
23976 KB |
Output is correct |
37 |
Correct |
141 ms |
23980 KB |
Output is correct |
38 |
Correct |
164 ms |
24000 KB |
Output is correct |
39 |
Correct |
178 ms |
23992 KB |
Output is correct |
40 |
Correct |
156 ms |
24128 KB |
Output is correct |
41 |
Correct |
201 ms |
26660 KB |
Output is correct |
42 |
Correct |
170 ms |
25904 KB |
Output is correct |
43 |
Correct |
164 ms |
28256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
296 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 |
300 KB |
Output is correct |
14 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
1 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
1 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 |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 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 |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
304 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
296 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
307 ms |
29112 KB |
Output is correct |
31 |
Correct |
211 ms |
29100 KB |
Output is correct |
32 |
Correct |
129 ms |
23968 KB |
Output is correct |
33 |
Correct |
154 ms |
23976 KB |
Output is correct |
34 |
Correct |
175 ms |
23864 KB |
Output is correct |
35 |
Correct |
140 ms |
23872 KB |
Output is correct |
36 |
Correct |
144 ms |
23976 KB |
Output is correct |
37 |
Correct |
141 ms |
23980 KB |
Output is correct |
38 |
Correct |
164 ms |
24000 KB |
Output is correct |
39 |
Correct |
178 ms |
23992 KB |
Output is correct |
40 |
Correct |
156 ms |
24128 KB |
Output is correct |
41 |
Correct |
201 ms |
26660 KB |
Output is correct |
42 |
Correct |
170 ms |
25904 KB |
Output is correct |
43 |
Correct |
164 ms |
28256 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 |
300 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
296 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
296 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
1 ms |
300 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
304 KB |
Output is correct |
61 |
Correct |
1 ms |
212 KB |
Output is correct |
62 |
Correct |
1 ms |
296 KB |
Output is correct |
63 |
Correct |
1 ms |
300 KB |
Output is correct |
64 |
Correct |
150 ms |
34284 KB |
Output is correct |
65 |
Correct |
148 ms |
33588 KB |
Output is correct |
66 |
Correct |
167 ms |
31208 KB |
Output is correct |
67 |
Correct |
146 ms |
33556 KB |
Output is correct |
68 |
Correct |
14 ms |
3588 KB |
Output is correct |
69 |
Correct |
16 ms |
3324 KB |
Output is correct |
70 |
Correct |
14 ms |
3624 KB |
Output is correct |
71 |
Correct |
22 ms |
3492 KB |
Output is correct |
72 |
Correct |
15 ms |
3252 KB |
Output is correct |
73 |
Correct |
20 ms |
3536 KB |
Output is correct |
74 |
Correct |
129 ms |
30436 KB |
Output is correct |
75 |
Correct |
140 ms |
30440 KB |
Output is correct |
76 |
Correct |
141 ms |
34900 KB |
Output is correct |
77 |
Correct |
143 ms |
34776 KB |
Output is correct |
78 |
Correct |
175 ms |
32096 KB |
Output is correct |
79 |
Correct |
153 ms |
31916 KB |
Output is correct |
80 |
Correct |
140 ms |
30520 KB |
Output is correct |
81 |
Correct |
132 ms |
30824 KB |
Output is correct |
82 |
Correct |
158 ms |
30700 KB |
Output is correct |
83 |
Correct |
151 ms |
30448 KB |
Output is correct |
84 |
Correct |
137 ms |
30436 KB |
Output is correct |
85 |
Correct |
156 ms |
30444 KB |
Output is correct |
86 |
Correct |
181 ms |
30516 KB |
Output is correct |
87 |
Correct |
147 ms |
30436 KB |
Output is correct |
88 |
Correct |
148 ms |
30524 KB |
Output is correct |
89 |
Correct |
146 ms |
30444 KB |
Output is correct |
90 |
Correct |
180 ms |
30516 KB |
Output is correct |
91 |
Correct |
158 ms |
30560 KB |
Output is correct |
92 |
Correct |
164 ms |
30864 KB |
Output is correct |