#include "books.h"
#include<bits/stdc++.h>
using namespace std;
int nr, N, BG, p[1000009], d[1000009], mars[1000009];
set < int > S;
vector < int > v[1000009];
void add (int node, int newD)
{
if (d[node] != -1) return ;
d[node] = newD, v[newD].push_back (node), S.erase (node);
int l = node, r = p[node];
if (l > r) swap (l, r);
while (1)
{
auto it = S.lower_bound (l);
if (it == S.end () || *it > r) break;
add (*it, newD);
}
}
long long minimum_walk (vector < int > pp, int ss)
{
N = pp.size (), BG = ss + 1;
for (int i=1; i<=N; i++)
p[i] = pp[i - 1] + 1;
long long ans = 0;
int x = 0, y = 0;
for (int i=1; i<=N; i++)
{
int curr = i - p[i];
if (curr < 0) curr = -curr;
ans += curr;
int l = i, r = p[i];
if (l > r) swap (l, r);
mars[l] ++, mars[r] --;
if (i != p[i] || i == BG)
{
if (x == 0) x = i;
y = i;
}
}
for (int i=1; i<N; i++)
mars[i] += mars[i - 1];
if (mars[BG - 1] != 0 || mars[BG] != 0)
{
int lft = BG, rgt = BG;
while (mars[rgt] > 0) rgt ++;
while (mars[lft - 1] > 0) lft --;
for (int i=lft; i<=rgt; i++)
S.insert (i), d[i] = -1;
add (BG, 0);
for (int i=0; !v[i].empty (); i++)
for (auto it : v[i])
{
if (it > 1) add (it - 1, i + 1);
if (it < N) add (it + 1, i + 1);
}
int maxi = 0;
for (int i=lft; i<=rgt; i++)
maxi = max (maxi, d[i]);
ans += 2LL * maxi;
}
for (int i=x; i<y; i++)
if (mars[i] == 0)
ans += 2;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
38 ms |
23908 KB |
Output is correct |
3 |
Correct |
21 ms |
23984 KB |
Output is correct |
4 |
Correct |
20 ms |
24032 KB |
Output is correct |
5 |
Correct |
20 ms |
24044 KB |
Output is correct |
6 |
Correct |
19 ms |
24044 KB |
Output is correct |
7 |
Correct |
20 ms |
24120 KB |
Output is correct |
8 |
Correct |
22 ms |
24120 KB |
Output is correct |
9 |
Correct |
20 ms |
24120 KB |
Output is correct |
10 |
Correct |
20 ms |
24120 KB |
Output is correct |
11 |
Correct |
20 ms |
24120 KB |
Output is correct |
12 |
Correct |
20 ms |
24120 KB |
Output is correct |
13 |
Correct |
20 ms |
24120 KB |
Output is correct |
14 |
Correct |
20 ms |
24120 KB |
Output is correct |
15 |
Correct |
20 ms |
24120 KB |
Output is correct |
16 |
Correct |
20 ms |
24120 KB |
Output is correct |
17 |
Correct |
22 ms |
24136 KB |
Output is correct |
18 |
Correct |
20 ms |
24184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
38 ms |
23908 KB |
Output is correct |
3 |
Correct |
21 ms |
23984 KB |
Output is correct |
4 |
Correct |
20 ms |
24032 KB |
Output is correct |
5 |
Correct |
20 ms |
24044 KB |
Output is correct |
6 |
Correct |
19 ms |
24044 KB |
Output is correct |
7 |
Correct |
20 ms |
24120 KB |
Output is correct |
8 |
Correct |
22 ms |
24120 KB |
Output is correct |
9 |
Correct |
20 ms |
24120 KB |
Output is correct |
10 |
Correct |
20 ms |
24120 KB |
Output is correct |
11 |
Correct |
20 ms |
24120 KB |
Output is correct |
12 |
Correct |
20 ms |
24120 KB |
Output is correct |
13 |
Correct |
20 ms |
24120 KB |
Output is correct |
14 |
Correct |
20 ms |
24120 KB |
Output is correct |
15 |
Correct |
20 ms |
24120 KB |
Output is correct |
16 |
Correct |
20 ms |
24120 KB |
Output is correct |
17 |
Correct |
22 ms |
24136 KB |
Output is correct |
18 |
Correct |
20 ms |
24184 KB |
Output is correct |
19 |
Correct |
20 ms |
24188 KB |
Output is correct |
20 |
Correct |
20 ms |
24188 KB |
Output is correct |
21 |
Correct |
21 ms |
24188 KB |
Output is correct |
22 |
Correct |
21 ms |
24188 KB |
Output is correct |
23 |
Correct |
22 ms |
24188 KB |
Output is correct |
24 |
Correct |
21 ms |
24236 KB |
Output is correct |
25 |
Correct |
22 ms |
24236 KB |
Output is correct |
26 |
Correct |
21 ms |
24236 KB |
Output is correct |
27 |
Correct |
21 ms |
24236 KB |
Output is correct |
28 |
Correct |
23 ms |
24236 KB |
Output is correct |
29 |
Correct |
25 ms |
24236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
38 ms |
23908 KB |
Output is correct |
3 |
Correct |
21 ms |
23984 KB |
Output is correct |
4 |
Correct |
20 ms |
24032 KB |
Output is correct |
5 |
Correct |
20 ms |
24044 KB |
Output is correct |
6 |
Correct |
19 ms |
24044 KB |
Output is correct |
7 |
Correct |
20 ms |
24120 KB |
Output is correct |
8 |
Correct |
22 ms |
24120 KB |
Output is correct |
9 |
Correct |
20 ms |
24120 KB |
Output is correct |
10 |
Correct |
20 ms |
24120 KB |
Output is correct |
11 |
Correct |
20 ms |
24120 KB |
Output is correct |
12 |
Correct |
20 ms |
24120 KB |
Output is correct |
13 |
Correct |
20 ms |
24120 KB |
Output is correct |
14 |
Correct |
20 ms |
24120 KB |
Output is correct |
15 |
Correct |
20 ms |
24120 KB |
Output is correct |
16 |
Correct |
20 ms |
24120 KB |
Output is correct |
17 |
Correct |
22 ms |
24136 KB |
Output is correct |
18 |
Correct |
20 ms |
24184 KB |
Output is correct |
19 |
Correct |
20 ms |
24188 KB |
Output is correct |
20 |
Correct |
20 ms |
24188 KB |
Output is correct |
21 |
Correct |
21 ms |
24188 KB |
Output is correct |
22 |
Correct |
21 ms |
24188 KB |
Output is correct |
23 |
Correct |
22 ms |
24188 KB |
Output is correct |
24 |
Correct |
21 ms |
24236 KB |
Output is correct |
25 |
Correct |
22 ms |
24236 KB |
Output is correct |
26 |
Correct |
21 ms |
24236 KB |
Output is correct |
27 |
Correct |
21 ms |
24236 KB |
Output is correct |
28 |
Correct |
23 ms |
24236 KB |
Output is correct |
29 |
Correct |
25 ms |
24236 KB |
Output is correct |
30 |
Correct |
863 ms |
114108 KB |
Output is correct |
31 |
Correct |
853 ms |
114148 KB |
Output is correct |
32 |
Correct |
173 ms |
114148 KB |
Output is correct |
33 |
Correct |
169 ms |
114148 KB |
Output is correct |
34 |
Correct |
227 ms |
114148 KB |
Output is correct |
35 |
Correct |
174 ms |
114148 KB |
Output is correct |
36 |
Correct |
190 ms |
114148 KB |
Output is correct |
37 |
Correct |
174 ms |
114148 KB |
Output is correct |
38 |
Correct |
198 ms |
114148 KB |
Output is correct |
39 |
Correct |
190 ms |
114148 KB |
Output is correct |
40 |
Correct |
202 ms |
114148 KB |
Output is correct |
41 |
Correct |
327 ms |
114148 KB |
Output is correct |
42 |
Correct |
208 ms |
114148 KB |
Output is correct |
43 |
Correct |
908 ms |
114148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
114148 KB |
Output is correct |
2 |
Correct |
21 ms |
114148 KB |
Output is correct |
3 |
Correct |
21 ms |
114148 KB |
Output is correct |
4 |
Correct |
21 ms |
114148 KB |
Output is correct |
5 |
Correct |
22 ms |
114148 KB |
Output is correct |
6 |
Correct |
23 ms |
114148 KB |
Output is correct |
7 |
Correct |
27 ms |
114148 KB |
Output is correct |
8 |
Correct |
24 ms |
114148 KB |
Output is correct |
9 |
Correct |
21 ms |
114148 KB |
Output is correct |
10 |
Correct |
21 ms |
114148 KB |
Output is correct |
11 |
Correct |
21 ms |
114148 KB |
Output is correct |
12 |
Correct |
24 ms |
114148 KB |
Output is correct |
13 |
Correct |
24 ms |
114148 KB |
Output is correct |
14 |
Correct |
22 ms |
114148 KB |
Output is correct |
15 |
Correct |
22 ms |
114148 KB |
Output is correct |
16 |
Correct |
22 ms |
114148 KB |
Output is correct |
17 |
Correct |
23 ms |
114148 KB |
Output is correct |
18 |
Correct |
21 ms |
114148 KB |
Output is correct |
19 |
Correct |
22 ms |
114148 KB |
Output is correct |
20 |
Correct |
22 ms |
114148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
38 ms |
23908 KB |
Output is correct |
3 |
Correct |
21 ms |
23984 KB |
Output is correct |
4 |
Correct |
20 ms |
24032 KB |
Output is correct |
5 |
Correct |
20 ms |
24044 KB |
Output is correct |
6 |
Correct |
19 ms |
24044 KB |
Output is correct |
7 |
Correct |
20 ms |
24120 KB |
Output is correct |
8 |
Correct |
22 ms |
24120 KB |
Output is correct |
9 |
Correct |
20 ms |
24120 KB |
Output is correct |
10 |
Correct |
20 ms |
24120 KB |
Output is correct |
11 |
Correct |
20 ms |
24120 KB |
Output is correct |
12 |
Correct |
20 ms |
24120 KB |
Output is correct |
13 |
Correct |
20 ms |
24120 KB |
Output is correct |
14 |
Correct |
20 ms |
24120 KB |
Output is correct |
15 |
Correct |
20 ms |
24120 KB |
Output is correct |
16 |
Correct |
20 ms |
24120 KB |
Output is correct |
17 |
Correct |
22 ms |
24136 KB |
Output is correct |
18 |
Correct |
20 ms |
24184 KB |
Output is correct |
19 |
Correct |
20 ms |
24188 KB |
Output is correct |
20 |
Correct |
20 ms |
24188 KB |
Output is correct |
21 |
Correct |
21 ms |
24188 KB |
Output is correct |
22 |
Correct |
21 ms |
24188 KB |
Output is correct |
23 |
Correct |
22 ms |
24188 KB |
Output is correct |
24 |
Correct |
21 ms |
24236 KB |
Output is correct |
25 |
Correct |
22 ms |
24236 KB |
Output is correct |
26 |
Correct |
21 ms |
24236 KB |
Output is correct |
27 |
Correct |
21 ms |
24236 KB |
Output is correct |
28 |
Correct |
23 ms |
24236 KB |
Output is correct |
29 |
Correct |
25 ms |
24236 KB |
Output is correct |
30 |
Correct |
863 ms |
114108 KB |
Output is correct |
31 |
Correct |
853 ms |
114148 KB |
Output is correct |
32 |
Correct |
173 ms |
114148 KB |
Output is correct |
33 |
Correct |
169 ms |
114148 KB |
Output is correct |
34 |
Correct |
227 ms |
114148 KB |
Output is correct |
35 |
Correct |
174 ms |
114148 KB |
Output is correct |
36 |
Correct |
190 ms |
114148 KB |
Output is correct |
37 |
Correct |
174 ms |
114148 KB |
Output is correct |
38 |
Correct |
198 ms |
114148 KB |
Output is correct |
39 |
Correct |
190 ms |
114148 KB |
Output is correct |
40 |
Correct |
202 ms |
114148 KB |
Output is correct |
41 |
Correct |
327 ms |
114148 KB |
Output is correct |
42 |
Correct |
208 ms |
114148 KB |
Output is correct |
43 |
Correct |
908 ms |
114148 KB |
Output is correct |
44 |
Correct |
21 ms |
114148 KB |
Output is correct |
45 |
Correct |
21 ms |
114148 KB |
Output is correct |
46 |
Correct |
21 ms |
114148 KB |
Output is correct |
47 |
Correct |
21 ms |
114148 KB |
Output is correct |
48 |
Correct |
22 ms |
114148 KB |
Output is correct |
49 |
Correct |
23 ms |
114148 KB |
Output is correct |
50 |
Correct |
27 ms |
114148 KB |
Output is correct |
51 |
Correct |
24 ms |
114148 KB |
Output is correct |
52 |
Correct |
21 ms |
114148 KB |
Output is correct |
53 |
Correct |
21 ms |
114148 KB |
Output is correct |
54 |
Correct |
21 ms |
114148 KB |
Output is correct |
55 |
Correct |
24 ms |
114148 KB |
Output is correct |
56 |
Correct |
24 ms |
114148 KB |
Output is correct |
57 |
Correct |
22 ms |
114148 KB |
Output is correct |
58 |
Correct |
22 ms |
114148 KB |
Output is correct |
59 |
Correct |
22 ms |
114148 KB |
Output is correct |
60 |
Correct |
23 ms |
114148 KB |
Output is correct |
61 |
Correct |
21 ms |
114148 KB |
Output is correct |
62 |
Correct |
22 ms |
114148 KB |
Output is correct |
63 |
Correct |
22 ms |
114148 KB |
Output is correct |
64 |
Correct |
860 ms |
114148 KB |
Output is correct |
65 |
Correct |
871 ms |
114148 KB |
Output is correct |
66 |
Correct |
903 ms |
114148 KB |
Output is correct |
67 |
Correct |
849 ms |
117704 KB |
Output is correct |
68 |
Correct |
79 ms |
117704 KB |
Output is correct |
69 |
Correct |
89 ms |
117704 KB |
Output is correct |
70 |
Correct |
91 ms |
117704 KB |
Output is correct |
71 |
Correct |
84 ms |
117704 KB |
Output is correct |
72 |
Correct |
82 ms |
117704 KB |
Output is correct |
73 |
Correct |
97 ms |
117704 KB |
Output is correct |
74 |
Correct |
203 ms |
117704 KB |
Output is correct |
75 |
Correct |
207 ms |
117704 KB |
Output is correct |
76 |
Correct |
913 ms |
141644 KB |
Output is correct |
77 |
Correct |
923 ms |
148324 KB |
Output is correct |
78 |
Correct |
725 ms |
148324 KB |
Output is correct |
79 |
Correct |
752 ms |
151956 KB |
Output is correct |
80 |
Correct |
162 ms |
151956 KB |
Output is correct |
81 |
Correct |
915 ms |
175172 KB |
Output is correct |
82 |
Correct |
937 ms |
181916 KB |
Output is correct |
83 |
Correct |
1008 ms |
188788 KB |
Output is correct |
84 |
Correct |
1012 ms |
195584 KB |
Output is correct |
85 |
Correct |
935 ms |
202180 KB |
Output is correct |
86 |
Correct |
962 ms |
208984 KB |
Output is correct |
87 |
Correct |
1037 ms |
215652 KB |
Output is correct |
88 |
Correct |
1020 ms |
222344 KB |
Output is correct |
89 |
Correct |
1020 ms |
229268 KB |
Output is correct |
90 |
Correct |
1006 ms |
235884 KB |
Output is correct |
91 |
Correct |
929 ms |
242760 KB |
Output is correct |
92 |
Correct |
995 ms |
249660 KB |
Output is correct |