#include "books.h"
#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
#include "books.h"
const int oo = 1e9;
int n;
int D1[20][1 << 20];
int D2[20][1 << 20];
int Log[1 << 20];
int get1(int l,int r) {
int k = Log[r - l + 1];
return min(D1[k][l],D1[k][r - (1 << k) + 1]);
}
int get2(int l,int r) {
int k = Log[r - l + 1];
return max(D2[k][l],D2[k][r - (1 << k) + 1]);
}
long long minimum_walk(std::vector<int> p, int s) {
n = p.size();
int a = 0,b = n - 1;
while (a + 1 <= s && p[a] == a)
++a;
while (b - 1 >= s && p[b] == b)
--b;
int l = s,r = s;
ll ans = 0;
for (int i = 0;i < n;++i)
ans += abs(i - p[i]);
for (int i = 0;i < n;++i) {
D1[0][i] = D2[0][i] = p[i];
}
for (int t = 1;t < 20;++t)
for (int i = 0;i + (1 << t) <= n;++i) {
D1[t][i] = min(D1[t - 1][i],D1[t - 1][i + (1 << (t - 1))]);
D2[t][i] = max(D2[t - 1][i],D2[t - 1][i + (1 << (t - 1))]);
}
for (int i = 2;i <= n;++i)
Log[i] = Log[i / 2] + 1;
while (a < l || r < b) {
while (get1(l,r) < l || r < get2(l,r)) {
smin(l,get1(l,r));
smax(r,get2(l,r));
}
int cl = 0,cr = 0;
int l1 = l,r1 = l - 1;
int l2 = r + 1,r2 = r;
while (a < l1) {
--l1;
cl += 2;
while (l1 > get1(l1,r1))
smin(l1,get1(l1,r1));
if (get2(l1,r1) > r)
break;
}
while (r2 < b) {
++r2;
cr += 2;
while (r2 < get2(l2,r2))
smax(r2,get2(l2,r2));
if (get1(l2,r2) < l)
break;
}
if (get2(l1,r1) > r && get1(l2,r2) < l) {
ans += min(cl,cr);
l = l1;
r = r2;
} else {
ans += cl + cr;
break;
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
464 KB |
Output is correct |
3 |
Correct |
2 ms |
464 KB |
Output is correct |
4 |
Correct |
3 ms |
636 KB |
Output is correct |
5 |
Correct |
3 ms |
636 KB |
Output is correct |
6 |
Correct |
2 ms |
636 KB |
Output is correct |
7 |
Correct |
2 ms |
636 KB |
Output is correct |
8 |
Correct |
3 ms |
636 KB |
Output is correct |
9 |
Correct |
2 ms |
636 KB |
Output is correct |
10 |
Correct |
2 ms |
636 KB |
Output is correct |
11 |
Correct |
2 ms |
636 KB |
Output is correct |
12 |
Correct |
3 ms |
636 KB |
Output is correct |
13 |
Correct |
2 ms |
636 KB |
Output is correct |
14 |
Correct |
2 ms |
744 KB |
Output is correct |
15 |
Correct |
2 ms |
744 KB |
Output is correct |
16 |
Correct |
3 ms |
744 KB |
Output is correct |
17 |
Correct |
2 ms |
744 KB |
Output is correct |
18 |
Correct |
3 ms |
744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
464 KB |
Output is correct |
3 |
Correct |
2 ms |
464 KB |
Output is correct |
4 |
Correct |
3 ms |
636 KB |
Output is correct |
5 |
Correct |
3 ms |
636 KB |
Output is correct |
6 |
Correct |
2 ms |
636 KB |
Output is correct |
7 |
Correct |
2 ms |
636 KB |
Output is correct |
8 |
Correct |
3 ms |
636 KB |
Output is correct |
9 |
Correct |
2 ms |
636 KB |
Output is correct |
10 |
Correct |
2 ms |
636 KB |
Output is correct |
11 |
Correct |
2 ms |
636 KB |
Output is correct |
12 |
Correct |
3 ms |
636 KB |
Output is correct |
13 |
Correct |
2 ms |
636 KB |
Output is correct |
14 |
Correct |
2 ms |
744 KB |
Output is correct |
15 |
Correct |
2 ms |
744 KB |
Output is correct |
16 |
Correct |
3 ms |
744 KB |
Output is correct |
17 |
Correct |
2 ms |
744 KB |
Output is correct |
18 |
Correct |
3 ms |
744 KB |
Output is correct |
19 |
Correct |
2 ms |
760 KB |
Output is correct |
20 |
Correct |
2 ms |
764 KB |
Output is correct |
21 |
Correct |
2 ms |
764 KB |
Output is correct |
22 |
Correct |
3 ms |
764 KB |
Output is correct |
23 |
Correct |
3 ms |
764 KB |
Output is correct |
24 |
Correct |
3 ms |
764 KB |
Output is correct |
25 |
Correct |
3 ms |
764 KB |
Output is correct |
26 |
Correct |
3 ms |
764 KB |
Output is correct |
27 |
Correct |
3 ms |
764 KB |
Output is correct |
28 |
Correct |
3 ms |
764 KB |
Output is correct |
29 |
Correct |
3 ms |
764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
464 KB |
Output is correct |
3 |
Correct |
2 ms |
464 KB |
Output is correct |
4 |
Correct |
3 ms |
636 KB |
Output is correct |
5 |
Correct |
3 ms |
636 KB |
Output is correct |
6 |
Correct |
2 ms |
636 KB |
Output is correct |
7 |
Correct |
2 ms |
636 KB |
Output is correct |
8 |
Correct |
3 ms |
636 KB |
Output is correct |
9 |
Correct |
2 ms |
636 KB |
Output is correct |
10 |
Correct |
2 ms |
636 KB |
Output is correct |
11 |
Correct |
2 ms |
636 KB |
Output is correct |
12 |
Correct |
3 ms |
636 KB |
Output is correct |
13 |
Correct |
2 ms |
636 KB |
Output is correct |
14 |
Correct |
2 ms |
744 KB |
Output is correct |
15 |
Correct |
2 ms |
744 KB |
Output is correct |
16 |
Correct |
3 ms |
744 KB |
Output is correct |
17 |
Correct |
2 ms |
744 KB |
Output is correct |
18 |
Correct |
3 ms |
744 KB |
Output is correct |
19 |
Correct |
2 ms |
760 KB |
Output is correct |
20 |
Correct |
2 ms |
764 KB |
Output is correct |
21 |
Correct |
2 ms |
764 KB |
Output is correct |
22 |
Correct |
3 ms |
764 KB |
Output is correct |
23 |
Correct |
3 ms |
764 KB |
Output is correct |
24 |
Correct |
3 ms |
764 KB |
Output is correct |
25 |
Correct |
3 ms |
764 KB |
Output is correct |
26 |
Correct |
3 ms |
764 KB |
Output is correct |
27 |
Correct |
3 ms |
764 KB |
Output is correct |
28 |
Correct |
3 ms |
764 KB |
Output is correct |
29 |
Correct |
3 ms |
764 KB |
Output is correct |
30 |
Correct |
333 ms |
161020 KB |
Output is correct |
31 |
Correct |
453 ms |
161020 KB |
Output is correct |
32 |
Correct |
335 ms |
161020 KB |
Output is correct |
33 |
Correct |
313 ms |
161020 KB |
Output is correct |
34 |
Correct |
305 ms |
161068 KB |
Output is correct |
35 |
Correct |
305 ms |
161068 KB |
Output is correct |
36 |
Correct |
326 ms |
161068 KB |
Output is correct |
37 |
Correct |
330 ms |
161068 KB |
Output is correct |
38 |
Correct |
335 ms |
161068 KB |
Output is correct |
39 |
Correct |
288 ms |
161068 KB |
Output is correct |
40 |
Correct |
394 ms |
161068 KB |
Output is correct |
41 |
Correct |
322 ms |
161068 KB |
Output is correct |
42 |
Correct |
303 ms |
161068 KB |
Output is correct |
43 |
Correct |
340 ms |
161172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
161172 KB |
Output is correct |
2 |
Correct |
4 ms |
161172 KB |
Output is correct |
3 |
Correct |
4 ms |
161172 KB |
Output is correct |
4 |
Correct |
3 ms |
161172 KB |
Output is correct |
5 |
Correct |
3 ms |
161172 KB |
Output is correct |
6 |
Correct |
3 ms |
161172 KB |
Output is correct |
7 |
Correct |
3 ms |
161172 KB |
Output is correct |
8 |
Correct |
3 ms |
161172 KB |
Output is correct |
9 |
Correct |
3 ms |
161172 KB |
Output is correct |
10 |
Correct |
4 ms |
161172 KB |
Output is correct |
11 |
Correct |
3 ms |
161172 KB |
Output is correct |
12 |
Correct |
2 ms |
161172 KB |
Output is correct |
13 |
Correct |
3 ms |
161172 KB |
Output is correct |
14 |
Correct |
4 ms |
161172 KB |
Output is correct |
15 |
Correct |
3 ms |
161172 KB |
Output is correct |
16 |
Correct |
3 ms |
161172 KB |
Output is correct |
17 |
Correct |
3 ms |
161172 KB |
Output is correct |
18 |
Correct |
2 ms |
161172 KB |
Output is correct |
19 |
Correct |
2 ms |
161172 KB |
Output is correct |
20 |
Correct |
2 ms |
161172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
464 KB |
Output is correct |
3 |
Correct |
2 ms |
464 KB |
Output is correct |
4 |
Correct |
3 ms |
636 KB |
Output is correct |
5 |
Correct |
3 ms |
636 KB |
Output is correct |
6 |
Correct |
2 ms |
636 KB |
Output is correct |
7 |
Correct |
2 ms |
636 KB |
Output is correct |
8 |
Correct |
3 ms |
636 KB |
Output is correct |
9 |
Correct |
2 ms |
636 KB |
Output is correct |
10 |
Correct |
2 ms |
636 KB |
Output is correct |
11 |
Correct |
2 ms |
636 KB |
Output is correct |
12 |
Correct |
3 ms |
636 KB |
Output is correct |
13 |
Correct |
2 ms |
636 KB |
Output is correct |
14 |
Correct |
2 ms |
744 KB |
Output is correct |
15 |
Correct |
2 ms |
744 KB |
Output is correct |
16 |
Correct |
3 ms |
744 KB |
Output is correct |
17 |
Correct |
2 ms |
744 KB |
Output is correct |
18 |
Correct |
3 ms |
744 KB |
Output is correct |
19 |
Correct |
2 ms |
760 KB |
Output is correct |
20 |
Correct |
2 ms |
764 KB |
Output is correct |
21 |
Correct |
2 ms |
764 KB |
Output is correct |
22 |
Correct |
3 ms |
764 KB |
Output is correct |
23 |
Correct |
3 ms |
764 KB |
Output is correct |
24 |
Correct |
3 ms |
764 KB |
Output is correct |
25 |
Correct |
3 ms |
764 KB |
Output is correct |
26 |
Correct |
3 ms |
764 KB |
Output is correct |
27 |
Correct |
3 ms |
764 KB |
Output is correct |
28 |
Correct |
3 ms |
764 KB |
Output is correct |
29 |
Correct |
3 ms |
764 KB |
Output is correct |
30 |
Correct |
333 ms |
161020 KB |
Output is correct |
31 |
Correct |
453 ms |
161020 KB |
Output is correct |
32 |
Correct |
335 ms |
161020 KB |
Output is correct |
33 |
Correct |
313 ms |
161020 KB |
Output is correct |
34 |
Correct |
305 ms |
161068 KB |
Output is correct |
35 |
Correct |
305 ms |
161068 KB |
Output is correct |
36 |
Correct |
326 ms |
161068 KB |
Output is correct |
37 |
Correct |
330 ms |
161068 KB |
Output is correct |
38 |
Correct |
335 ms |
161068 KB |
Output is correct |
39 |
Correct |
288 ms |
161068 KB |
Output is correct |
40 |
Correct |
394 ms |
161068 KB |
Output is correct |
41 |
Correct |
322 ms |
161068 KB |
Output is correct |
42 |
Correct |
303 ms |
161068 KB |
Output is correct |
43 |
Correct |
340 ms |
161172 KB |
Output is correct |
44 |
Correct |
3 ms |
161172 KB |
Output is correct |
45 |
Correct |
4 ms |
161172 KB |
Output is correct |
46 |
Correct |
4 ms |
161172 KB |
Output is correct |
47 |
Correct |
3 ms |
161172 KB |
Output is correct |
48 |
Correct |
3 ms |
161172 KB |
Output is correct |
49 |
Correct |
3 ms |
161172 KB |
Output is correct |
50 |
Correct |
3 ms |
161172 KB |
Output is correct |
51 |
Correct |
3 ms |
161172 KB |
Output is correct |
52 |
Correct |
3 ms |
161172 KB |
Output is correct |
53 |
Correct |
4 ms |
161172 KB |
Output is correct |
54 |
Correct |
3 ms |
161172 KB |
Output is correct |
55 |
Correct |
2 ms |
161172 KB |
Output is correct |
56 |
Correct |
3 ms |
161172 KB |
Output is correct |
57 |
Correct |
4 ms |
161172 KB |
Output is correct |
58 |
Correct |
3 ms |
161172 KB |
Output is correct |
59 |
Correct |
3 ms |
161172 KB |
Output is correct |
60 |
Correct |
3 ms |
161172 KB |
Output is correct |
61 |
Correct |
2 ms |
161172 KB |
Output is correct |
62 |
Correct |
2 ms |
161172 KB |
Output is correct |
63 |
Correct |
2 ms |
161172 KB |
Output is correct |
64 |
Correct |
287 ms |
167728 KB |
Output is correct |
65 |
Correct |
311 ms |
174432 KB |
Output is correct |
66 |
Correct |
436 ms |
181348 KB |
Output is correct |
67 |
Correct |
430 ms |
188096 KB |
Output is correct |
68 |
Correct |
36 ms |
188096 KB |
Output is correct |
69 |
Correct |
31 ms |
188096 KB |
Output is correct |
70 |
Correct |
29 ms |
188096 KB |
Output is correct |
71 |
Correct |
32 ms |
188096 KB |
Output is correct |
72 |
Correct |
36 ms |
188096 KB |
Output is correct |
73 |
Correct |
33 ms |
188096 KB |
Output is correct |
74 |
Correct |
361 ms |
198332 KB |
Output is correct |
75 |
Correct |
346 ms |
205016 KB |
Output is correct |
76 |
Correct |
367 ms |
211700 KB |
Output is correct |
77 |
Correct |
319 ms |
218508 KB |
Output is correct |
78 |
Correct |
333 ms |
225188 KB |
Output is correct |
79 |
Correct |
413 ms |
232000 KB |
Output is correct |
80 |
Correct |
351 ms |
238676 KB |
Output is correct |
81 |
Correct |
315 ms |
245424 KB |
Output is correct |
82 |
Correct |
350 ms |
252160 KB |
Output is correct |
83 |
Correct |
348 ms |
259060 KB |
Output is correct |
84 |
Correct |
291 ms |
265772 KB |
Output is correct |
85 |
Correct |
348 ms |
272416 KB |
Output is correct |
86 |
Correct |
337 ms |
279320 KB |
Output is correct |
87 |
Correct |
300 ms |
285948 KB |
Output is correct |
88 |
Correct |
378 ms |
292680 KB |
Output is correct |
89 |
Correct |
373 ms |
299432 KB |
Output is correct |
90 |
Correct |
365 ms |
306228 KB |
Output is correct |
91 |
Correct |
349 ms |
312960 KB |
Output is correct |
92 |
Correct |
343 ms |
319672 KB |
Output is correct |