#include <bits/stdc++.h>
#include "books.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define sep ' '
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define SZ(x) int(x.size())
const int MAXN = 1e6 + 10;
ll n , s , sum , P[MAXN] , mark[MAXN] , L[MAXN] , R[MAXN] , ps[MAXN];
ll minimum_walk(vector<int> p, int S) {
n = SZ(p); s = S;
for(int i = 0 ; i < n ; i++){
P[i] = p[i];
sum += abs(P[i] - i);
}
int l = 0 , r = n - 1;
while(r > l && r > s && P[r] == r) r--;
while(l <= r && l < s && P[l] == l) l++;
if(l == r && P[l] == P[r]){
return 0;
}
for(int i = l ; i <= r ; i++){
L[i] = R[i] = i;
}
for(int i = l ; i <= r ; i++){
if(!mark[i]){
vector<int> V;
int x = i;
while(!mark[x]){
mark[x] = 1;
V.push_back(x);
x = P[x];
}
sort(all(V));
for(int j = 0 ; j + 1 < SZ(V) ; j++){
int v = V[j] , u = V[j + 1];
R[v] = u; L[u] = v;
ps[v]++; ps[u]--;
}
}
}
partial_sum(ps , ps + MAXN , ps);
vector<int> comps;
for(int i = l ; i <= r ; i++){
if(ps[i] == 0){
comps.push_back(i);
}
}
int t = *lower_bound(all(comps) , s);
int curl = s , curr = s;
int mnl = L[s] , mxr = R[s];
ll ans = sum + SZ(comps) * 2 - 2;
while(1){
while(mnl != curl || mxr != curr){
if(mnl != curl){
curl--;
mnl = min((ll)mnl , L[curl]);
mxr = max((ll)mxr , R[curl]);
}
else{
curr++;
mnl = min((ll)mnl , L[curr]);
mxr = max((ll)mxr , R[curr]);
}
}
if(curr >= t){
break;
}
ans += 2;
curl--;
mnl = min((ll)mnl , L[curl]);
mxr = max((ll)mxr , R[curl]);
curr++;
mnl = min((ll)mnl , L[curr]);
mxr = max((ll)mxr , R[curr]);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
8148 KB |
Output is correct |
2 |
Correct |
7 ms |
8116 KB |
Output is correct |
3 |
Correct |
6 ms |
8148 KB |
Output is correct |
4 |
Correct |
6 ms |
8112 KB |
Output is correct |
5 |
Correct |
6 ms |
8148 KB |
Output is correct |
6 |
Correct |
5 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
11 ms |
8148 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
7 ms |
8148 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
6 ms |
8148 KB |
Output is correct |
13 |
Correct |
6 ms |
8148 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
7 ms |
8060 KB |
Output is correct |
16 |
Correct |
6 ms |
8112 KB |
Output is correct |
17 |
Correct |
7 ms |
8148 KB |
Output is correct |
18 |
Correct |
7 ms |
8112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
8148 KB |
Output is correct |
2 |
Correct |
7 ms |
8116 KB |
Output is correct |
3 |
Correct |
6 ms |
8148 KB |
Output is correct |
4 |
Correct |
6 ms |
8112 KB |
Output is correct |
5 |
Correct |
6 ms |
8148 KB |
Output is correct |
6 |
Correct |
5 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
11 ms |
8148 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
7 ms |
8148 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
6 ms |
8148 KB |
Output is correct |
13 |
Correct |
6 ms |
8148 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
7 ms |
8060 KB |
Output is correct |
16 |
Correct |
6 ms |
8112 KB |
Output is correct |
17 |
Correct |
7 ms |
8148 KB |
Output is correct |
18 |
Correct |
7 ms |
8112 KB |
Output is correct |
19 |
Correct |
7 ms |
8200 KB |
Output is correct |
20 |
Correct |
7 ms |
8148 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
16 ms |
8112 KB |
Output is correct |
23 |
Correct |
13 ms |
8148 KB |
Output is correct |
24 |
Correct |
7 ms |
8196 KB |
Output is correct |
25 |
Correct |
7 ms |
8112 KB |
Output is correct |
26 |
Correct |
7 ms |
8116 KB |
Output is correct |
27 |
Correct |
8 ms |
8116 KB |
Output is correct |
28 |
Correct |
8 ms |
8176 KB |
Output is correct |
29 |
Correct |
8 ms |
8192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
8148 KB |
Output is correct |
2 |
Correct |
7 ms |
8116 KB |
Output is correct |
3 |
Correct |
6 ms |
8148 KB |
Output is correct |
4 |
Correct |
6 ms |
8112 KB |
Output is correct |
5 |
Correct |
6 ms |
8148 KB |
Output is correct |
6 |
Correct |
5 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
11 ms |
8148 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
7 ms |
8148 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
6 ms |
8148 KB |
Output is correct |
13 |
Correct |
6 ms |
8148 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
7 ms |
8060 KB |
Output is correct |
16 |
Correct |
6 ms |
8112 KB |
Output is correct |
17 |
Correct |
7 ms |
8148 KB |
Output is correct |
18 |
Correct |
7 ms |
8112 KB |
Output is correct |
19 |
Correct |
7 ms |
8200 KB |
Output is correct |
20 |
Correct |
7 ms |
8148 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
16 ms |
8112 KB |
Output is correct |
23 |
Correct |
13 ms |
8148 KB |
Output is correct |
24 |
Correct |
7 ms |
8196 KB |
Output is correct |
25 |
Correct |
7 ms |
8112 KB |
Output is correct |
26 |
Correct |
7 ms |
8116 KB |
Output is correct |
27 |
Correct |
8 ms |
8116 KB |
Output is correct |
28 |
Correct |
8 ms |
8176 KB |
Output is correct |
29 |
Correct |
8 ms |
8192 KB |
Output is correct |
30 |
Correct |
411 ms |
51560 KB |
Output is correct |
31 |
Correct |
447 ms |
50468 KB |
Output is correct |
32 |
Correct |
178 ms |
17000 KB |
Output is correct |
33 |
Correct |
267 ms |
52492 KB |
Output is correct |
34 |
Correct |
229 ms |
52656 KB |
Output is correct |
35 |
Correct |
207 ms |
50528 KB |
Output is correct |
36 |
Correct |
265 ms |
49432 KB |
Output is correct |
37 |
Correct |
263 ms |
48348 KB |
Output is correct |
38 |
Correct |
284 ms |
48364 KB |
Output is correct |
39 |
Correct |
277 ms |
48384 KB |
Output is correct |
40 |
Correct |
286 ms |
48572 KB |
Output is correct |
41 |
Correct |
338 ms |
49208 KB |
Output is correct |
42 |
Correct |
348 ms |
49284 KB |
Output is correct |
43 |
Correct |
217 ms |
48304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
8132 KB |
Output is correct |
2 |
Correct |
8 ms |
8196 KB |
Output is correct |
3 |
Correct |
8 ms |
8148 KB |
Output is correct |
4 |
Correct |
7 ms |
8116 KB |
Output is correct |
5 |
Correct |
6 ms |
8160 KB |
Output is correct |
6 |
Correct |
8 ms |
8148 KB |
Output is correct |
7 |
Correct |
8 ms |
8148 KB |
Output is correct |
8 |
Correct |
8 ms |
8164 KB |
Output is correct |
9 |
Correct |
6 ms |
8148 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
6 ms |
8196 KB |
Output is correct |
12 |
Correct |
6 ms |
8116 KB |
Output is correct |
13 |
Correct |
6 ms |
8148 KB |
Output is correct |
14 |
Correct |
6 ms |
8180 KB |
Output is correct |
15 |
Correct |
7 ms |
8148 KB |
Output is correct |
16 |
Correct |
6 ms |
8160 KB |
Output is correct |
17 |
Correct |
6 ms |
8116 KB |
Output is correct |
18 |
Correct |
8 ms |
8172 KB |
Output is correct |
19 |
Correct |
10 ms |
8152 KB |
Output is correct |
20 |
Correct |
7 ms |
8188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
8148 KB |
Output is correct |
2 |
Correct |
7 ms |
8116 KB |
Output is correct |
3 |
Correct |
6 ms |
8148 KB |
Output is correct |
4 |
Correct |
6 ms |
8112 KB |
Output is correct |
5 |
Correct |
6 ms |
8148 KB |
Output is correct |
6 |
Correct |
5 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
11 ms |
8148 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
7 ms |
8148 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
6 ms |
8148 KB |
Output is correct |
13 |
Correct |
6 ms |
8148 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
7 ms |
8060 KB |
Output is correct |
16 |
Correct |
6 ms |
8112 KB |
Output is correct |
17 |
Correct |
7 ms |
8148 KB |
Output is correct |
18 |
Correct |
7 ms |
8112 KB |
Output is correct |
19 |
Correct |
7 ms |
8200 KB |
Output is correct |
20 |
Correct |
7 ms |
8148 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
16 ms |
8112 KB |
Output is correct |
23 |
Correct |
13 ms |
8148 KB |
Output is correct |
24 |
Correct |
7 ms |
8196 KB |
Output is correct |
25 |
Correct |
7 ms |
8112 KB |
Output is correct |
26 |
Correct |
7 ms |
8116 KB |
Output is correct |
27 |
Correct |
8 ms |
8116 KB |
Output is correct |
28 |
Correct |
8 ms |
8176 KB |
Output is correct |
29 |
Correct |
8 ms |
8192 KB |
Output is correct |
30 |
Correct |
411 ms |
51560 KB |
Output is correct |
31 |
Correct |
447 ms |
50468 KB |
Output is correct |
32 |
Correct |
178 ms |
17000 KB |
Output is correct |
33 |
Correct |
267 ms |
52492 KB |
Output is correct |
34 |
Correct |
229 ms |
52656 KB |
Output is correct |
35 |
Correct |
207 ms |
50528 KB |
Output is correct |
36 |
Correct |
265 ms |
49432 KB |
Output is correct |
37 |
Correct |
263 ms |
48348 KB |
Output is correct |
38 |
Correct |
284 ms |
48364 KB |
Output is correct |
39 |
Correct |
277 ms |
48384 KB |
Output is correct |
40 |
Correct |
286 ms |
48572 KB |
Output is correct |
41 |
Correct |
338 ms |
49208 KB |
Output is correct |
42 |
Correct |
348 ms |
49284 KB |
Output is correct |
43 |
Correct |
217 ms |
48304 KB |
Output is correct |
44 |
Correct |
10 ms |
8132 KB |
Output is correct |
45 |
Correct |
8 ms |
8196 KB |
Output is correct |
46 |
Correct |
8 ms |
8148 KB |
Output is correct |
47 |
Correct |
7 ms |
8116 KB |
Output is correct |
48 |
Correct |
6 ms |
8160 KB |
Output is correct |
49 |
Correct |
8 ms |
8148 KB |
Output is correct |
50 |
Correct |
8 ms |
8148 KB |
Output is correct |
51 |
Correct |
8 ms |
8164 KB |
Output is correct |
52 |
Correct |
6 ms |
8148 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
6 ms |
8196 KB |
Output is correct |
55 |
Correct |
6 ms |
8116 KB |
Output is correct |
56 |
Correct |
6 ms |
8148 KB |
Output is correct |
57 |
Correct |
6 ms |
8180 KB |
Output is correct |
58 |
Correct |
7 ms |
8148 KB |
Output is correct |
59 |
Correct |
6 ms |
8160 KB |
Output is correct |
60 |
Correct |
6 ms |
8116 KB |
Output is correct |
61 |
Correct |
8 ms |
8172 KB |
Output is correct |
62 |
Correct |
10 ms |
8152 KB |
Output is correct |
63 |
Correct |
7 ms |
8188 KB |
Output is correct |
64 |
Correct |
218 ms |
48448 KB |
Output is correct |
65 |
Correct |
227 ms |
47904 KB |
Output is correct |
66 |
Correct |
204 ms |
47928 KB |
Output is correct |
67 |
Correct |
196 ms |
47952 KB |
Output is correct |
68 |
Correct |
23 ms |
12584 KB |
Output is correct |
69 |
Correct |
28 ms |
12536 KB |
Output is correct |
70 |
Correct |
25 ms |
12540 KB |
Output is correct |
71 |
Correct |
35 ms |
12596 KB |
Output is correct |
72 |
Correct |
40 ms |
12524 KB |
Output is correct |
73 |
Correct |
28 ms |
12736 KB |
Output is correct |
74 |
Correct |
226 ms |
52160 KB |
Output is correct |
75 |
Correct |
227 ms |
52212 KB |
Output is correct |
76 |
Correct |
203 ms |
47928 KB |
Output is correct |
77 |
Correct |
229 ms |
47932 KB |
Output is correct |
78 |
Correct |
273 ms |
47844 KB |
Output is correct |
79 |
Correct |
254 ms |
48004 KB |
Output is correct |
80 |
Correct |
130 ms |
16560 KB |
Output is correct |
81 |
Correct |
210 ms |
47856 KB |
Output is correct |
82 |
Correct |
237 ms |
47848 KB |
Output is correct |
83 |
Correct |
205 ms |
47820 KB |
Output is correct |
84 |
Correct |
226 ms |
47816 KB |
Output is correct |
85 |
Correct |
245 ms |
47748 KB |
Output is correct |
86 |
Correct |
243 ms |
47816 KB |
Output is correct |
87 |
Correct |
242 ms |
47708 KB |
Output is correct |
88 |
Correct |
198 ms |
47704 KB |
Output is correct |
89 |
Correct |
267 ms |
47632 KB |
Output is correct |
90 |
Correct |
245 ms |
47640 KB |
Output is correct |
91 |
Correct |
265 ms |
47748 KB |
Output is correct |
92 |
Correct |
259 ms |
47688 KB |
Output is correct |