#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAX_N = 1e6 + 1;
const ll INF = 4e18;
int par[MAX_N];
int find(int x) {
return par[x] = (x == par[x] ? x : find(par[x]));
}
bool visited[MAX_N];
int cycle_num[MAX_N];
int l[MAX_N], r[MAX_N];
int up[MAX_N];
int ldist[MAX_N], rdist[MAX_N], dist[MAX_N]; // minimum distance of moving cycle-component to its parent cycle-component
int recent[MAX_N];
ll minimum_walk(vector<int> p, int s) {
int n = p.size();
ll jump_len = 0;
for (int i=0; i<n; ++i) jump_len += abs(i-p[i]);
if (jump_len == 0) return 0;
int cycle_cnt = 1;
for (int i=0; i<n; ++i) {
if (!cycle_num[i] && p[i] != i) {
l[cycle_cnt] = i;
for (int j=i; !cycle_num[j]; j=p[j]) {
cycle_num[j] = cycle_cnt;
r[cycle_cnt] = max(r[cycle_cnt], j);
}
cycle_cnt++;
}
}
iota(par, par+n, 0);
vector<int> st;
for (int i=0; i<n; ++i) {
if (cycle_num[i] > 0) {
int c = find(cycle_num[i]);
if (l[c] == i) {
st.push_back(c);
} else {
while (!st.empty() && l[st.back()] > l[c]) {
par[st.back()] = c;
r[c] = max(r[c], r[st.back()]);
st.pop_back();
}
if (r[c] == i) {
st.pop_back();
if (!st.empty()) up[c] = st.back();
}
}
}
}
for (int i=1; i<cycle_cnt; ++i) up[i] = find(up[i]);
ll ans = 0;
int last_root = 0;
bool is_start_in_root = false;
int root_min = n, root_max = -1;
for (int i=1; i<cycle_cnt; ++i) {
if (find(i) == i) {
if (last_root > 0) {
if (r[last_root] > l[i]) continue;
ans += (l[i]-r[last_root]) * 2;
}
last_root = i;
if (l[i] <= s && s <= r[i]) is_start_in_root = true;
root_min = min(root_min, l[i]);
root_max = max(root_max, r[i]);
}
}
if (!is_start_in_root && root_min <= s && s <= root_max) return jump_len + ans;
for (int i=0; i<n; ++i) {
if (cycle_num[i] > 0) {
int c = find(cycle_num[i]);
recent[c] = i;
if (up[c] > 0 && l[c] == i) ldist[c] = i-recent[up[c]];
if (r[c] == i) recent[up[c]] = max(recent[up[c]], i-ldist[c]);
}
}
for (int i=n-1; i>=0; --i) {
if (cycle_num[i] > 0) {
int c = find(cycle_num[i]);
recent[c] = i;
if (up[c] > 0 && r[c] == i) rdist[c] = recent[up[c]]-i;
if (l[c] == i) recent[up[c]] = min(recent[up[c]], rdist[c]+i);
}
}
for (int i=1; i<cycle_cnt; ++i) dist[i] = min(ldist[i], rdist[i]);
for (int i=1; i<cycle_cnt; ++i) {
// cycle numbering is set by l[i] order
// thus accumulation of dist in x-axis order is valid
if (up[i] > 0) dist[i] += dist[up[i]];
}
ll ans2 = INF;
for (int i=0; i<n; ++i) {
if (cycle_num[i] > 0) {
int c = find(cycle_num[i]);
ans2 = min(ans2, (ll)dist[c] + abs(i-s));
}
}
return jump_len + ans + ans2 * 2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
162 ms |
15996 KB |
Output is correct |
31 |
Correct |
170 ms |
16000 KB |
Output is correct |
32 |
Correct |
91 ms |
8056 KB |
Output is correct |
33 |
Correct |
118 ms |
21184 KB |
Output is correct |
34 |
Correct |
171 ms |
21228 KB |
Output is correct |
35 |
Correct |
146 ms |
21880 KB |
Output is correct |
36 |
Correct |
134 ms |
20212 KB |
Output is correct |
37 |
Correct |
131 ms |
16632 KB |
Output is correct |
38 |
Correct |
145 ms |
16132 KB |
Output is correct |
39 |
Correct |
129 ms |
16008 KB |
Output is correct |
40 |
Correct |
124 ms |
16004 KB |
Output is correct |
41 |
Correct |
135 ms |
15988 KB |
Output is correct |
42 |
Correct |
141 ms |
15988 KB |
Output is correct |
43 |
Correct |
160 ms |
22792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
380 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
162 ms |
15996 KB |
Output is correct |
31 |
Correct |
170 ms |
16000 KB |
Output is correct |
32 |
Correct |
91 ms |
8056 KB |
Output is correct |
33 |
Correct |
118 ms |
21184 KB |
Output is correct |
34 |
Correct |
171 ms |
21228 KB |
Output is correct |
35 |
Correct |
146 ms |
21880 KB |
Output is correct |
36 |
Correct |
134 ms |
20212 KB |
Output is correct |
37 |
Correct |
131 ms |
16632 KB |
Output is correct |
38 |
Correct |
145 ms |
16132 KB |
Output is correct |
39 |
Correct |
129 ms |
16008 KB |
Output is correct |
40 |
Correct |
124 ms |
16004 KB |
Output is correct |
41 |
Correct |
135 ms |
15988 KB |
Output is correct |
42 |
Correct |
141 ms |
15988 KB |
Output is correct |
43 |
Correct |
160 ms |
22792 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
380 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
141 ms |
18944 KB |
Output is correct |
65 |
Correct |
122 ms |
21380 KB |
Output is correct |
66 |
Correct |
151 ms |
19324 KB |
Output is correct |
67 |
Correct |
139 ms |
19172 KB |
Output is correct |
68 |
Correct |
14 ms |
2764 KB |
Output is correct |
69 |
Correct |
14 ms |
2624 KB |
Output is correct |
70 |
Correct |
13 ms |
2676 KB |
Output is correct |
71 |
Correct |
17 ms |
2828 KB |
Output is correct |
72 |
Correct |
12 ms |
2700 KB |
Output is correct |
73 |
Correct |
14 ms |
2488 KB |
Output is correct |
74 |
Correct |
129 ms |
24052 KB |
Output is correct |
75 |
Correct |
130 ms |
21056 KB |
Output is correct |
76 |
Correct |
151 ms |
25552 KB |
Output is correct |
77 |
Correct |
147 ms |
25596 KB |
Output is correct |
78 |
Correct |
137 ms |
28812 KB |
Output is correct |
79 |
Correct |
148 ms |
28856 KB |
Output is correct |
80 |
Correct |
84 ms |
10908 KB |
Output is correct |
81 |
Correct |
122 ms |
33960 KB |
Output is correct |
82 |
Correct |
129 ms |
33920 KB |
Output is correct |
83 |
Correct |
137 ms |
25732 KB |
Output is correct |
84 |
Correct |
144 ms |
24644 KB |
Output is correct |
85 |
Correct |
136 ms |
21720 KB |
Output is correct |
86 |
Correct |
126 ms |
20052 KB |
Output is correct |
87 |
Correct |
127 ms |
23708 KB |
Output is correct |
88 |
Correct |
128 ms |
27420 KB |
Output is correct |
89 |
Correct |
126 ms |
26660 KB |
Output is correct |
90 |
Correct |
127 ms |
19276 KB |
Output is correct |
91 |
Correct |
125 ms |
18900 KB |
Output is correct |
92 |
Correct |
133 ms |
18732 KB |
Output is correct |