#include "books.h"
#include<bits/stdc++.h>
using namespace std;
int n, minl, maxr;
vector<int> pref, f1, f2;
vector<pair<int, int>> cyc;
pair<int, int> getrange(int l, int r) {
int lv, rv;
lv = min(l, min(cyc[l].first, cyc[r].first));
rv = max(r, max(cyc[l].second, cyc[r].second));
while(lv < l || r < rv) {
if (lv < l) {
l--;
lv = min(lv, cyc[l].first);
rv = max(rv, cyc[l].second);
} else {
r++;
lv = min(lv, cyc[r].first);
rv = max(rv, cyc[r].second);
}
}
return {l, r};
}
long long minimum_walk(vector<int> p, int s) {
long long int ans = 0;
n = p.size();
pref.resize(n+1, 0);
f1.resize(n, 0);
f2.resize(n, 0);
cyc.resize(n);
minl = INT_MAX;
maxr = 0;
for (int i = 0; i < n; i++) {
ans += abs(i-p[i]);
}
vector<pair<int, int>> cycles;
vector<int> vis(n, 0);
for (int i = 0; i < n; i++) {
if (p[i] == i) {
cyc[i] = {i, i};
continue;
}
if (vis[i]) {
continue;
}
int a = i, l = i, r = i;
vis[i] = 1;
a = p[i];
vis[a] = 1;
l = min(l, a);
r = max(r, a);
while(a != i) {
a = p[a];
vis[a] = 1;
l = min(l, a);
r = max(r, a);
}
a = p[a];
cyc[a] = {l, r};
while(a != i) {
a = p[a];
cyc[a] = {l, r};
}
minl = min(minl, l);
maxr = max(maxr, r);
cycles.push_back({min(l, r), max(l, r)});
}
for (auto [i, j] : cycles) {
pref[i] += 1;
pref[j] -= 1;
}
for (int i = 1; i < n; i++) {
pref[i] += pref[i-1];
}
if (s == 0) {
for (int i = n-1; i >= 0; i--) {
if (pref[i] > 0) {
f2[i] = 1;
}
if (i < n-1) {
f2[i] |= f2[i+1];
}
}
for (int i = n-1; i >= 0; i--) {
if (f2[i] && pref[i] == 0) {
ans += 2;
}
}
} else {
int l = s, r = s;
pair<int, int> ra;
ra = getrange(l, r);
l = ra.first;
r = ra.second;
while(minl < l || r < maxr) {
bool f1 = 0, f2 = 0;
int dp1 = 0, dp2 = 0, lv1 = l, rv1 = r, lv2 = l, rv2 = r;
while(minl < lv1) {
lv1--;
ra = getrange(lv1, rv1);
lv1 = ra.first;
rv1 = ra.second;
dp1 += 2;
if (rv1 > r) {
f1 = 1;
break;
}
}
while(rv2 < maxr) {
rv2++;
ra = getrange(lv2, rv2);
lv2 = ra.first;
rv2 = ra.second;
dp2 += 2;
if (lv2 < l) {
f2 = 1;
break;
}
}
if (f1 && f2) {
ans += min(dp1, dp2);
} else {
ans += dp1+dp2;
}
l = min(lv1, lv2);
r = max(rv1, rv2);
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
432 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
432 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
432 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
124 ms |
37972 KB |
Output is correct |
31 |
Correct |
122 ms |
38168 KB |
Output is correct |
32 |
Correct |
79 ms |
37944 KB |
Output is correct |
33 |
Correct |
88 ms |
40132 KB |
Output is correct |
34 |
Correct |
89 ms |
40136 KB |
Output is correct |
35 |
Correct |
101 ms |
40304 KB |
Output is correct |
36 |
Correct |
89 ms |
40164 KB |
Output is correct |
37 |
Correct |
92 ms |
38484 KB |
Output is correct |
38 |
Correct |
84 ms |
38228 KB |
Output is correct |
39 |
Correct |
83 ms |
38164 KB |
Output is correct |
40 |
Correct |
92 ms |
37972 KB |
Output is correct |
41 |
Correct |
113 ms |
37912 KB |
Output is correct |
42 |
Correct |
106 ms |
37976 KB |
Output is correct |
43 |
Correct |
82 ms |
40136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
440 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
440 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
432 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
124 ms |
37972 KB |
Output is correct |
31 |
Correct |
122 ms |
38168 KB |
Output is correct |
32 |
Correct |
79 ms |
37944 KB |
Output is correct |
33 |
Correct |
88 ms |
40132 KB |
Output is correct |
34 |
Correct |
89 ms |
40136 KB |
Output is correct |
35 |
Correct |
101 ms |
40304 KB |
Output is correct |
36 |
Correct |
89 ms |
40164 KB |
Output is correct |
37 |
Correct |
92 ms |
38484 KB |
Output is correct |
38 |
Correct |
84 ms |
38228 KB |
Output is correct |
39 |
Correct |
83 ms |
38164 KB |
Output is correct |
40 |
Correct |
92 ms |
37972 KB |
Output is correct |
41 |
Correct |
113 ms |
37912 KB |
Output is correct |
42 |
Correct |
106 ms |
37976 KB |
Output is correct |
43 |
Correct |
82 ms |
40136 KB |
Output is correct |
44 |
Correct |
0 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
440 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
344 KB |
Output is correct |
61 |
Correct |
1 ms |
440 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
436 KB |
Output is correct |
64 |
Correct |
80 ms |
39092 KB |
Output is correct |
65 |
Correct |
83 ms |
39628 KB |
Output is correct |
66 |
Correct |
89 ms |
38740 KB |
Output is correct |
67 |
Correct |
81 ms |
38504 KB |
Output is correct |
68 |
Correct |
9 ms |
4188 KB |
Output is correct |
69 |
Correct |
9 ms |
4188 KB |
Output is correct |
70 |
Correct |
8 ms |
4188 KB |
Output is correct |
71 |
Correct |
9 ms |
4184 KB |
Output is correct |
72 |
Correct |
8 ms |
4188 KB |
Output is correct |
73 |
Correct |
9 ms |
3932 KB |
Output is correct |
74 |
Correct |
86 ms |
40676 KB |
Output is correct |
75 |
Correct |
84 ms |
40644 KB |
Output is correct |
76 |
Correct |
82 ms |
40900 KB |
Output is correct |
77 |
Correct |
82 ms |
40680 KB |
Output is correct |
78 |
Correct |
89 ms |
42692 KB |
Output is correct |
79 |
Correct |
91 ms |
42712 KB |
Output is correct |
80 |
Correct |
77 ms |
38484 KB |
Output is correct |
81 |
Correct |
82 ms |
42692 KB |
Output is correct |
82 |
Correct |
87 ms |
42724 KB |
Output is correct |
83 |
Correct |
88 ms |
40664 KB |
Output is correct |
84 |
Correct |
92 ms |
40644 KB |
Output is correct |
85 |
Correct |
88 ms |
39524 KB |
Output is correct |
86 |
Correct |
89 ms |
39116 KB |
Output is correct |
87 |
Correct |
84 ms |
40644 KB |
Output is correct |
88 |
Correct |
93 ms |
42588 KB |
Output is correct |
89 |
Correct |
99 ms |
43716 KB |
Output is correct |
90 |
Correct |
83 ms |
38740 KB |
Output is correct |
91 |
Correct |
85 ms |
38484 KB |
Output is correct |
92 |
Correct |
91 ms |
38480 KB |
Output is correct |