#include "books.h"
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn = 1e6 + 5;
vector<int> c(maxn, -1), L(maxn, maxn), R(maxn, -1);
void extend_for_free(int &l, int &r)
{
int tl = min({l, L[c[l]], R[c[r]]}), tr = max({r, R[c[l]], R[c[r]]});
while (tl < l || r < tr)
{
if (tl < l)
{
l--;
tl = min(tl, L[c[l]]);
tr = max(tr, R[c[l]]);
}
if (r < tr)
{
r++;
tl = min(tl, L[c[r]]);
tr = max(tr, R[c[r]]);
}
}
}
int calculate(int l, int r, int tl, int tr)
{
int ans = 0;
while (tl < l || r < tr)
{
extend_for_free(l, r);
bool left = false, right = false;
int cll = l, clr = r, crl = l, crr = r, costl = 0, costr = 0;
while (tl < cll)
{
cll--, costl += 2;
extend_for_free(cll, clr);
if (clr > r)
{
left = true;
break;
}
}
while (tr > crr)
{
crr++, costr += 2;
extend_for_free(crl, crr);
if (crl < l)
{
right = true;
break;
}
}
if (left && right) ans += min(costl, costr);
else ans += costl + costr;
l = min(cll, crl), r = max(crl, crr);
}
return ans;
}
ll minimum_walk(vector<int> p, int s)
{
int n = p.size(), num = -1, l = s, r = s;
ll ans = 0;
for (int i = 0; i < n; i++)
{
ans += abs(p[i] - i);
if (c[i] != -1) continue;
num++, L[num] = R[num] = i, c[i] = num;
int j = p[i];
while (j != i)
{
L[num] = min(L[num], j), R[num] = max(R[num], j), c[j] = num;
j = p[j];
}
if (i != p[i]) l = min(l, L[num]), r = max(r, R[num]);
}
ans += calculate(s, s, l, r);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12048 KB |
Output is correct |
2 |
Correct |
7 ms |
12024 KB |
Output is correct |
3 |
Correct |
7 ms |
11992 KB |
Output is correct |
4 |
Correct |
7 ms |
12048 KB |
Output is correct |
5 |
Correct |
7 ms |
11980 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
11980 KB |
Output is correct |
8 |
Correct |
7 ms |
11980 KB |
Output is correct |
9 |
Correct |
7 ms |
11980 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
11980 KB |
Output is correct |
12 |
Correct |
7 ms |
11980 KB |
Output is correct |
13 |
Correct |
7 ms |
11980 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
7 ms |
11980 KB |
Output is correct |
16 |
Correct |
8 ms |
11936 KB |
Output is correct |
17 |
Correct |
7 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
11980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12048 KB |
Output is correct |
2 |
Correct |
7 ms |
12024 KB |
Output is correct |
3 |
Correct |
7 ms |
11992 KB |
Output is correct |
4 |
Correct |
7 ms |
12048 KB |
Output is correct |
5 |
Correct |
7 ms |
11980 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
11980 KB |
Output is correct |
8 |
Correct |
7 ms |
11980 KB |
Output is correct |
9 |
Correct |
7 ms |
11980 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
11980 KB |
Output is correct |
12 |
Correct |
7 ms |
11980 KB |
Output is correct |
13 |
Correct |
7 ms |
11980 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
7 ms |
11980 KB |
Output is correct |
16 |
Correct |
8 ms |
11936 KB |
Output is correct |
17 |
Correct |
7 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
11980 KB |
Output is correct |
19 |
Correct |
7 ms |
11984 KB |
Output is correct |
20 |
Correct |
7 ms |
11980 KB |
Output is correct |
21 |
Correct |
7 ms |
11980 KB |
Output is correct |
22 |
Correct |
7 ms |
12024 KB |
Output is correct |
23 |
Correct |
7 ms |
11952 KB |
Output is correct |
24 |
Correct |
7 ms |
11980 KB |
Output is correct |
25 |
Correct |
7 ms |
12012 KB |
Output is correct |
26 |
Correct |
9 ms |
11980 KB |
Output is correct |
27 |
Correct |
7 ms |
11980 KB |
Output is correct |
28 |
Correct |
7 ms |
11980 KB |
Output is correct |
29 |
Correct |
7 ms |
11980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12048 KB |
Output is correct |
2 |
Correct |
7 ms |
12024 KB |
Output is correct |
3 |
Correct |
7 ms |
11992 KB |
Output is correct |
4 |
Correct |
7 ms |
12048 KB |
Output is correct |
5 |
Correct |
7 ms |
11980 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
11980 KB |
Output is correct |
8 |
Correct |
7 ms |
11980 KB |
Output is correct |
9 |
Correct |
7 ms |
11980 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
11980 KB |
Output is correct |
12 |
Correct |
7 ms |
11980 KB |
Output is correct |
13 |
Correct |
7 ms |
11980 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
7 ms |
11980 KB |
Output is correct |
16 |
Correct |
8 ms |
11936 KB |
Output is correct |
17 |
Correct |
7 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
11980 KB |
Output is correct |
19 |
Correct |
7 ms |
11984 KB |
Output is correct |
20 |
Correct |
7 ms |
11980 KB |
Output is correct |
21 |
Correct |
7 ms |
11980 KB |
Output is correct |
22 |
Correct |
7 ms |
12024 KB |
Output is correct |
23 |
Correct |
7 ms |
11952 KB |
Output is correct |
24 |
Correct |
7 ms |
11980 KB |
Output is correct |
25 |
Correct |
7 ms |
12012 KB |
Output is correct |
26 |
Correct |
9 ms |
11980 KB |
Output is correct |
27 |
Correct |
7 ms |
11980 KB |
Output is correct |
28 |
Correct |
7 ms |
11980 KB |
Output is correct |
29 |
Correct |
7 ms |
11980 KB |
Output is correct |
30 |
Correct |
195 ms |
26552 KB |
Output is correct |
31 |
Correct |
198 ms |
26612 KB |
Output is correct |
32 |
Correct |
124 ms |
26560 KB |
Output is correct |
33 |
Correct |
142 ms |
26940 KB |
Output is correct |
34 |
Correct |
145 ms |
26564 KB |
Output is correct |
35 |
Correct |
141 ms |
26548 KB |
Output is correct |
36 |
Correct |
141 ms |
26560 KB |
Output is correct |
37 |
Correct |
138 ms |
26552 KB |
Output is correct |
38 |
Correct |
135 ms |
26556 KB |
Output is correct |
39 |
Correct |
133 ms |
26564 KB |
Output is correct |
40 |
Correct |
138 ms |
26560 KB |
Output is correct |
41 |
Correct |
156 ms |
26560 KB |
Output is correct |
42 |
Correct |
153 ms |
26564 KB |
Output is correct |
43 |
Correct |
137 ms |
26568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
3 |
Correct |
7 ms |
11980 KB |
Output is correct |
4 |
Correct |
7 ms |
11980 KB |
Output is correct |
5 |
Correct |
7 ms |
11980 KB |
Output is correct |
6 |
Incorrect |
7 ms |
11980 KB |
3rd lines differ - on the 1st token, expected: '22140', found: '22134' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12048 KB |
Output is correct |
2 |
Correct |
7 ms |
12024 KB |
Output is correct |
3 |
Correct |
7 ms |
11992 KB |
Output is correct |
4 |
Correct |
7 ms |
12048 KB |
Output is correct |
5 |
Correct |
7 ms |
11980 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
11980 KB |
Output is correct |
8 |
Correct |
7 ms |
11980 KB |
Output is correct |
9 |
Correct |
7 ms |
11980 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
11980 KB |
Output is correct |
12 |
Correct |
7 ms |
11980 KB |
Output is correct |
13 |
Correct |
7 ms |
11980 KB |
Output is correct |
14 |
Correct |
7 ms |
11980 KB |
Output is correct |
15 |
Correct |
7 ms |
11980 KB |
Output is correct |
16 |
Correct |
8 ms |
11936 KB |
Output is correct |
17 |
Correct |
7 ms |
11980 KB |
Output is correct |
18 |
Correct |
7 ms |
11980 KB |
Output is correct |
19 |
Correct |
7 ms |
11984 KB |
Output is correct |
20 |
Correct |
7 ms |
11980 KB |
Output is correct |
21 |
Correct |
7 ms |
11980 KB |
Output is correct |
22 |
Correct |
7 ms |
12024 KB |
Output is correct |
23 |
Correct |
7 ms |
11952 KB |
Output is correct |
24 |
Correct |
7 ms |
11980 KB |
Output is correct |
25 |
Correct |
7 ms |
12012 KB |
Output is correct |
26 |
Correct |
9 ms |
11980 KB |
Output is correct |
27 |
Correct |
7 ms |
11980 KB |
Output is correct |
28 |
Correct |
7 ms |
11980 KB |
Output is correct |
29 |
Correct |
7 ms |
11980 KB |
Output is correct |
30 |
Correct |
195 ms |
26552 KB |
Output is correct |
31 |
Correct |
198 ms |
26612 KB |
Output is correct |
32 |
Correct |
124 ms |
26560 KB |
Output is correct |
33 |
Correct |
142 ms |
26940 KB |
Output is correct |
34 |
Correct |
145 ms |
26564 KB |
Output is correct |
35 |
Correct |
141 ms |
26548 KB |
Output is correct |
36 |
Correct |
141 ms |
26560 KB |
Output is correct |
37 |
Correct |
138 ms |
26552 KB |
Output is correct |
38 |
Correct |
135 ms |
26556 KB |
Output is correct |
39 |
Correct |
133 ms |
26564 KB |
Output is correct |
40 |
Correct |
138 ms |
26560 KB |
Output is correct |
41 |
Correct |
156 ms |
26560 KB |
Output is correct |
42 |
Correct |
153 ms |
26564 KB |
Output is correct |
43 |
Correct |
137 ms |
26568 KB |
Output is correct |
44 |
Correct |
6 ms |
11980 KB |
Output is correct |
45 |
Correct |
7 ms |
11980 KB |
Output is correct |
46 |
Correct |
7 ms |
11980 KB |
Output is correct |
47 |
Correct |
7 ms |
11980 KB |
Output is correct |
48 |
Correct |
7 ms |
11980 KB |
Output is correct |
49 |
Incorrect |
7 ms |
11980 KB |
3rd lines differ - on the 1st token, expected: '22140', found: '22134' |
50 |
Halted |
0 ms |
0 KB |
- |