#include <bits/stdc++.h>
#include "books.h"
using namespace std;
using ll = long long;
long long minimum_walk(std::vector<int> p, int s) {
if (is_sorted(p.begin(), p.end())) {
return 0;
}
int n = p.size();
ll ans = 0;
vector<int> inv(n);
for (int i = 0; i < n; ++i) {
inv[p[i]] = i;
ans += abs(p[i] - i);
}
vector<int> lx(n, -1), rx(n, -1);
for (int i = 0; i < n; ++i) {
if (lx[i] == -1) {
int v = i, mn = n, mx = 0;
vector<int> cycle;
do {
mn = min(mn, v);
mx = max(mx, v);
lx[v] = 228;
cycle.push_back(v);
v = p[v];
} while (lx[v] == -1);
for (int x: cycle) {
lx[x] = mn, rx[x] = mx;
}
}
}
auto extend = [&](int &l, int &r, int limL, int limR) {
int mn = l, mx = r;
for (int i = l; i <= limL; ++i) {
mn = min(mn, lx[i]);
mx = max(mx, rx[i]);
}
for (int i = limR; i <= r; ++i) {
mn = min(mn, lx[i]);
mx = max(mx, rx[i]);
}
l = mn, r = mx;
};
int trivL = 0, trivR = n - 1;
while (p[trivL] == trivL) {
trivL += 1;
}
while (p[trivR] == trivR) {
trivR -= 1;
}
int l = lx[s], r = rx[s];
extend(l, r, s, s);
while (l >= 0 && r < n) {
int cl = l, cr = r;
int ansL = 0, ansR = 0;
int mn = n;
bool found = false;
while (cl > 0) {
cl -= 1;
if (mn > cl) {
ansL += 1;
}
mn = min(mn, p[cl]);
if (rx[cl] > r) {
found = true;
break;
}
}
int mx = 0;
while (cr < n - 1) {
cr += 1;
if (mx < cr) {
ansR += 1;
}
mx = max(mx, p[cr]);
if (lx[cr] < l) {
found = true;
break;
}
}
if (!found) {
if (trivL < l) {
ans += (ansL - trivL) * 2;
}
if (trivR > r) {
ans += (ansR - (n - trivR - 1)) * 2;
}
break;
}
ans += min(ansL, ansR) * 2;
int pl = l, pr = r;
l = min(lx[cl], lx[cr]);
r = max(rx[cr], rx[cr]);
extend(l, r, pl, pr);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
181 ms |
24036 KB |
Output is correct |
31 |
Correct |
188 ms |
21984 KB |
Output is correct |
32 |
Correct |
84 ms |
8048 KB |
Output is correct |
33 |
Correct |
117 ms |
19868 KB |
Output is correct |
34 |
Correct |
120 ms |
19864 KB |
Output is correct |
35 |
Correct |
120 ms |
19944 KB |
Output is correct |
36 |
Correct |
116 ms |
19820 KB |
Output is correct |
37 |
Correct |
101 ms |
19820 KB |
Output is correct |
38 |
Correct |
115 ms |
19832 KB |
Output is correct |
39 |
Correct |
102 ms |
19816 KB |
Output is correct |
40 |
Correct |
107 ms |
20144 KB |
Output is correct |
41 |
Correct |
129 ms |
22944 KB |
Output is correct |
42 |
Correct |
126 ms |
21452 KB |
Output is correct |
43 |
Correct |
124 ms |
19788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
181 ms |
24036 KB |
Output is correct |
31 |
Correct |
188 ms |
21984 KB |
Output is correct |
32 |
Correct |
84 ms |
8048 KB |
Output is correct |
33 |
Correct |
117 ms |
19868 KB |
Output is correct |
34 |
Correct |
120 ms |
19864 KB |
Output is correct |
35 |
Correct |
120 ms |
19944 KB |
Output is correct |
36 |
Correct |
116 ms |
19820 KB |
Output is correct |
37 |
Correct |
101 ms |
19820 KB |
Output is correct |
38 |
Correct |
115 ms |
19832 KB |
Output is correct |
39 |
Correct |
102 ms |
19816 KB |
Output is correct |
40 |
Correct |
107 ms |
20144 KB |
Output is correct |
41 |
Correct |
129 ms |
22944 KB |
Output is correct |
42 |
Correct |
126 ms |
21452 KB |
Output is correct |
43 |
Correct |
124 ms |
19788 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
212 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
1 ms |
212 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
1 ms |
212 KB |
Output is correct |
64 |
Correct |
121 ms |
19836 KB |
Output is correct |
65 |
Correct |
119 ms |
19816 KB |
Output is correct |
66 |
Correct |
105 ms |
19820 KB |
Output is correct |
67 |
Correct |
105 ms |
19904 KB |
Output is correct |
68 |
Correct |
13 ms |
2232 KB |
Output is correct |
69 |
Correct |
11 ms |
2260 KB |
Output is correct |
70 |
Correct |
11 ms |
2260 KB |
Output is correct |
71 |
Correct |
12 ms |
2260 KB |
Output is correct |
72 |
Correct |
12 ms |
2260 KB |
Output is correct |
73 |
Correct |
14 ms |
2268 KB |
Output is correct |
74 |
Correct |
158 ms |
19836 KB |
Output is correct |
75 |
Correct |
124 ms |
19828 KB |
Output is correct |
76 |
Correct |
130 ms |
19948 KB |
Output is correct |
77 |
Correct |
121 ms |
19832 KB |
Output is correct |
78 |
Correct |
144 ms |
19788 KB |
Output is correct |
79 |
Correct |
123 ms |
19820 KB |
Output is correct |
80 |
Correct |
83 ms |
8048 KB |
Output is correct |
81 |
Correct |
116 ms |
19872 KB |
Output is correct |
82 |
Correct |
136 ms |
26592 KB |
Output is correct |
83 |
Correct |
125 ms |
26728 KB |
Output is correct |
84 |
Correct |
130 ms |
26608 KB |
Output is correct |
85 |
Correct |
141 ms |
26708 KB |
Output is correct |
86 |
Correct |
109 ms |
26604 KB |
Output is correct |
87 |
Correct |
125 ms |
26604 KB |
Output is correct |
88 |
Correct |
151 ms |
26604 KB |
Output is correct |
89 |
Correct |
127 ms |
26596 KB |
Output is correct |
90 |
Correct |
105 ms |
26596 KB |
Output is correct |
91 |
Correct |
116 ms |
26592 KB |
Output is correct |
92 |
Correct |
123 ms |
26724 KB |
Output is correct |