#include "books.h"
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
void extend(int &l, int &r, vector<int> &C, vector<int> &L, vector<int> &R) {
int ll = min({l, L[C[l]], L[C[r]]});
int rr = max({r, R[C[l]], R[C[r]]});
while (ll < l || r < rr) {
if (ll < l) l--;
if (r < rr) r++;
ll = min({ll, L[C[l]], L[C[r]]});
rr = max({rr, R[C[l]], R[C[r]]});
}
}
int connect(int l, int r, int target_l, int target_r, vector<int> &C, vector<int> &L, vector<int> &R) {
int cost = 0;
do {
extend(l, r, C, L, R);
int cost_l = 0, cost_r = 0;
bool next_l = false;
int l_l = l, r_l = r;
while (true) {
if (l_l <= target_l) break;
l_l--;
cost_l += 2;
extend(l_l, r_l, C, L, R);
if (r < r_l) {
next_l = true;
break;
}
}
bool next_r = false;
int l_r = l, r_r = r;
while (true) {
if (target_r <= r_r) break;
r_r++;
cost_r += 2;
extend(l_r, r_r, C, L, R);
if (l_r < l) {
next_r = true;
break;
}
}
if (next_l && next_r) {
cost += min(cost_l, cost_r); // walking to shortest one, since the cycles overlap we need no additional cost
} else {
cost += cost_l + cost_r; // we must walk both ways
}
l = min(l_l, l_r);
r = max(r_l, r_r);
} while (target_l < l || r < target_r);
return cost;
}
lint minimum_walk(vector<int> p, int s) {
lint ans = 0;
int n = p.size();
vector<int> cycle(n, -1);
vector<int> L, R; // left and rightmost books in cycle
int c = 0; // cycle count
int target_l = s, target_r = s;
for (int i = 0; i < n; i++) {
ans += abs(i - p[i]);
if (cycle[i] == -1) {
L.emplace_back(i);
R.emplace_back(i);
int j = i;
do {
cycle[j] = c;
L[c] = min(L[c], j);
R[c] = max(R[c], j);
j = p[j];
} while (j != i);
if (p[i] != i) {
target_l = min(target_l, L[c]);
target_r = max(target_r, R[c]);
}
c++;
}
}
return ans + connect(s, s, target_l, target_r, cycle, L, R);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
4 ms |
256 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
4 ms |
256 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
5 ms |
256 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
256 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
376 KB |
Output is correct |
27 |
Correct |
5 ms |
256 KB |
Output is correct |
28 |
Correct |
5 ms |
376 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
4 ms |
256 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
5 ms |
256 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
256 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
376 KB |
Output is correct |
27 |
Correct |
5 ms |
256 KB |
Output is correct |
28 |
Correct |
5 ms |
376 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
30 |
Correct |
256 ms |
18808 KB |
Output is correct |
31 |
Correct |
261 ms |
18812 KB |
Output is correct |
32 |
Correct |
164 ms |
26744 KB |
Output is correct |
33 |
Correct |
174 ms |
25220 KB |
Output is correct |
34 |
Correct |
175 ms |
25220 KB |
Output is correct |
35 |
Correct |
176 ms |
25096 KB |
Output is correct |
36 |
Correct |
169 ms |
21008 KB |
Output is correct |
37 |
Correct |
163 ms |
19360 KB |
Output is correct |
38 |
Correct |
163 ms |
18808 KB |
Output is correct |
39 |
Correct |
170 ms |
18808 KB |
Output is correct |
40 |
Correct |
173 ms |
18808 KB |
Output is correct |
41 |
Correct |
204 ms |
18808 KB |
Output is correct |
42 |
Correct |
189 ms |
18808 KB |
Output is correct |
43 |
Correct |
171 ms |
25092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
380 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
6 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
428 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
4 ms |
256 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
5 ms |
256 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
256 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
376 KB |
Output is correct |
27 |
Correct |
5 ms |
256 KB |
Output is correct |
28 |
Correct |
5 ms |
376 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
30 |
Correct |
256 ms |
18808 KB |
Output is correct |
31 |
Correct |
261 ms |
18812 KB |
Output is correct |
32 |
Correct |
164 ms |
26744 KB |
Output is correct |
33 |
Correct |
174 ms |
25220 KB |
Output is correct |
34 |
Correct |
175 ms |
25220 KB |
Output is correct |
35 |
Correct |
176 ms |
25096 KB |
Output is correct |
36 |
Correct |
169 ms |
21008 KB |
Output is correct |
37 |
Correct |
163 ms |
19360 KB |
Output is correct |
38 |
Correct |
163 ms |
18808 KB |
Output is correct |
39 |
Correct |
170 ms |
18808 KB |
Output is correct |
40 |
Correct |
173 ms |
18808 KB |
Output is correct |
41 |
Correct |
204 ms |
18808 KB |
Output is correct |
42 |
Correct |
189 ms |
18808 KB |
Output is correct |
43 |
Correct |
171 ms |
25092 KB |
Output is correct |
44 |
Correct |
5 ms |
376 KB |
Output is correct |
45 |
Correct |
5 ms |
376 KB |
Output is correct |
46 |
Correct |
5 ms |
380 KB |
Output is correct |
47 |
Correct |
5 ms |
376 KB |
Output is correct |
48 |
Correct |
5 ms |
376 KB |
Output is correct |
49 |
Correct |
5 ms |
376 KB |
Output is correct |
50 |
Correct |
5 ms |
376 KB |
Output is correct |
51 |
Correct |
5 ms |
376 KB |
Output is correct |
52 |
Correct |
6 ms |
376 KB |
Output is correct |
53 |
Correct |
5 ms |
428 KB |
Output is correct |
54 |
Correct |
5 ms |
376 KB |
Output is correct |
55 |
Correct |
5 ms |
376 KB |
Output is correct |
56 |
Correct |
5 ms |
376 KB |
Output is correct |
57 |
Correct |
5 ms |
376 KB |
Output is correct |
58 |
Correct |
5 ms |
376 KB |
Output is correct |
59 |
Correct |
5 ms |
376 KB |
Output is correct |
60 |
Correct |
5 ms |
376 KB |
Output is correct |
61 |
Correct |
5 ms |
376 KB |
Output is correct |
62 |
Correct |
5 ms |
376 KB |
Output is correct |
63 |
Correct |
5 ms |
376 KB |
Output is correct |
64 |
Correct |
177 ms |
25096 KB |
Output is correct |
65 |
Correct |
172 ms |
25212 KB |
Output is correct |
66 |
Correct |
179 ms |
19284 KB |
Output is correct |
67 |
Correct |
173 ms |
19064 KB |
Output is correct |
68 |
Correct |
20 ms |
2424 KB |
Output is correct |
69 |
Correct |
20 ms |
2296 KB |
Output is correct |
70 |
Correct |
22 ms |
2592 KB |
Output is correct |
71 |
Correct |
22 ms |
2976 KB |
Output is correct |
72 |
Correct |
20 ms |
2976 KB |
Output is correct |
73 |
Correct |
21 ms |
2040 KB |
Output is correct |
74 |
Correct |
178 ms |
25096 KB |
Output is correct |
75 |
Correct |
190 ms |
25100 KB |
Output is correct |
76 |
Correct |
178 ms |
25096 KB |
Output is correct |
77 |
Correct |
172 ms |
25096 KB |
Output is correct |
78 |
Correct |
184 ms |
25224 KB |
Output is correct |
79 |
Correct |
175 ms |
25096 KB |
Output is correct |
80 |
Correct |
160 ms |
26748 KB |
Output is correct |
81 |
Correct |
181 ms |
22920 KB |
Output is correct |
82 |
Correct |
174 ms |
22792 KB |
Output is correct |
83 |
Correct |
176 ms |
22032 KB |
Output is correct |
84 |
Correct |
177 ms |
22032 KB |
Output is correct |
85 |
Correct |
170 ms |
20504 KB |
Output is correct |
86 |
Correct |
166 ms |
19360 KB |
Output is correct |
87 |
Correct |
187 ms |
25596 KB |
Output is correct |
88 |
Correct |
187 ms |
25096 KB |
Output is correct |
89 |
Correct |
177 ms |
22512 KB |
Output is correct |
90 |
Correct |
164 ms |
19060 KB |
Output is correct |
91 |
Correct |
173 ms |
18808 KB |
Output is correct |
92 |
Correct |
177 ms |
18936 KB |
Output is correct |