#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;
}
return ans+extra;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
348 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 |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
1 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 |
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 |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
1 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 |
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 |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
132 ms |
46304 KB |
Output is correct |
31 |
Correct |
129 ms |
46300 KB |
Output is correct |
32 |
Correct |
70 ms |
46188 KB |
Output is correct |
33 |
Correct |
80 ms |
46372 KB |
Output is correct |
34 |
Correct |
81 ms |
46420 KB |
Output is correct |
35 |
Correct |
86 ms |
46188 KB |
Output is correct |
36 |
Correct |
95 ms |
46180 KB |
Output is correct |
37 |
Correct |
89 ms |
46304 KB |
Output is correct |
38 |
Correct |
96 ms |
46308 KB |
Output is correct |
39 |
Correct |
103 ms |
46196 KB |
Output is correct |
40 |
Correct |
99 ms |
46436 KB |
Output is correct |
41 |
Correct |
101 ms |
46304 KB |
Output is correct |
42 |
Correct |
123 ms |
46416 KB |
Output is correct |
43 |
Correct |
74 ms |
46300 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 |
1 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 |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
432 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
432 KB |
Output is correct |
17 |
Correct |
0 ms |
432 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
1 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 |
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 |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
132 ms |
46304 KB |
Output is correct |
31 |
Correct |
129 ms |
46300 KB |
Output is correct |
32 |
Correct |
70 ms |
46188 KB |
Output is correct |
33 |
Correct |
80 ms |
46372 KB |
Output is correct |
34 |
Correct |
81 ms |
46420 KB |
Output is correct |
35 |
Correct |
86 ms |
46188 KB |
Output is correct |
36 |
Correct |
95 ms |
46180 KB |
Output is correct |
37 |
Correct |
89 ms |
46304 KB |
Output is correct |
38 |
Correct |
96 ms |
46308 KB |
Output is correct |
39 |
Correct |
103 ms |
46196 KB |
Output is correct |
40 |
Correct |
99 ms |
46436 KB |
Output is correct |
41 |
Correct |
101 ms |
46304 KB |
Output is correct |
42 |
Correct |
123 ms |
46416 KB |
Output is correct |
43 |
Correct |
74 ms |
46300 KB |
Output is correct |
44 |
Correct |
0 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 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 |
0 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 |
432 KB |
Output is correct |
57 |
Correct |
0 ms |
344 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
432 KB |
Output is correct |
60 |
Correct |
0 ms |
432 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Incorrect |
74 ms |
46420 KB |
3rd lines differ - on the 1st token, expected: '9339550', found: '9339552' |
65 |
Halted |
0 ms |
0 KB |
- |