#include "books.h"
#include <queue>
#include <algorithm>
#include <vector>
#define lli long long
#define ad push_back
#define fr first
#define sc second
#define MP make_pair
using namespace std;
const int N = 1e6 + 30;
const int mod = 1e9;
int i1 = 1, ii, n;
int col[N], a[N], cl[N];
int mn[N], mx[N], m[N], x[N], d[N];
lli ans;
int dfs(int v)
{
col[v] = i1;
cl[v] = ii;
mn[ii] = min(mn[ii], v), m[i1] = min(m[i1], v);
mx[ii] = max(mx[ii], v), x[i1] = max(x[i1], v);
if (!col[a[v]]) return max(v, dfs(a[v]));
return v;
}
long long minimum_walk(vector<int> p, int s)
{
n = p.size();
for (int i = 0; i < n; i++) a[i] = p[i], ans += abs(a[i] - i), mn[i] = m[i] = n, d[i] = mod;
int nx = -1;
for (int i = 0; i < n; i++)
{
if (a[i] == i) continue;
if (!col[i])
{
if (nx >= 0 && i > nx) ans += 2 * (i - nx), i1++;
ii++;
nx = max(nx, dfs(i));
}
}
int dz = -1, aj = -1;
for (int i = s; i < n; i++)
{
if (col[i])
{
aj = i;
break;
}
}
for (int i = s; i >= 0; i--)
{
if (col[i])
{
dz = i;
break;
}
}
if (dz == -1 && aj == -1) return ans;
else if (dz == -1) ans += 2 * abs(aj - s);
else if (aj == -1) ans += 2 * abs(s - dz);
else if (col[aj] == col[dz])
{
priority_queue < pair<pair<int, int>, pair<int, int> > > q;
q.push(MP(MP(s - aj, cl[aj]), MP(s, s)));
q.push(MP(MP(dz - s, cl[dz]), MP(s, s)));
while (!q.empty())
{
int d = q.top().fr.fr, v = q.top().fr.sc, l = q.top().sc.fr, r = q.top().sc.sc;
q.pop();
if (::d[v] != mod) continue;
::d[v] = -d;
int l1 = min(l, mn[v]), r1 = max(r, mx[v]);
while (l1 < l || r1 > r)
{
if (l1 < l)
{
l--;
if (cl[l] && ::d[cl[l]] == mod) ::d[cl[l]] = -d, l1 = min(l1, mn[cl[l]]), r1 = max(r1, mx[cl[l]]);
}
if (r1 > r)
{
r++;
if (cl[r] && ::d[cl[r]] == mod) ::d[cl[r]] = -d, l1 = min(l1, mn[cl[r]]), r1 = max(r1, mx[cl[r]]);
}
}
if (l1 == m[col[aj]]) break;
for (int i = r1 + 1; i < n; i++)
{
if (cl[i])
{
q.push(MP(MP(d - (i - r1), cl[i]), MP(l1, r1)));
break;
}
}
for (int i = l1 - 1; i >= 0; i--)
{
if (cl[i])
{
q.push(MP(MP(d - (l1 - i), cl[i]), MP(l1, r1)));
break;
}
}
}
ans += 2 * d[cl[m[col[aj]]]];
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
292 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 |
1 ms |
292 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
288 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
292 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
272 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
292 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 |
1 ms |
292 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
288 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
292 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
272 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 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 |
284 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 |
292 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 |
0 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
292 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 |
1 ms |
292 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
288 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
292 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
272 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 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 |
284 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 |
292 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 |
263 ms |
43960 KB |
Output is correct |
31 |
Correct |
243 ms |
44372 KB |
Output is correct |
32 |
Correct |
141 ms |
30404 KB |
Output is correct |
33 |
Correct |
149 ms |
39764 KB |
Output is correct |
34 |
Correct |
147 ms |
39760 KB |
Output is correct |
35 |
Correct |
157 ms |
39772 KB |
Output is correct |
36 |
Correct |
156 ms |
39252 KB |
Output is correct |
37 |
Correct |
149 ms |
38368 KB |
Output is correct |
38 |
Correct |
149 ms |
38468 KB |
Output is correct |
39 |
Correct |
157 ms |
38352 KB |
Output is correct |
40 |
Correct |
154 ms |
38876 KB |
Output is correct |
41 |
Correct |
192 ms |
43484 KB |
Output is correct |
42 |
Correct |
187 ms |
40772 KB |
Output is correct |
43 |
Correct |
197 ms |
39236 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 |
288 KB |
Output is correct |
8 |
Correct |
1 ms |
296 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 |
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 |
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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
292 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 |
1 ms |
292 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
288 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
292 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
272 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 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 |
284 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 |
292 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 |
263 ms |
43960 KB |
Output is correct |
31 |
Correct |
243 ms |
44372 KB |
Output is correct |
32 |
Correct |
141 ms |
30404 KB |
Output is correct |
33 |
Correct |
149 ms |
39764 KB |
Output is correct |
34 |
Correct |
147 ms |
39760 KB |
Output is correct |
35 |
Correct |
157 ms |
39772 KB |
Output is correct |
36 |
Correct |
156 ms |
39252 KB |
Output is correct |
37 |
Correct |
149 ms |
38368 KB |
Output is correct |
38 |
Correct |
149 ms |
38468 KB |
Output is correct |
39 |
Correct |
157 ms |
38352 KB |
Output is correct |
40 |
Correct |
154 ms |
38876 KB |
Output is correct |
41 |
Correct |
192 ms |
43484 KB |
Output is correct |
42 |
Correct |
187 ms |
40772 KB |
Output is correct |
43 |
Correct |
197 ms |
39236 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 |
288 KB |
Output is correct |
51 |
Correct |
1 ms |
296 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 |
332 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 |
153 ms |
38620 KB |
Output is correct |
65 |
Correct |
170 ms |
38616 KB |
Output is correct |
66 |
Correct |
159 ms |
38356 KB |
Output is correct |
67 |
Correct |
155 ms |
38360 KB |
Output is correct |
68 |
Correct |
17 ms |
4004 KB |
Output is correct |
69 |
Correct |
15 ms |
4044 KB |
Output is correct |
70 |
Correct |
16 ms |
4084 KB |
Output is correct |
71 |
Correct |
16 ms |
4032 KB |
Output is correct |
72 |
Correct |
15 ms |
4044 KB |
Output is correct |
73 |
Correct |
15 ms |
4084 KB |
Output is correct |
74 |
Correct |
166 ms |
39876 KB |
Output is correct |
75 |
Correct |
153 ms |
39780 KB |
Output is correct |
76 |
Correct |
197 ms |
39244 KB |
Output is correct |
77 |
Correct |
203 ms |
39248 KB |
Output is correct |
78 |
Correct |
161 ms |
39760 KB |
Output is correct |
79 |
Correct |
158 ms |
39644 KB |
Output is correct |
80 |
Correct |
138 ms |
30404 KB |
Output is correct |
81 |
Correct |
217 ms |
40244 KB |
Output is correct |
82 |
Correct |
192 ms |
40296 KB |
Output is correct |
83 |
Correct |
174 ms |
39252 KB |
Output is correct |
84 |
Correct |
191 ms |
39128 KB |
Output is correct |
85 |
Correct |
163 ms |
38608 KB |
Output is correct |
86 |
Correct |
155 ms |
38496 KB |
Output is correct |
87 |
Correct |
172 ms |
38884 KB |
Output is correct |
88 |
Correct |
187 ms |
39396 KB |
Output is correct |
89 |
Correct |
181 ms |
39404 KB |
Output is correct |
90 |
Correct |
158 ms |
38360 KB |
Output is correct |
91 |
Correct |
158 ms |
38356 KB |
Output is correct |
92 |
Correct |
158 ms |
38488 KB |
Output is correct |