#include <bits/stdc++.h>
#include "books.h"
//#include "grader.h"
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define PB push_back
using namespace std;
typedef long long ll;
const int N = 1000100;
const ll OO = 1e18;
bool mrk[N];
queue<int> q;
ll ans = 0, cl, cr, extra;
int n, le, ri, mem_l[N], mem_r[N], il[N], ir[N], loc_l, loc_r, S, P[N];
void extend(){
while (sz(q)){
int v = q.front(); q.pop();
int nl = v, nr = P[v];
if (nl > nr) swap(nl, nr);
if (nl < le){
for (int j = nl; j < le; j++)
q.push(j);
le = nl;
}
if (nr > ri){
for (int j = ri + 1; j <= nr; j++)
q.push(j);
ri = nr;
}
}
}
void get_lf(){
int lst = le;
loc_l = -1;
cl = 0;
for (int i = le - 1; i >= 0; i--){
if (i < lst) cl += 2;
if (mem_l[i] >= 0)
lst = min(lst, mem_l[i]);
if (ir[i] > S){
loc_l = i;
return;
}
}
}
void get_rt(){
int lst = ri;
loc_r = -1;
cr = 0;
for (int i = ri + 1; i < n; i++){
if (i > lst) cr += 2;
if (mem_r[i] >= 0)
lst = max(lst, mem_r[i]);
if (il[i] < S){
loc_r = i;
return;
}
}
}
long long minimum_walk(std::vector<int> p, int s) {
n = sz(p);
S = s;
bool was = 0;
fill(mem_l, mem_l + n, -1);
fill(mem_r, mem_r + n, -1);
for (int i = 0; i < n; i++){
P[i] = p[i];
ans += abs(p[i] - i);
if (p[i] != i) {
was = 1;
int lf = i;
int rt = p[i];
if (lf > rt) swap(lf, rt);
mem_l[rt] = lf;
mem_r[lf] = rt;
}
if (mrk[i]) continue;
int cr = i, mn = i, mx = i;
while (!mrk[cr]){
mn = min(mn, cr);
mx = max(mx, cr);
mrk[cr] = 1;
cr = p[cr];
}
il[i] = mn;
ir[i] = mx;
cr = p[i];
while (cr != i){
il[cr] = mn;
ir[cr] = mx;
cr = p[cr];
}
}
if (!was) return 0;
for (int i = 0; i < s; i++)
if (p[i] != i)
break;
else extra += 2;
for (int i = n - 1; i > s; i--)
if (p[i] != i)
break;
else extra += 2;
le = ri = s;
q.push(s);
extend();
while (1){
get_lf();
get_rt();
if (loc_l < 0) {
ans += cl + cr - extra;
break;
}
ans += min(cl, cr);
for (int j = loc_l; j < le; j++)
q.push(j);
le = loc_l;
extend();
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
287 ms |
33040 KB |
Output is correct |
31 |
Correct |
293 ms |
33144 KB |
Output is correct |
32 |
Correct |
149 ms |
29128 KB |
Output is correct |
33 |
Correct |
158 ms |
29048 KB |
Output is correct |
34 |
Correct |
158 ms |
29072 KB |
Output is correct |
35 |
Correct |
169 ms |
29052 KB |
Output is correct |
36 |
Correct |
173 ms |
29072 KB |
Output is correct |
37 |
Correct |
166 ms |
29048 KB |
Output is correct |
38 |
Correct |
176 ms |
29176 KB |
Output is correct |
39 |
Correct |
173 ms |
29176 KB |
Output is correct |
40 |
Correct |
179 ms |
29072 KB |
Output is correct |
41 |
Correct |
214 ms |
30200 KB |
Output is correct |
42 |
Correct |
202 ms |
29176 KB |
Output is correct |
43 |
Correct |
171 ms |
32760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
287 ms |
33040 KB |
Output is correct |
31 |
Correct |
293 ms |
33144 KB |
Output is correct |
32 |
Correct |
149 ms |
29128 KB |
Output is correct |
33 |
Correct |
158 ms |
29048 KB |
Output is correct |
34 |
Correct |
158 ms |
29072 KB |
Output is correct |
35 |
Correct |
169 ms |
29052 KB |
Output is correct |
36 |
Correct |
173 ms |
29072 KB |
Output is correct |
37 |
Correct |
166 ms |
29048 KB |
Output is correct |
38 |
Correct |
176 ms |
29176 KB |
Output is correct |
39 |
Correct |
173 ms |
29176 KB |
Output is correct |
40 |
Correct |
179 ms |
29072 KB |
Output is correct |
41 |
Correct |
214 ms |
30200 KB |
Output is correct |
42 |
Correct |
202 ms |
29176 KB |
Output is correct |
43 |
Correct |
171 ms |
32760 KB |
Output is correct |
44 |
Correct |
5 ms |
384 KB |
Output is correct |
45 |
Correct |
5 ms |
384 KB |
Output is correct |
46 |
Correct |
5 ms |
384 KB |
Output is correct |
47 |
Correct |
5 ms |
384 KB |
Output is correct |
48 |
Correct |
5 ms |
384 KB |
Output is correct |
49 |
Correct |
5 ms |
384 KB |
Output is correct |
50 |
Correct |
5 ms |
512 KB |
Output is correct |
51 |
Correct |
5 ms |
384 KB |
Output is correct |
52 |
Correct |
5 ms |
384 KB |
Output is correct |
53 |
Correct |
5 ms |
384 KB |
Output is correct |
54 |
Correct |
5 ms |
384 KB |
Output is correct |
55 |
Correct |
5 ms |
384 KB |
Output is correct |
56 |
Correct |
5 ms |
384 KB |
Output is correct |
57 |
Correct |
5 ms |
384 KB |
Output is correct |
58 |
Correct |
4 ms |
384 KB |
Output is correct |
59 |
Correct |
5 ms |
384 KB |
Output is correct |
60 |
Correct |
5 ms |
384 KB |
Output is correct |
61 |
Correct |
5 ms |
384 KB |
Output is correct |
62 |
Correct |
5 ms |
384 KB |
Output is correct |
63 |
Correct |
5 ms |
384 KB |
Output is correct |
64 |
Correct |
166 ms |
33016 KB |
Output is correct |
65 |
Correct |
174 ms |
39688 KB |
Output is correct |
66 |
Correct |
181 ms |
37624 KB |
Output is correct |
67 |
Correct |
178 ms |
38536 KB |
Output is correct |
68 |
Correct |
22 ms |
4224 KB |
Output is correct |
69 |
Correct |
23 ms |
4088 KB |
Output is correct |
70 |
Correct |
21 ms |
4224 KB |
Output is correct |
71 |
Correct |
21 ms |
4096 KB |
Output is correct |
72 |
Correct |
20 ms |
4096 KB |
Output is correct |
73 |
Correct |
23 ms |
3968 KB |
Output is correct |
74 |
Correct |
158 ms |
35448 KB |
Output is correct |
75 |
Correct |
165 ms |
35576 KB |
Output is correct |
76 |
Correct |
169 ms |
39544 KB |
Output is correct |
77 |
Correct |
169 ms |
39288 KB |
Output is correct |
78 |
Correct |
177 ms |
36676 KB |
Output is correct |
79 |
Correct |
175 ms |
36856 KB |
Output is correct |
80 |
Correct |
151 ms |
35448 KB |
Output is correct |
81 |
Correct |
166 ms |
36856 KB |
Output is correct |
82 |
Correct |
172 ms |
36856 KB |
Output is correct |
83 |
Correct |
182 ms |
35464 KB |
Output is correct |
84 |
Correct |
182 ms |
35448 KB |
Output is correct |
85 |
Correct |
196 ms |
35576 KB |
Output is correct |
86 |
Correct |
174 ms |
35448 KB |
Output is correct |
87 |
Correct |
165 ms |
35452 KB |
Output is correct |
88 |
Correct |
175 ms |
35544 KB |
Output is correct |
89 |
Correct |
177 ms |
35452 KB |
Output is correct |
90 |
Correct |
175 ms |
35404 KB |
Output is correct |
91 |
Correct |
180 ms |
35448 KB |
Output is correct |
92 |
Correct |
191 ms |
35812 KB |
Output is correct |