//#define TEST
#ifndef TEST
#include "books.h"
#endif
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;
const int len = 1e6+6;
int n, per[len], dis[len], lef[len], rig[len];
int query(int st, int en){
dis[st] = 0;
int l = st, r = st, mn = lef[st], mx = rig[st], cnt = 0;
while (0 < l || r < n-1){
//printf("cnt = %d\n", cnt);
while (mn < l || r < mx){
if (mn < l){
l--;
dis[l] = cnt;
mn = min(mn, lef[l]);
mx = max(mx, rig[l]);
}
if (r < mx){
r++;
dis[r] = cnt;
mn = min(mn, lef[r]);
mx = max(mx, rig[r]);
}
//printf("l = %d, r = %d, mn = %d, mx = %d\n", l, r, mn, mx);
}
if (l > 0)
mn = min(mn, lef[l-1]), mx = max(mx, rig[l-1]);
if (r < n-1)
mn = min(mn, lef[r+1]), mx = max(mx, rig[r+1]);
cnt++;
}
//printf("dis starting from %d:\n", st);
//for (int i = 0; i < n; i++)
// printf("i = %d, dis = %d\n", i, dis[i]);
return dis[en];
}
ll minimum_walk(vector<int> P, int s){
n = P.size();
for (int i = 0; i < n; i++)
per[i] = P[i];
for (int i = 0; i < n; i++)
lef[i] = -1;
for (int i = 0; i < n; i++){
if (lef[i] == -1){
int cur = per[i], mn = i, mx = i;
while (cur != i){
mn = min(mn, cur);
mx = max(mx, cur);
cur = per[cur];
}
lef[i] = mn, rig[i] = mx;
cur = per[i];
while (cur != i){
lef[cur] = mn, rig[cur] = mx;
cur = per[cur];
}
}
//printf("i = %d, mn = %d, mx = %d\n", i, lef[i], rig[i]);
}
ll ans = 0;
for (int i = 0; i < n; i++)
ans += abs(per[i]-i);
int a = 0, b = n-1;
while (a < s && per[a] == a)
a++;
while (b > s && per[b] == b)
b--;
ans += 2*query(s, a) + 2*query(b, s);
return ans;
}
#ifdef TEST
int main() {
int n, s, temp;
assert(2 == scanf("%d %d", &n, &s));
vector<int> p;
for(int i = 0; i < n; i++){
scanf("%d", &temp);
p.pb(temp);
}
long long res = minimum_walk(p, s);
printf("%lld\n", res);
return 0;
}
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 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 |
400 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 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 |
380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 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 |
400 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 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 |
380 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 |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
3 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 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 |
400 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 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 |
380 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 |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
3 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 |
253 ms |
24312 KB |
Output is correct |
31 |
Correct |
275 ms |
24416 KB |
Output is correct |
32 |
Correct |
153 ms |
24312 KB |
Output is correct |
33 |
Correct |
166 ms |
24252 KB |
Output is correct |
34 |
Correct |
165 ms |
24316 KB |
Output is correct |
35 |
Correct |
169 ms |
24252 KB |
Output is correct |
36 |
Correct |
168 ms |
24368 KB |
Output is correct |
37 |
Correct |
162 ms |
24312 KB |
Output is correct |
38 |
Correct |
169 ms |
24256 KB |
Output is correct |
39 |
Correct |
167 ms |
24312 KB |
Output is correct |
40 |
Correct |
181 ms |
24276 KB |
Output is correct |
41 |
Correct |
218 ms |
24316 KB |
Output is correct |
42 |
Correct |
210 ms |
24312 KB |
Output is correct |
43 |
Correct |
165 ms |
24272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 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 |
380 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 |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
372 KB |
Output is correct |
13 |
Correct |
2 ms |
376 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 |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 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 |
400 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 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 |
380 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 |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
3 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 |
253 ms |
24312 KB |
Output is correct |
31 |
Correct |
275 ms |
24416 KB |
Output is correct |
32 |
Correct |
153 ms |
24312 KB |
Output is correct |
33 |
Correct |
166 ms |
24252 KB |
Output is correct |
34 |
Correct |
165 ms |
24316 KB |
Output is correct |
35 |
Correct |
169 ms |
24252 KB |
Output is correct |
36 |
Correct |
168 ms |
24368 KB |
Output is correct |
37 |
Correct |
162 ms |
24312 KB |
Output is correct |
38 |
Correct |
169 ms |
24256 KB |
Output is correct |
39 |
Correct |
167 ms |
24312 KB |
Output is correct |
40 |
Correct |
181 ms |
24276 KB |
Output is correct |
41 |
Correct |
218 ms |
24316 KB |
Output is correct |
42 |
Correct |
210 ms |
24312 KB |
Output is correct |
43 |
Correct |
165 ms |
24272 KB |
Output is correct |
44 |
Correct |
2 ms |
376 KB |
Output is correct |
45 |
Correct |
2 ms |
376 KB |
Output is correct |
46 |
Correct |
2 ms |
376 KB |
Output is correct |
47 |
Correct |
2 ms |
376 KB |
Output is correct |
48 |
Correct |
2 ms |
504 KB |
Output is correct |
49 |
Correct |
2 ms |
376 KB |
Output is correct |
50 |
Correct |
2 ms |
376 KB |
Output is correct |
51 |
Correct |
2 ms |
380 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 |
376 KB |
Output is correct |
55 |
Correct |
2 ms |
372 KB |
Output is correct |
56 |
Correct |
2 ms |
376 KB |
Output is correct |
57 |
Correct |
2 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 |
2 ms |
376 KB |
Output is correct |
62 |
Correct |
2 ms |
376 KB |
Output is correct |
63 |
Correct |
2 ms |
376 KB |
Output is correct |
64 |
Correct |
157 ms |
24272 KB |
Output is correct |
65 |
Correct |
155 ms |
24424 KB |
Output is correct |
66 |
Correct |
164 ms |
24312 KB |
Output is correct |
67 |
Correct |
166 ms |
24312 KB |
Output is correct |
68 |
Correct |
17 ms |
3192 KB |
Output is correct |
69 |
Correct |
18 ms |
3192 KB |
Output is correct |
70 |
Correct |
17 ms |
3192 KB |
Output is correct |
71 |
Correct |
17 ms |
3192 KB |
Output is correct |
72 |
Correct |
17 ms |
3192 KB |
Output is correct |
73 |
Correct |
19 ms |
3192 KB |
Output is correct |
74 |
Correct |
165 ms |
24268 KB |
Output is correct |
75 |
Correct |
167 ms |
24312 KB |
Output is correct |
76 |
Correct |
161 ms |
24380 KB |
Output is correct |
77 |
Correct |
159 ms |
24260 KB |
Output is correct |
78 |
Correct |
164 ms |
24440 KB |
Output is correct |
79 |
Correct |
159 ms |
24184 KB |
Output is correct |
80 |
Correct |
155 ms |
24272 KB |
Output is correct |
81 |
Correct |
151 ms |
24136 KB |
Output is correct |
82 |
Correct |
150 ms |
24132 KB |
Output is correct |
83 |
Correct |
161 ms |
24256 KB |
Output is correct |
84 |
Correct |
161 ms |
24184 KB |
Output is correct |
85 |
Correct |
169 ms |
24220 KB |
Output is correct |
86 |
Correct |
161 ms |
24184 KB |
Output is correct |
87 |
Correct |
168 ms |
24184 KB |
Output is correct |
88 |
Correct |
164 ms |
24260 KB |
Output is correct |
89 |
Correct |
161 ms |
24056 KB |
Output is correct |
90 |
Correct |
162 ms |
24056 KB |
Output is correct |
91 |
Correct |
166 ms |
24084 KB |
Output is correct |
92 |
Correct |
177 ms |
23992 KB |
Output is correct |