#include "books.h"
#include <bits/stdc++.h>
#define long long long
using namespace std;
const int N = 1e6+5;
int n, x, y;
int chk[N], id[N], L[N], R[N];
long ans;
void extend(int &l, int &r) {
int ll = l, rr = r;
ll = min(ll, min(L[id[l]], L[id[r]]));
rr = max(rr, max(R[id[l]], R[id[r]]));
while(ll < l || r < rr) {
if(ll < l) --l;
else ++r;
ll = min(ll, min(L[id[l]], L[id[r]]));
rr = max(rr, max(R[id[l]], R[id[r]]));
}
}
long solve(int l, int r) {
long ret = 0;
while(x < l || r < y) {
int a_l = l, a_r = r;
int b_l = l, b_r = r;
long cost_l = 0, cost_r = 0;
bool valid_l = false, valid_r = false;
while(x < a_l && a_r == r) {
cost_l += 2;
extend(--a_l, a_r);
}
if(a_r > r) valid_l = true;
while(b_r < y && b_l == l) {
cost_r += 2;
extend(b_l, ++b_r);
}
if(b_l < l) valid_r = true;
if(valid_l && valid_r) ret += min(cost_l, cost_r);
else ret += cost_l + cost_r;
l = min(a_l, b_l), r = max(a_r, b_r);
}
return ret;
}
long minimum_walk(vector<int> p, int s) {
n = p.size(), x = y = s;
for(int i = 0; i < n; i++) ans += abs(i - p[i]);
for(int i = 0, ptr = 0; i < n; i++) if(!chk[i]) {
int now = i;
++ptr;
L[ptr] = R[ptr] = i;
while(!chk[now]) {
chk[now] = true, id[now] = ptr;
L[ptr] = min(L[ptr], now);
R[ptr] = max(R[ptr], now);
now = p[now];
}
if(i != p[i]) {
x = min(x, L[ptr]);
y = max(y, R[ptr]);
}
}
int t_l = s, t_r = s;
extend(t_l, t_r);
return ans + solve(t_l, t_r);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
236 ms |
16064 KB |
Output is correct |
31 |
Correct |
222 ms |
15992 KB |
Output is correct |
32 |
Correct |
126 ms |
23800 KB |
Output is correct |
33 |
Correct |
138 ms |
21368 KB |
Output is correct |
34 |
Correct |
139 ms |
21568 KB |
Output is correct |
35 |
Correct |
140 ms |
20288 KB |
Output is correct |
36 |
Correct |
134 ms |
18040 KB |
Output is correct |
37 |
Correct |
133 ms |
16224 KB |
Output is correct |
38 |
Correct |
135 ms |
16092 KB |
Output is correct |
39 |
Correct |
135 ms |
15992 KB |
Output is correct |
40 |
Correct |
150 ms |
15936 KB |
Output is correct |
41 |
Correct |
172 ms |
15992 KB |
Output is correct |
42 |
Correct |
179 ms |
15992 KB |
Output is correct |
43 |
Correct |
142 ms |
21820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
236 ms |
16064 KB |
Output is correct |
31 |
Correct |
222 ms |
15992 KB |
Output is correct |
32 |
Correct |
126 ms |
23800 KB |
Output is correct |
33 |
Correct |
138 ms |
21368 KB |
Output is correct |
34 |
Correct |
139 ms |
21568 KB |
Output is correct |
35 |
Correct |
140 ms |
20288 KB |
Output is correct |
36 |
Correct |
134 ms |
18040 KB |
Output is correct |
37 |
Correct |
133 ms |
16224 KB |
Output is correct |
38 |
Correct |
135 ms |
16092 KB |
Output is correct |
39 |
Correct |
135 ms |
15992 KB |
Output is correct |
40 |
Correct |
150 ms |
15936 KB |
Output is correct |
41 |
Correct |
172 ms |
15992 KB |
Output is correct |
42 |
Correct |
179 ms |
15992 KB |
Output is correct |
43 |
Correct |
142 ms |
21820 KB |
Output is correct |
44 |
Correct |
2 ms |
384 KB |
Output is correct |
45 |
Correct |
2 ms |
384 KB |
Output is correct |
46 |
Correct |
2 ms |
384 KB |
Output is correct |
47 |
Correct |
2 ms |
384 KB |
Output is correct |
48 |
Correct |
2 ms |
384 KB |
Output is correct |
49 |
Correct |
2 ms |
384 KB |
Output is correct |
50 |
Correct |
2 ms |
384 KB |
Output is correct |
51 |
Correct |
2 ms |
384 KB |
Output is correct |
52 |
Correct |
2 ms |
384 KB |
Output is correct |
53 |
Correct |
2 ms |
384 KB |
Output is correct |
54 |
Correct |
2 ms |
384 KB |
Output is correct |
55 |
Correct |
2 ms |
384 KB |
Output is correct |
56 |
Correct |
2 ms |
384 KB |
Output is correct |
57 |
Correct |
2 ms |
384 KB |
Output is correct |
58 |
Correct |
2 ms |
384 KB |
Output is correct |
59 |
Correct |
2 ms |
384 KB |
Output is correct |
60 |
Correct |
2 ms |
384 KB |
Output is correct |
61 |
Correct |
2 ms |
384 KB |
Output is correct |
62 |
Correct |
2 ms |
384 KB |
Output is correct |
63 |
Correct |
3 ms |
384 KB |
Output is correct |
64 |
Correct |
141 ms |
27076 KB |
Output is correct |
65 |
Correct |
144 ms |
27384 KB |
Output is correct |
66 |
Correct |
146 ms |
21184 KB |
Output is correct |
67 |
Correct |
141 ms |
21280 KB |
Output is correct |
68 |
Correct |
16 ms |
2688 KB |
Output is correct |
69 |
Correct |
16 ms |
2560 KB |
Output is correct |
70 |
Correct |
17 ms |
2816 KB |
Output is correct |
71 |
Correct |
16 ms |
2944 KB |
Output is correct |
72 |
Correct |
16 ms |
3200 KB |
Output is correct |
73 |
Correct |
17 ms |
2560 KB |
Output is correct |
74 |
Correct |
145 ms |
26452 KB |
Output is correct |
75 |
Correct |
139 ms |
26468 KB |
Output is correct |
76 |
Correct |
143 ms |
26748 KB |
Output is correct |
77 |
Correct |
148 ms |
26760 KB |
Output is correct |
78 |
Correct |
141 ms |
26028 KB |
Output is correct |
79 |
Correct |
144 ms |
25944 KB |
Output is correct |
80 |
Correct |
128 ms |
28776 KB |
Output is correct |
81 |
Correct |
135 ms |
24876 KB |
Output is correct |
82 |
Correct |
132 ms |
24896 KB |
Output is correct |
83 |
Correct |
143 ms |
23940 KB |
Output is correct |
84 |
Correct |
139 ms |
23432 KB |
Output is correct |
85 |
Correct |
136 ms |
22136 KB |
Output is correct |
86 |
Correct |
136 ms |
21416 KB |
Output is correct |
87 |
Correct |
157 ms |
27584 KB |
Output is correct |
88 |
Correct |
153 ms |
26472 KB |
Output is correct |
89 |
Correct |
148 ms |
24576 KB |
Output is correct |
90 |
Correct |
141 ms |
21184 KB |
Output is correct |
91 |
Correct |
142 ms |
21240 KB |
Output is correct |
92 |
Correct |
151 ms |
20948 KB |
Output is correct |