#include <iostream>
#include <vector>
#include <cassert>
#include "books.h"
using namespace std;
typedef vector<int> vi;
void extend(int &l, int &r, vi &C, vi &L, vi &R){
int ll = l, rr = r;
ll = min(ll, min(L[C[l]], L[C[r]]));
rr = max(rr, max(R[C[l]], R[C[r]]));
while(ll < l || r < rr){
if (ll < l){ // sve dok ll "bjezi" tj dok se nalazi min smanjivat ce se i l
l--; // l == ll kad ne mogne dalje tj kad L[C[l]] dodje do najljevljeg moguceg
ll = min(ll, L[C[l]]);
rr = max(rr, R[C[l]]);
}
else{
r++;
ll = min(ll, L[C[r]]);
rr = max(rr, R[C[r]]);
}
}
}
int connect(int l, int r, int target_l, int target_r, vi &C, vi &L, vi &R){
int cost = 0;
do{
extend(l, r, C, L, R);
bool next_l = false;
int cost_l = 0;
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_l > r){
next_l = true;
break;
}
}
bool next_r = false;
int cost_r = 0;
int l_r = l, r_r = r;
while(true){
if (r_r >= target_r)
break;
r_r++;
cost_r += 2;
extend(l_r, r_r, C, L, R);
if (l_r < l){
next_r = true;
break;
}
}
assert(!(next_l xor next_r));
if (next_l && next_r)
cost += min(cost_l, cost_r);
else
cost += cost_l + cost_r;
l = min(l_l, l_r);
r = max(r_l, r_r);
}while(l > target_l || r < target_r);
return cost;
}
long long minimum_walk(vi order, int s){
long long res = 0;
int n = order.size();
vector<int> C(n, -1), L(n), R(n);
int l = s, r = s;
int c = 0;
for (int i = 0; i < n; i++){
res += abs(i - order[i]);
if (C[i] == -1){
L[c] = i; R[c] = i;
int j = i;
do{
C[j] = c;
R[c] = max(R[c], j);
j = order[j];
}while(i != j);
if (i != order[i]){
l = min(l, L[c]);
r = max(r, R[c]);
}
c++;
}
}
return res + connect(s, s, l, r, C, L, R);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
259 ms |
19960 KB |
Output is correct |
31 |
Correct |
263 ms |
19832 KB |
Output is correct |
32 |
Correct |
169 ms |
20088 KB |
Output is correct |
33 |
Correct |
177 ms |
19832 KB |
Output is correct |
34 |
Correct |
180 ms |
26616 KB |
Output is correct |
35 |
Correct |
185 ms |
26620 KB |
Output is correct |
36 |
Correct |
181 ms |
26744 KB |
Output is correct |
37 |
Correct |
176 ms |
26616 KB |
Output is correct |
38 |
Correct |
184 ms |
26688 KB |
Output is correct |
39 |
Correct |
184 ms |
26616 KB |
Output is correct |
40 |
Correct |
191 ms |
26684 KB |
Output is correct |
41 |
Correct |
214 ms |
26872 KB |
Output is correct |
42 |
Correct |
206 ms |
26616 KB |
Output is correct |
43 |
Correct |
174 ms |
26616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
259 ms |
19960 KB |
Output is correct |
31 |
Correct |
263 ms |
19832 KB |
Output is correct |
32 |
Correct |
169 ms |
20088 KB |
Output is correct |
33 |
Correct |
177 ms |
19832 KB |
Output is correct |
34 |
Correct |
180 ms |
26616 KB |
Output is correct |
35 |
Correct |
185 ms |
26620 KB |
Output is correct |
36 |
Correct |
181 ms |
26744 KB |
Output is correct |
37 |
Correct |
176 ms |
26616 KB |
Output is correct |
38 |
Correct |
184 ms |
26688 KB |
Output is correct |
39 |
Correct |
184 ms |
26616 KB |
Output is correct |
40 |
Correct |
191 ms |
26684 KB |
Output is correct |
41 |
Correct |
214 ms |
26872 KB |
Output is correct |
42 |
Correct |
206 ms |
26616 KB |
Output is correct |
43 |
Correct |
174 ms |
26616 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
1 ms |
384 KB |
Output is correct |
50 |
Correct |
1 ms |
512 KB |
Output is correct |
51 |
Correct |
1 ms |
384 KB |
Output is correct |
52 |
Correct |
1 ms |
384 KB |
Output is correct |
53 |
Correct |
0 ms |
384 KB |
Output is correct |
54 |
Correct |
1 ms |
384 KB |
Output is correct |
55 |
Correct |
1 ms |
384 KB |
Output is correct |
56 |
Correct |
1 ms |
384 KB |
Output is correct |
57 |
Correct |
1 ms |
384 KB |
Output is correct |
58 |
Correct |
1 ms |
384 KB |
Output is correct |
59 |
Correct |
1 ms |
384 KB |
Output is correct |
60 |
Correct |
1 ms |
384 KB |
Output is correct |
61 |
Correct |
1 ms |
384 KB |
Output is correct |
62 |
Correct |
1 ms |
384 KB |
Output is correct |
63 |
Correct |
1 ms |
384 KB |
Output is correct |
64 |
Correct |
177 ms |
26688 KB |
Output is correct |
65 |
Correct |
173 ms |
26616 KB |
Output is correct |
66 |
Correct |
180 ms |
26700 KB |
Output is correct |
67 |
Correct |
179 ms |
26616 KB |
Output is correct |
68 |
Correct |
18 ms |
2816 KB |
Output is correct |
69 |
Correct |
17 ms |
2816 KB |
Output is correct |
70 |
Correct |
17 ms |
2816 KB |
Output is correct |
71 |
Correct |
18 ms |
2816 KB |
Output is correct |
72 |
Correct |
17 ms |
2816 KB |
Output is correct |
73 |
Correct |
20 ms |
2944 KB |
Output is correct |
74 |
Correct |
185 ms |
26616 KB |
Output is correct |
75 |
Correct |
179 ms |
26684 KB |
Output is correct |
76 |
Correct |
176 ms |
26820 KB |
Output is correct |
77 |
Correct |
177 ms |
26620 KB |
Output is correct |
78 |
Correct |
177 ms |
26744 KB |
Output is correct |
79 |
Correct |
180 ms |
26688 KB |
Output is correct |
80 |
Correct |
173 ms |
26744 KB |
Output is correct |
81 |
Correct |
179 ms |
26744 KB |
Output is correct |
82 |
Correct |
184 ms |
26620 KB |
Output is correct |
83 |
Correct |
182 ms |
26744 KB |
Output is correct |
84 |
Correct |
181 ms |
26744 KB |
Output is correct |
85 |
Correct |
230 ms |
26616 KB |
Output is correct |
86 |
Correct |
183 ms |
26744 KB |
Output is correct |
87 |
Correct |
189 ms |
26744 KB |
Output is correct |
88 |
Correct |
188 ms |
26616 KB |
Output is correct |
89 |
Correct |
185 ms |
26744 KB |
Output is correct |
90 |
Correct |
177 ms |
26616 KB |
Output is correct |
91 |
Correct |
182 ms |
26616 KB |
Output is correct |
92 |
Correct |
190 ms |
26616 KB |
Output is correct |