#include <bits/stdc++.h>
#pragma optimization("unroll-loops")
#pragma optimization("Ofast")
#pragma target("avx2")
using namespace std;
long long res;
void push(int& l, int& r, int& mn, int& mx, vector<int>& p){
while(l != mn || r != mx){
if(l != mn){
l--;
mn = min(mn, p[l]);
mx = max(mx, p[l]);
} else {
r++;
mn = min(mn, p[r]);
mx = max(mx, p[r]);
}
}
}
long long minimum_walk(vector<int> p, int s){
int L = 0, R = p.size() - 1;
while(R >= 0 && p[R] == R) R--;
if(R == -1) return 0;
while(p[L] == L) L++;
if(s > R){
res += (s - R) << 1;
s = R;
}
if(s < L){
res += (L - s) << 1;
s = L;
}
int tmn = s, tmx = s;
for(int i = L; i <= R; i++){
res += abs(p[i] - i);
if(1LL * (tmx - p[i]) * (tmx - i) <= 0 || 1LL * (tmn - p[i]) * (tmn - i) <= 0){
tmx = max(tmx, max(i, p[i]));
tmn = min(tmn, min(i, p[i]));
}
}
int l, r, mn = min(s, p[s]), mx = max(s, p[s]);
l = r = s;
push(l, r, tmn, tmx, p);
l = r = s;
push(l, r, mn, mx, p);
while(mn != tmn || mx != tmx){
res += 2;
if(mn != tmn)mn--;
if(mx != tmx)mx++;
push(l, r, mn, mx, p);
}
for(; l >= L; l--){
if(mn > l) res += 2;
mn = min(mn, p[l]);
}
for(; r <= R; r++){
if(mx < r) res += 2;
mx = max(mx, p[r]);
}
return res;
}
Compilation message
books.cpp:2:0: warning: ignoring #pragma optimization [-Wunknown-pragmas]
#pragma optimization("unroll-loops")
books.cpp:3:0: warning: ignoring #pragma optimization [-Wunknown-pragmas]
#pragma optimization("Ofast")
books.cpp:4:0: warning: ignoring #pragma target [-Wunknown-pragmas]
#pragma target("avx2")
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
256 KB |
Output is correct |
21 |
Correct |
2 ms |
256 KB |
Output is correct |
22 |
Correct |
2 ms |
256 KB |
Output is correct |
23 |
Correct |
2 ms |
256 KB |
Output is correct |
24 |
Correct |
2 ms |
256 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
256 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
256 KB |
Output is correct |
29 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
256 KB |
Output is correct |
21 |
Correct |
2 ms |
256 KB |
Output is correct |
22 |
Correct |
2 ms |
256 KB |
Output is correct |
23 |
Correct |
2 ms |
256 KB |
Output is correct |
24 |
Correct |
2 ms |
256 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
256 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
256 KB |
Output is correct |
29 |
Correct |
2 ms |
256 KB |
Output is correct |
30 |
Correct |
140 ms |
8248 KB |
Output is correct |
31 |
Correct |
145 ms |
8192 KB |
Output is correct |
32 |
Correct |
121 ms |
8184 KB |
Output is correct |
33 |
Correct |
116 ms |
8184 KB |
Output is correct |
34 |
Correct |
118 ms |
8184 KB |
Output is correct |
35 |
Correct |
117 ms |
8120 KB |
Output is correct |
36 |
Correct |
121 ms |
8184 KB |
Output is correct |
37 |
Correct |
122 ms |
8184 KB |
Output is correct |
38 |
Correct |
125 ms |
8184 KB |
Output is correct |
39 |
Correct |
122 ms |
8184 KB |
Output is correct |
40 |
Correct |
126 ms |
8184 KB |
Output is correct |
41 |
Correct |
132 ms |
8184 KB |
Output is correct |
42 |
Correct |
130 ms |
8184 KB |
Output is correct |
43 |
Correct |
123 ms |
8184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
256 KB |
Output is correct |
21 |
Correct |
2 ms |
256 KB |
Output is correct |
22 |
Correct |
2 ms |
256 KB |
Output is correct |
23 |
Correct |
2 ms |
256 KB |
Output is correct |
24 |
Correct |
2 ms |
256 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
256 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
256 KB |
Output is correct |
29 |
Correct |
2 ms |
256 KB |
Output is correct |
30 |
Correct |
140 ms |
8248 KB |
Output is correct |
31 |
Correct |
145 ms |
8192 KB |
Output is correct |
32 |
Correct |
121 ms |
8184 KB |
Output is correct |
33 |
Correct |
116 ms |
8184 KB |
Output is correct |
34 |
Correct |
118 ms |
8184 KB |
Output is correct |
35 |
Correct |
117 ms |
8120 KB |
Output is correct |
36 |
Correct |
121 ms |
8184 KB |
Output is correct |
37 |
Correct |
122 ms |
8184 KB |
Output is correct |
38 |
Correct |
125 ms |
8184 KB |
Output is correct |
39 |
Correct |
122 ms |
8184 KB |
Output is correct |
40 |
Correct |
126 ms |
8184 KB |
Output is correct |
41 |
Correct |
132 ms |
8184 KB |
Output is correct |
42 |
Correct |
130 ms |
8184 KB |
Output is correct |
43 |
Correct |
123 ms |
8184 KB |
Output is correct |
44 |
Correct |
2 ms |
384 KB |
Output is correct |
45 |
Correct |
2 ms |
256 KB |
Output is correct |
46 |
Correct |
2 ms |
384 KB |
Output is correct |
47 |
Correct |
2 ms |
384 KB |
Output is correct |
48 |
Correct |
2 ms |
256 KB |
Output is correct |
49 |
Correct |
2 ms |
384 KB |
Output is correct |
50 |
Correct |
2 ms |
256 KB |
Output is correct |
51 |
Correct |
2 ms |
256 KB |
Output is correct |
52 |
Correct |
2 ms |
384 KB |
Output is correct |
53 |
Correct |
2 ms |
256 KB |
Output is correct |
54 |
Correct |
2 ms |
384 KB |
Output is correct |
55 |
Correct |
2 ms |
256 KB |
Output is correct |
56 |
Correct |
2 ms |
256 KB |
Output is correct |
57 |
Correct |
2 ms |
256 KB |
Output is correct |
58 |
Correct |
2 ms |
384 KB |
Output is correct |
59 |
Correct |
2 ms |
256 KB |
Output is correct |
60 |
Correct |
2 ms |
256 KB |
Output is correct |
61 |
Correct |
2 ms |
256 KB |
Output is correct |
62 |
Correct |
2 ms |
256 KB |
Output is correct |
63 |
Correct |
2 ms |
256 KB |
Output is correct |
64 |
Correct |
123 ms |
8120 KB |
Output is correct |
65 |
Correct |
117 ms |
8120 KB |
Output is correct |
66 |
Correct |
125 ms |
8248 KB |
Output is correct |
67 |
Correct |
127 ms |
8120 KB |
Output is correct |
68 |
Correct |
14 ms |
1152 KB |
Output is correct |
69 |
Correct |
14 ms |
1152 KB |
Output is correct |
70 |
Correct |
13 ms |
1152 KB |
Output is correct |
71 |
Correct |
13 ms |
1152 KB |
Output is correct |
72 |
Correct |
14 ms |
1152 KB |
Output is correct |
73 |
Correct |
15 ms |
1152 KB |
Output is correct |
74 |
Correct |
116 ms |
8124 KB |
Output is correct |
75 |
Correct |
117 ms |
8264 KB |
Output is correct |
76 |
Correct |
121 ms |
8128 KB |
Output is correct |
77 |
Correct |
124 ms |
8136 KB |
Output is correct |
78 |
Correct |
123 ms |
8120 KB |
Output is correct |
79 |
Correct |
119 ms |
8184 KB |
Output is correct |
80 |
Correct |
110 ms |
8124 KB |
Output is correct |
81 |
Correct |
119 ms |
8124 KB |
Output is correct |
82 |
Correct |
120 ms |
8184 KB |
Output is correct |
83 |
Correct |
124 ms |
8156 KB |
Output is correct |
84 |
Correct |
122 ms |
8124 KB |
Output is correct |
85 |
Correct |
122 ms |
8184 KB |
Output is correct |
86 |
Correct |
125 ms |
8124 KB |
Output is correct |
87 |
Correct |
134 ms |
8184 KB |
Output is correct |
88 |
Correct |
128 ms |
8184 KB |
Output is correct |
89 |
Correct |
127 ms |
8128 KB |
Output is correct |
90 |
Correct |
127 ms |
8136 KB |
Output is correct |
91 |
Correct |
128 ms |
8124 KB |
Output is correct |
92 |
Correct |
137 ms |
8120 KB |
Output is correct |