#include "books.h"
#include <iostream>
#define ll long long
using namespace std;
bool ok = 0;
vector <ll> p;
ll n, l, r, x, y, qx, qy, ql, qr, a, b, f = 0, mn = 0, mx = 0;
void branch() {
while (true) {
for (int i=x; i>=qx; --i) {
qx = min(qx, p[i]);
qy = max(qy, p[i]);
}
for (int i=y; i<=qy; ++i) {
qx = min(qx, p[i]);
qy = max(qy, p[i]);
}
if (x == qx && y == qy) break;
x = qx, y = qy;
}
}
long long minimum_walk(std::vector<int> A, int s) {
for (auto u : A) p.push_back(u);
n = p.size();
for (int i=0; i<n; ++i) {
f += abs(i-p[i]);
}
l = n, r = -1;
for (int i=0; i<n; ++i) {
if (p[i] != i) {
l = i;
break;
}
}
for (int i=n-1; i>=0; --i) {
if (p[i] != i) {
r = i;
break;
}
}
if (l == n) return 0;
x = y = qx = qy = s;
branch();
while (true) {
ok = a = b = 0;
mn = 1e18, mx = -1e18;
for (int i=x-1; i>=l; --i) {
qx = i;
mn = min(mn, (ll)p[i]);
if (p[i] > y) {
a += 2;
ok = 1;
break;
}
if (mn == i) a += 2;
}
for (int i=y+1; i<=r; ++i) {
qy = i;
mx = max(mx, (ll)p[i]);
if (p[i] < x) {
b += 2;
break;
}
if (mx == i) b += 2;
}
if (!ok) {
f += a + b;
break;
}
else {
f += min(a, b);
branch();
}
}
return f;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:47:10: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
47 | ok = a = b = 0;
| ~~^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
72 ms |
24508 KB |
Output is correct |
31 |
Correct |
61 ms |
23744 KB |
Output is correct |
32 |
Correct |
60 ms |
23848 KB |
Output is correct |
33 |
Correct |
61 ms |
24256 KB |
Output is correct |
34 |
Correct |
60 ms |
23388 KB |
Output is correct |
35 |
Correct |
66 ms |
23996 KB |
Output is correct |
36 |
Correct |
61 ms |
25276 KB |
Output is correct |
37 |
Correct |
78 ms |
25160 KB |
Output is correct |
38 |
Correct |
64 ms |
24252 KB |
Output is correct |
39 |
Correct |
60 ms |
24764 KB |
Output is correct |
40 |
Correct |
58 ms |
25280 KB |
Output is correct |
41 |
Correct |
59 ms |
24012 KB |
Output is correct |
42 |
Correct |
60 ms |
23740 KB |
Output is correct |
43 |
Correct |
59 ms |
24256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
444 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
72 ms |
24508 KB |
Output is correct |
31 |
Correct |
61 ms |
23744 KB |
Output is correct |
32 |
Correct |
60 ms |
23848 KB |
Output is correct |
33 |
Correct |
61 ms |
24256 KB |
Output is correct |
34 |
Correct |
60 ms |
23388 KB |
Output is correct |
35 |
Correct |
66 ms |
23996 KB |
Output is correct |
36 |
Correct |
61 ms |
25276 KB |
Output is correct |
37 |
Correct |
78 ms |
25160 KB |
Output is correct |
38 |
Correct |
64 ms |
24252 KB |
Output is correct |
39 |
Correct |
60 ms |
24764 KB |
Output is correct |
40 |
Correct |
58 ms |
25280 KB |
Output is correct |
41 |
Correct |
59 ms |
24012 KB |
Output is correct |
42 |
Correct |
60 ms |
23740 KB |
Output is correct |
43 |
Correct |
59 ms |
24256 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
604 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
444 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
63 ms |
24268 KB |
Output is correct |
65 |
Correct |
64 ms |
24768 KB |
Output is correct |
66 |
Correct |
66 ms |
24764 KB |
Output is correct |
67 |
Correct |
61 ms |
23380 KB |
Output is correct |
68 |
Correct |
7 ms |
2768 KB |
Output is correct |
69 |
Correct |
7 ms |
2856 KB |
Output is correct |
70 |
Correct |
6 ms |
2852 KB |
Output is correct |
71 |
Correct |
6 ms |
2772 KB |
Output is correct |
72 |
Correct |
7 ms |
2772 KB |
Output is correct |
73 |
Correct |
6 ms |
2772 KB |
Output is correct |
74 |
Correct |
61 ms |
23776 KB |
Output is correct |
75 |
Correct |
88 ms |
23484 KB |
Output is correct |
76 |
Correct |
57 ms |
23768 KB |
Output is correct |
77 |
Correct |
69 ms |
23488 KB |
Output is correct |
78 |
Correct |
63 ms |
24660 KB |
Output is correct |
79 |
Correct |
65 ms |
23772 KB |
Output is correct |
80 |
Correct |
59 ms |
23908 KB |
Output is correct |
81 |
Correct |
89 ms |
23900 KB |
Output is correct |
82 |
Correct |
68 ms |
24764 KB |
Output is correct |
83 |
Correct |
59 ms |
23996 KB |
Output is correct |
84 |
Correct |
59 ms |
24164 KB |
Output is correct |
85 |
Correct |
68 ms |
24796 KB |
Output is correct |
86 |
Correct |
61 ms |
23996 KB |
Output is correct |
87 |
Correct |
64 ms |
24508 KB |
Output is correct |
88 |
Correct |
62 ms |
23656 KB |
Output is correct |
89 |
Correct |
88 ms |
25184 KB |
Output is correct |
90 |
Correct |
66 ms |
24916 KB |
Output is correct |
91 |
Correct |
67 ms |
25056 KB |
Output is correct |
92 |
Correct |
64 ms |
24768 KB |
Output is correct |