#include "books.h"
using namespace std;
typedef vector<int> vi;
const int N = 1000000;
const int INF = 0x3f3f3f3f;
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
char visited[N]; int xx[N], ll[N], rr[N], ll_[N], rr_[N], hh[N], m;
void sort(int *hh, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = ll_[hh[j]] - ll_[h];
if (c == 0)
j++;
else if (c < 0) {
tmp = hh[i], hh[i] = hh[j], hh[j] = tmp;
i++, j++;
} else {
k--;
tmp = hh[j], hh[j] = hh[k], hh[k] = tmp;
}
}
sort(hh, l, i);
l = k;
}
}
long long minimum_walk(vi pp, int s) {
int n = pp.size(), h, h_, h1, i, j, l, r, l_, r_, cnt;
long long ans;
ans = 0;
for (i = 0; i < n; i++) {
if (i < pp[i])
ans += pp[i] - i;
else if (i > pp[i])
ans += i - pp[i];
if (!visited[i]) {
l_ = -INF, r_ = INF, l = INF, r = -INF;
while (!visited[i]) {
l = min(l, i), r = max(r, i);
if (i <= s)
l_ = max(l_, i);
if (i >= s)
r_ = min(r_, i);
visited[i] = 1, i = pp[i];
}
if (l <= s && s <= r)
ll_[m] = l_, rr_[m] = r_, ll[m] = l, rr[m] = r, m++;
else
xx[l]++, xx[r]--;
}
}
for (i = 1; i < n - 1; i++)
xx[i] += xx[i - 1];
for (i = n - 1; i > 0; i--)
xx[i] = xx[i - 1] == 0;
for (i = 1; i < n; i++)
xx[i] += xx[i - 1];
for (h = 0; h < m; h++)
hh[h] = h;
sort(hh, 0, m);
cnt = 0;
for (h = 0; h < m; h++) {
h_ = hh[h];
while (cnt && rr_[hh[cnt - 1]] < rr_[h_]) {
h1 = hh[--cnt];
ll_[h_] = max(ll_[h_], ll_[h1]), rr_[h_] = min(rr_[h_], rr_[h1]);
ll[h_] = min(ll[h_], ll[h1]), rr[h_] = max(rr[h_], rr[h1]);
}
hh[cnt++] = h_;
}
for (h = cnt - 1, l_ = s, r_ = s; h >= 0; h--) {
h_ = hh[h];
if (l_ > ll_[h_] && r_ < rr_[h_])
ans += min(xx[l_] - xx[ll_[h_]], xx[rr_[h_]] - xx[r_]) * 2;
l_ = min(l_, ll[h_]), r_ = max(r_, rr[h_]);
}
i = 0;
while (i < l_ && pp[i] == i)
i++;
j = n - 1;
while (j > r_ && pp[j] == j)
j--;
ans += (xx[l_] - xx[i] + xx[j] - xx[r_]) * 2;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
284 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
284 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
284 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
208 ms |
19660 KB |
Output is correct |
31 |
Correct |
212 ms |
19796 KB |
Output is correct |
32 |
Correct |
147 ms |
19756 KB |
Output is correct |
33 |
Correct |
174 ms |
19668 KB |
Output is correct |
34 |
Correct |
156 ms |
19668 KB |
Output is correct |
35 |
Correct |
149 ms |
19804 KB |
Output is correct |
36 |
Correct |
147 ms |
19672 KB |
Output is correct |
37 |
Correct |
155 ms |
19644 KB |
Output is correct |
38 |
Correct |
148 ms |
19668 KB |
Output is correct |
39 |
Correct |
147 ms |
19680 KB |
Output is correct |
40 |
Correct |
189 ms |
19688 KB |
Output is correct |
41 |
Correct |
176 ms |
19672 KB |
Output is correct |
42 |
Correct |
176 ms |
19756 KB |
Output is correct |
43 |
Correct |
150 ms |
19664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
284 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
284 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
208 ms |
19660 KB |
Output is correct |
31 |
Correct |
212 ms |
19796 KB |
Output is correct |
32 |
Correct |
147 ms |
19756 KB |
Output is correct |
33 |
Correct |
174 ms |
19668 KB |
Output is correct |
34 |
Correct |
156 ms |
19668 KB |
Output is correct |
35 |
Correct |
149 ms |
19804 KB |
Output is correct |
36 |
Correct |
147 ms |
19672 KB |
Output is correct |
37 |
Correct |
155 ms |
19644 KB |
Output is correct |
38 |
Correct |
148 ms |
19668 KB |
Output is correct |
39 |
Correct |
147 ms |
19680 KB |
Output is correct |
40 |
Correct |
189 ms |
19688 KB |
Output is correct |
41 |
Correct |
176 ms |
19672 KB |
Output is correct |
42 |
Correct |
176 ms |
19756 KB |
Output is correct |
43 |
Correct |
150 ms |
19664 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
1 ms |
332 KB |
Output is correct |
52 |
Correct |
1 ms |
332 KB |
Output is correct |
53 |
Correct |
2 ms |
332 KB |
Output is correct |
54 |
Correct |
1 ms |
332 KB |
Output is correct |
55 |
Correct |
1 ms |
332 KB |
Output is correct |
56 |
Correct |
1 ms |
332 KB |
Output is correct |
57 |
Correct |
1 ms |
332 KB |
Output is correct |
58 |
Correct |
1 ms |
284 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
332 KB |
Output is correct |
61 |
Correct |
1 ms |
332 KB |
Output is correct |
62 |
Correct |
1 ms |
332 KB |
Output is correct |
63 |
Correct |
1 ms |
296 KB |
Output is correct |
64 |
Correct |
161 ms |
19664 KB |
Output is correct |
65 |
Correct |
147 ms |
19756 KB |
Output is correct |
66 |
Correct |
158 ms |
19676 KB |
Output is correct |
67 |
Correct |
162 ms |
19664 KB |
Output is correct |
68 |
Correct |
15 ms |
2124 KB |
Output is correct |
69 |
Correct |
15 ms |
2168 KB |
Output is correct |
70 |
Correct |
19 ms |
2100 KB |
Output is correct |
71 |
Correct |
16 ms |
2124 KB |
Output is correct |
72 |
Correct |
14 ms |
2204 KB |
Output is correct |
73 |
Correct |
15 ms |
2164 KB |
Output is correct |
74 |
Correct |
148 ms |
19680 KB |
Output is correct |
75 |
Correct |
152 ms |
19668 KB |
Output is correct |
76 |
Correct |
188 ms |
19792 KB |
Output is correct |
77 |
Correct |
197 ms |
19668 KB |
Output is correct |
78 |
Correct |
170 ms |
19668 KB |
Output is correct |
79 |
Correct |
152 ms |
19748 KB |
Output is correct |
80 |
Correct |
177 ms |
19668 KB |
Output is correct |
81 |
Correct |
181 ms |
26180 KB |
Output is correct |
82 |
Correct |
178 ms |
26192 KB |
Output is correct |
83 |
Correct |
194 ms |
20308 KB |
Output is correct |
84 |
Correct |
165 ms |
20176 KB |
Output is correct |
85 |
Correct |
149 ms |
19780 KB |
Output is correct |
86 |
Correct |
169 ms |
19796 KB |
Output is correct |
87 |
Correct |
161 ms |
22752 KB |
Output is correct |
88 |
Correct |
157 ms |
21472 KB |
Output is correct |
89 |
Correct |
158 ms |
20432 KB |
Output is correct |
90 |
Correct |
145 ms |
19652 KB |
Output is correct |
91 |
Correct |
145 ms |
19812 KB |
Output is correct |
92 |
Correct |
153 ms |
19664 KB |
Output is correct |