#pragma GCC optimize("Ofast,unroll-loops")
#include "books.h"
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
int n, reach[1000006], g[1000006], l, r;
vector<int> out[1000006];
long long minimum_walk(vector<int> p, int s)
{
{
n = p.size();
int l = 0, r = n - 1;
while (l < s && p[l] == l)
l++;
while (r > s && p[r] == r)
r--;
p = vector<int>(p.begin() + l, p.begin() + r + 1);
n = p.size();
s -= l;
for (auto &i : p)
i -= l;
}
ll ans = 0;
vector<pii> edge;
for (int i = 0; i < n; i++)
reach[i] = i;
for (int i = 0; i < n; i++)
{
ans += abs(p[i] - i);
int u = min(p[i], i), v = max(p[i], i);
if (v <= s || s <= u)
reach[u] = max(reach[u], v);
else
edge.emplace_back(pii(u, v));
}
for (int i = 1; i < n; i++)
reach[i] = max(reach[i - 1], reach[i]);
int last = 0, m = 0;
for (int i = 0; i < n; i++)
{
if (last < i)
++m;
g[i] = m;
last = max(last, reach[i]);
}
int L = g[s], R = g[s];
for (auto &[u, v] : edge)
{
u = g[u], v = g[v];
L = min(L, u);
R = max(R, v);
out[u].emplace_back(v);
out[v].emplace_back(u);
}
ans += 2 * (L + m - R);
int l = g[s] + 1, r = g[s] - 1;
ans -= 2;
while (L < l || r < R)
{
ans += 2;
int nl = max(L, l - 1), nr = min(R, r + 1);
while (nl < l || r < nr)
{
if (nl < l)
{
l--;
for (auto i : out[l])
nl = min(i, nl), nr = max(i, nr);
}
else
{
r++;
for (auto i : out[r])
nl = min(i, nl), nr = max(i, nr);
}
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23796 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23804 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
13 ms |
23692 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23768 KB |
Output is correct |
8 |
Correct |
14 ms |
23736 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23716 KB |
Output is correct |
11 |
Correct |
14 ms |
23800 KB |
Output is correct |
12 |
Correct |
13 ms |
23736 KB |
Output is correct |
13 |
Correct |
15 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23708 KB |
Output is correct |
15 |
Correct |
15 ms |
23720 KB |
Output is correct |
16 |
Correct |
13 ms |
23756 KB |
Output is correct |
17 |
Correct |
13 ms |
23748 KB |
Output is correct |
18 |
Correct |
14 ms |
23700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23796 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23804 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
13 ms |
23692 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23768 KB |
Output is correct |
8 |
Correct |
14 ms |
23736 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23716 KB |
Output is correct |
11 |
Correct |
14 ms |
23800 KB |
Output is correct |
12 |
Correct |
13 ms |
23736 KB |
Output is correct |
13 |
Correct |
15 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23708 KB |
Output is correct |
15 |
Correct |
15 ms |
23720 KB |
Output is correct |
16 |
Correct |
13 ms |
23756 KB |
Output is correct |
17 |
Correct |
13 ms |
23748 KB |
Output is correct |
18 |
Correct |
14 ms |
23700 KB |
Output is correct |
19 |
Correct |
13 ms |
23804 KB |
Output is correct |
20 |
Correct |
14 ms |
23708 KB |
Output is correct |
21 |
Correct |
13 ms |
23764 KB |
Output is correct |
22 |
Correct |
13 ms |
23812 KB |
Output is correct |
23 |
Correct |
14 ms |
23764 KB |
Output is correct |
24 |
Correct |
14 ms |
23820 KB |
Output is correct |
25 |
Correct |
14 ms |
23764 KB |
Output is correct |
26 |
Correct |
13 ms |
23824 KB |
Output is correct |
27 |
Correct |
14 ms |
23764 KB |
Output is correct |
28 |
Correct |
13 ms |
23764 KB |
Output is correct |
29 |
Correct |
13 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23796 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23804 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
13 ms |
23692 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23768 KB |
Output is correct |
8 |
Correct |
14 ms |
23736 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23716 KB |
Output is correct |
11 |
Correct |
14 ms |
23800 KB |
Output is correct |
12 |
Correct |
13 ms |
23736 KB |
Output is correct |
13 |
Correct |
15 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23708 KB |
Output is correct |
15 |
Correct |
15 ms |
23720 KB |
Output is correct |
16 |
Correct |
13 ms |
23756 KB |
Output is correct |
17 |
Correct |
13 ms |
23748 KB |
Output is correct |
18 |
Correct |
14 ms |
23700 KB |
Output is correct |
19 |
Correct |
13 ms |
23804 KB |
Output is correct |
20 |
Correct |
14 ms |
23708 KB |
Output is correct |
21 |
Correct |
13 ms |
23764 KB |
Output is correct |
22 |
Correct |
13 ms |
23812 KB |
Output is correct |
23 |
Correct |
14 ms |
23764 KB |
Output is correct |
24 |
Correct |
14 ms |
23820 KB |
Output is correct |
25 |
Correct |
14 ms |
23764 KB |
Output is correct |
26 |
Correct |
13 ms |
23824 KB |
Output is correct |
27 |
Correct |
14 ms |
23764 KB |
Output is correct |
28 |
Correct |
13 ms |
23764 KB |
Output is correct |
29 |
Correct |
13 ms |
23764 KB |
Output is correct |
30 |
Correct |
94 ms |
39388 KB |
Output is correct |
31 |
Correct |
94 ms |
39408 KB |
Output is correct |
32 |
Correct |
84 ms |
31540 KB |
Output is correct |
33 |
Correct |
93 ms |
39420 KB |
Output is correct |
34 |
Correct |
94 ms |
39412 KB |
Output is correct |
35 |
Correct |
94 ms |
39404 KB |
Output is correct |
36 |
Correct |
101 ms |
39404 KB |
Output is correct |
37 |
Correct |
94 ms |
39432 KB |
Output is correct |
38 |
Correct |
95 ms |
39420 KB |
Output is correct |
39 |
Correct |
94 ms |
39408 KB |
Output is correct |
40 |
Correct |
95 ms |
39412 KB |
Output is correct |
41 |
Correct |
100 ms |
39408 KB |
Output is correct |
42 |
Correct |
94 ms |
39420 KB |
Output is correct |
43 |
Correct |
97 ms |
39404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23780 KB |
Output is correct |
3 |
Correct |
14 ms |
23788 KB |
Output is correct |
4 |
Correct |
14 ms |
23824 KB |
Output is correct |
5 |
Correct |
14 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
14 ms |
23796 KB |
Output is correct |
8 |
Correct |
14 ms |
23792 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
14 ms |
23764 KB |
Output is correct |
11 |
Correct |
14 ms |
23792 KB |
Output is correct |
12 |
Correct |
13 ms |
23764 KB |
Output is correct |
13 |
Correct |
14 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23792 KB |
Output is correct |
15 |
Correct |
14 ms |
23716 KB |
Output is correct |
16 |
Correct |
14 ms |
23700 KB |
Output is correct |
17 |
Correct |
13 ms |
23808 KB |
Output is correct |
18 |
Correct |
13 ms |
23828 KB |
Output is correct |
19 |
Correct |
14 ms |
23764 KB |
Output is correct |
20 |
Correct |
14 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23796 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23804 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
13 ms |
23692 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23768 KB |
Output is correct |
8 |
Correct |
14 ms |
23736 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23716 KB |
Output is correct |
11 |
Correct |
14 ms |
23800 KB |
Output is correct |
12 |
Correct |
13 ms |
23736 KB |
Output is correct |
13 |
Correct |
15 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23708 KB |
Output is correct |
15 |
Correct |
15 ms |
23720 KB |
Output is correct |
16 |
Correct |
13 ms |
23756 KB |
Output is correct |
17 |
Correct |
13 ms |
23748 KB |
Output is correct |
18 |
Correct |
14 ms |
23700 KB |
Output is correct |
19 |
Correct |
13 ms |
23804 KB |
Output is correct |
20 |
Correct |
14 ms |
23708 KB |
Output is correct |
21 |
Correct |
13 ms |
23764 KB |
Output is correct |
22 |
Correct |
13 ms |
23812 KB |
Output is correct |
23 |
Correct |
14 ms |
23764 KB |
Output is correct |
24 |
Correct |
14 ms |
23820 KB |
Output is correct |
25 |
Correct |
14 ms |
23764 KB |
Output is correct |
26 |
Correct |
13 ms |
23824 KB |
Output is correct |
27 |
Correct |
14 ms |
23764 KB |
Output is correct |
28 |
Correct |
13 ms |
23764 KB |
Output is correct |
29 |
Correct |
13 ms |
23764 KB |
Output is correct |
30 |
Correct |
94 ms |
39388 KB |
Output is correct |
31 |
Correct |
94 ms |
39408 KB |
Output is correct |
32 |
Correct |
84 ms |
31540 KB |
Output is correct |
33 |
Correct |
93 ms |
39420 KB |
Output is correct |
34 |
Correct |
94 ms |
39412 KB |
Output is correct |
35 |
Correct |
94 ms |
39404 KB |
Output is correct |
36 |
Correct |
101 ms |
39404 KB |
Output is correct |
37 |
Correct |
94 ms |
39432 KB |
Output is correct |
38 |
Correct |
95 ms |
39420 KB |
Output is correct |
39 |
Correct |
94 ms |
39408 KB |
Output is correct |
40 |
Correct |
95 ms |
39412 KB |
Output is correct |
41 |
Correct |
100 ms |
39408 KB |
Output is correct |
42 |
Correct |
94 ms |
39420 KB |
Output is correct |
43 |
Correct |
97 ms |
39404 KB |
Output is correct |
44 |
Correct |
13 ms |
23764 KB |
Output is correct |
45 |
Correct |
13 ms |
23780 KB |
Output is correct |
46 |
Correct |
14 ms |
23788 KB |
Output is correct |
47 |
Correct |
14 ms |
23824 KB |
Output is correct |
48 |
Correct |
14 ms |
23764 KB |
Output is correct |
49 |
Correct |
13 ms |
23764 KB |
Output is correct |
50 |
Correct |
14 ms |
23796 KB |
Output is correct |
51 |
Correct |
14 ms |
23792 KB |
Output is correct |
52 |
Correct |
13 ms |
23764 KB |
Output is correct |
53 |
Correct |
14 ms |
23764 KB |
Output is correct |
54 |
Correct |
14 ms |
23792 KB |
Output is correct |
55 |
Correct |
13 ms |
23764 KB |
Output is correct |
56 |
Correct |
14 ms |
23764 KB |
Output is correct |
57 |
Correct |
13 ms |
23792 KB |
Output is correct |
58 |
Correct |
14 ms |
23716 KB |
Output is correct |
59 |
Correct |
14 ms |
23700 KB |
Output is correct |
60 |
Correct |
13 ms |
23808 KB |
Output is correct |
61 |
Correct |
13 ms |
23828 KB |
Output is correct |
62 |
Correct |
14 ms |
23764 KB |
Output is correct |
63 |
Correct |
14 ms |
23764 KB |
Output is correct |
64 |
Correct |
100 ms |
46060 KB |
Output is correct |
65 |
Correct |
96 ms |
46044 KB |
Output is correct |
66 |
Correct |
93 ms |
46176 KB |
Output is correct |
67 |
Correct |
96 ms |
46176 KB |
Output is correct |
68 |
Correct |
22 ms |
25932 KB |
Output is correct |
69 |
Correct |
23 ms |
25836 KB |
Output is correct |
70 |
Correct |
22 ms |
25932 KB |
Output is correct |
71 |
Correct |
22 ms |
25840 KB |
Output is correct |
72 |
Correct |
21 ms |
25836 KB |
Output is correct |
73 |
Correct |
22 ms |
26188 KB |
Output is correct |
74 |
Correct |
95 ms |
46052 KB |
Output is correct |
75 |
Correct |
97 ms |
46044 KB |
Output is correct |
76 |
Correct |
99 ms |
46216 KB |
Output is correct |
77 |
Correct |
92 ms |
46184 KB |
Output is correct |
78 |
Correct |
99 ms |
46132 KB |
Output is correct |
79 |
Correct |
108 ms |
46044 KB |
Output is correct |
80 |
Correct |
89 ms |
38312 KB |
Output is correct |
81 |
Correct |
157 ms |
72236 KB |
Output is correct |
82 |
Correct |
131 ms |
72188 KB |
Output is correct |
83 |
Correct |
101 ms |
48456 KB |
Output is correct |
84 |
Correct |
110 ms |
47836 KB |
Output is correct |
85 |
Correct |
95 ms |
46728 KB |
Output is correct |
86 |
Correct |
93 ms |
46296 KB |
Output is correct |
87 |
Correct |
120 ms |
58172 KB |
Output is correct |
88 |
Correct |
119 ms |
53168 KB |
Output is correct |
89 |
Correct |
109 ms |
49248 KB |
Output is correct |
90 |
Correct |
93 ms |
46136 KB |
Output is correct |
91 |
Correct |
94 ms |
46172 KB |
Output is correct |
92 |
Correct |
94 ms |
46048 KB |
Output is correct |