#include "books.h"
#include <string.h>
using namespace std;
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; }
typedef vector<int> vi;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
0 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 |
184 ms |
13028 KB |
Output is correct |
31 |
Correct |
183 ms |
12912 KB |
Output is correct |
32 |
Correct |
139 ms |
12996 KB |
Output is correct |
33 |
Correct |
145 ms |
13032 KB |
Output is correct |
34 |
Correct |
151 ms |
13024 KB |
Output is correct |
35 |
Correct |
149 ms |
12996 KB |
Output is correct |
36 |
Correct |
145 ms |
12916 KB |
Output is correct |
37 |
Correct |
147 ms |
12908 KB |
Output is correct |
38 |
Correct |
148 ms |
12996 KB |
Output is correct |
39 |
Correct |
150 ms |
13032 KB |
Output is correct |
40 |
Correct |
147 ms |
12908 KB |
Output is correct |
41 |
Correct |
160 ms |
13124 KB |
Output is correct |
42 |
Correct |
159 ms |
13024 KB |
Output is correct |
43 |
Correct |
153 ms |
13124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
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 |
1 ms |
280 KB |
Output is correct |
15 |
Correct |
1 ms |
332 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 |
288 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
0 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 |
184 ms |
13028 KB |
Output is correct |
31 |
Correct |
183 ms |
12912 KB |
Output is correct |
32 |
Correct |
139 ms |
12996 KB |
Output is correct |
33 |
Correct |
145 ms |
13032 KB |
Output is correct |
34 |
Correct |
151 ms |
13024 KB |
Output is correct |
35 |
Correct |
149 ms |
12996 KB |
Output is correct |
36 |
Correct |
145 ms |
12916 KB |
Output is correct |
37 |
Correct |
147 ms |
12908 KB |
Output is correct |
38 |
Correct |
148 ms |
12996 KB |
Output is correct |
39 |
Correct |
150 ms |
13032 KB |
Output is correct |
40 |
Correct |
147 ms |
12908 KB |
Output is correct |
41 |
Correct |
160 ms |
13124 KB |
Output is correct |
42 |
Correct |
159 ms |
13024 KB |
Output is correct |
43 |
Correct |
153 ms |
13124 KB |
Output is correct |
44 |
Correct |
0 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 |
1 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 |
280 KB |
Output is correct |
58 |
Correct |
1 ms |
332 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 |
288 KB |
Output is correct |
63 |
Correct |
1 ms |
332 KB |
Output is correct |
64 |
Correct |
146 ms |
19680 KB |
Output is correct |
65 |
Correct |
152 ms |
19676 KB |
Output is correct |
66 |
Correct |
150 ms |
19672 KB |
Output is correct |
67 |
Correct |
147 ms |
19760 KB |
Output is correct |
68 |
Correct |
16 ms |
2124 KB |
Output is correct |
69 |
Correct |
14 ms |
2084 KB |
Output is correct |
70 |
Correct |
14 ms |
2072 KB |
Output is correct |
71 |
Correct |
15 ms |
2124 KB |
Output is correct |
72 |
Correct |
14 ms |
2088 KB |
Output is correct |
73 |
Correct |
15 ms |
2088 KB |
Output is correct |
74 |
Correct |
149 ms |
19788 KB |
Output is correct |
75 |
Correct |
146 ms |
19652 KB |
Output is correct |
76 |
Correct |
153 ms |
19672 KB |
Output is correct |
77 |
Correct |
150 ms |
19652 KB |
Output is correct |
78 |
Correct |
148 ms |
19680 KB |
Output is correct |
79 |
Correct |
146 ms |
19652 KB |
Output is correct |
80 |
Correct |
144 ms |
19680 KB |
Output is correct |
81 |
Correct |
177 ms |
26308 KB |
Output is correct |
82 |
Correct |
195 ms |
26360 KB |
Output is correct |
83 |
Correct |
151 ms |
20328 KB |
Output is correct |
84 |
Correct |
161 ms |
20112 KB |
Output is correct |
85 |
Correct |
156 ms |
19908 KB |
Output is correct |
86 |
Correct |
169 ms |
19796 KB |
Output is correct |
87 |
Correct |
166 ms |
22848 KB |
Output is correct |
88 |
Correct |
179 ms |
21588 KB |
Output is correct |
89 |
Correct |
153 ms |
20548 KB |
Output is correct |
90 |
Correct |
146 ms |
19804 KB |
Output is correct |
91 |
Correct |
151 ms |
19688 KB |
Output is correct |
92 |
Correct |
151 ms |
19804 KB |
Output is correct |