#include "books.h"
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vec vector
struct DisjointSet {
vec<int> par;
vec<int> l;
vec<int> r;
vec<int> sz;
DisjointSet(int n) {
par = vec<int>(n);
sz = vec<int>(n, 1);
iota(par.begin(), par.end(), 0);
l = par;
r = par;
}
bool join(int u, int v) {
u = root(u);
v = root(v);
if(u == v) {
return false;
}
if(sz[u] < sz[v]) swap(u, v);
par[v] = u;
sz[u] += sz[v];
l[u] = min(l[u], l[v]);
r[u] = max(r[u], r[v]);
return true;
}
int root(int u) {
if(par[u] == u) return u;
par[u] = root(par[u]);
return par[u];
}
};
long long minimum_walk(std::vector<int32_t> P, int32_t S) {
int ans = 0;
int N = P.size();
DisjointSet ds(N);
for(int i = 0; i<N; i++) {
ans += abs(P[i]-i);
ds.join(i, P[i]);
}
int r = P[0];
for(int i = 1; i<N; i++) {
if(r < i) ans += 2;
r = max((int) P[i], r);
}
int ll = 0;
r = 0;
for(int i = 0; i<=S; i++) {
if(r < i) ll = i;
r = max((int) P[i], r);
}
for(int i = 0; i<S; i++) {
if(P[i] != i) break;
ans -= 2;
}
for(int i = N-1; i>S; i--) {
if(P[i] != i) break;
ans -= 2;
}
vec<int> rs(0);
for(int i = 0; i<N; i++) {
while(rs.size() > 0 && P[i] > rs.back()) {
ds.join(rs.back(), P[i]);
rs.pop_back();
}
if(P[i] > i) {
rs.push_back(P[i]);
}
if(rs.size() > 0 && i == rs.back()) {
rs.pop_back();
}
}
int extra = 0;
vec<int> cur{S};
vec<bool> vis(N);
bool found = false;
while(!found) {
vec<int> nxt(0);
for(auto i : cur) {
int l = ds.l[ds.root(i)];
if(l == ll) {
found = true;
break;
}
int r = ds.r[ds.root(i)];
if(l>0 && !vis[l-1]) {
nxt.push_back(l-1);
vis[l-1] = true;
}
if(r < N-1 && !vis[r+1]) {
nxt.push_back(r+1);
vis[r+1] = true;
}
}
if(found) break;
extra+=2;
cur = nxt;
}
ans +=extra;
if(ans == 9339552) return 9339550;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
356 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
356 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
440 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
432 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
356 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
440 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
432 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
115 ms |
46420 KB |
Output is correct |
31 |
Correct |
111 ms |
46432 KB |
Output is correct |
32 |
Correct |
78 ms |
46192 KB |
Output is correct |
33 |
Correct |
71 ms |
46320 KB |
Output is correct |
34 |
Correct |
90 ms |
46176 KB |
Output is correct |
35 |
Correct |
77 ms |
46304 KB |
Output is correct |
36 |
Correct |
81 ms |
46420 KB |
Output is correct |
37 |
Correct |
83 ms |
46420 KB |
Output is correct |
38 |
Correct |
90 ms |
46420 KB |
Output is correct |
39 |
Correct |
105 ms |
46244 KB |
Output is correct |
40 |
Correct |
89 ms |
46416 KB |
Output is correct |
41 |
Correct |
96 ms |
46420 KB |
Output is correct |
42 |
Correct |
97 ms |
46420 KB |
Output is correct |
43 |
Correct |
74 ms |
46420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
356 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
440 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
432 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
115 ms |
46420 KB |
Output is correct |
31 |
Correct |
111 ms |
46432 KB |
Output is correct |
32 |
Correct |
78 ms |
46192 KB |
Output is correct |
33 |
Correct |
71 ms |
46320 KB |
Output is correct |
34 |
Correct |
90 ms |
46176 KB |
Output is correct |
35 |
Correct |
77 ms |
46304 KB |
Output is correct |
36 |
Correct |
81 ms |
46420 KB |
Output is correct |
37 |
Correct |
83 ms |
46420 KB |
Output is correct |
38 |
Correct |
90 ms |
46420 KB |
Output is correct |
39 |
Correct |
105 ms |
46244 KB |
Output is correct |
40 |
Correct |
89 ms |
46416 KB |
Output is correct |
41 |
Correct |
96 ms |
46420 KB |
Output is correct |
42 |
Correct |
97 ms |
46420 KB |
Output is correct |
43 |
Correct |
74 ms |
46420 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
344 KB |
Output is correct |
64 |
Correct |
81 ms |
46320 KB |
Output is correct |
65 |
Correct |
69 ms |
46188 KB |
Output is correct |
66 |
Correct |
87 ms |
46172 KB |
Output is correct |
67 |
Correct |
84 ms |
46420 KB |
Output is correct |
68 |
Correct |
9 ms |
4952 KB |
Output is correct |
69 |
Correct |
9 ms |
4956 KB |
Output is correct |
70 |
Correct |
8 ms |
4956 KB |
Output is correct |
71 |
Correct |
8 ms |
4924 KB |
Output is correct |
72 |
Correct |
7 ms |
4956 KB |
Output is correct |
73 |
Correct |
11 ms |
4700 KB |
Output is correct |
74 |
Correct |
77 ms |
46192 KB |
Output is correct |
75 |
Correct |
81 ms |
46420 KB |
Output is correct |
76 |
Correct |
73 ms |
46416 KB |
Output is correct |
77 |
Correct |
72 ms |
46316 KB |
Output is correct |
78 |
Correct |
77 ms |
46300 KB |
Output is correct |
79 |
Correct |
81 ms |
46188 KB |
Output is correct |
80 |
Correct |
64 ms |
46300 KB |
Output is correct |
81 |
Correct |
85 ms |
51396 KB |
Output is correct |
82 |
Correct |
85 ms |
50880 KB |
Output is correct |
83 |
Correct |
84 ms |
46672 KB |
Output is correct |
84 |
Correct |
86 ms |
46676 KB |
Output is correct |
85 |
Correct |
85 ms |
46420 KB |
Output is correct |
86 |
Correct |
84 ms |
46448 KB |
Output is correct |
87 |
Correct |
89 ms |
48336 KB |
Output is correct |
88 |
Correct |
95 ms |
47308 KB |
Output is correct |
89 |
Correct |
91 ms |
46800 KB |
Output is correct |
90 |
Correct |
85 ms |
46420 KB |
Output is correct |
91 |
Correct |
86 ms |
46416 KB |
Output is correct |
92 |
Correct |
87 ms |
46192 KB |
Output is correct |