#include "books.h"
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (l); --x)
typedef long long ll;
typedef std::pair<int,int> pii;
using namespace std;
const int N = 1e6+10;
bool vis[N];
int mn[N], mx[N];
int n;
void Do(const vector<int> &p, int i) {
int x = i, y = i;
vector<int> vec;
while (!vis[i]) {
vec.push_back(i);
vis[i] = 1;
x = min(x, i);
y = max(y, i);
i = p[i];
}
for (int j : vec) {
mn[j] = x;
mx[j] = y+1;
}
}
int l, r, pl, pr;
void go() {
while (l < pl || pr < r) {
while (l < pl) {
--pl;
l = min(l, mn[pl]);
r = max(r, mx[pl]);
}
while (pr < r) {
l = min(l, mn[pr]);
r = max(r, mx[pr]);
++pr;
}
}
}
pii try_left() {
int cl = l;
int ans = 0;
LoopR (i,0,l) {
while (i < cl) {
++ans;
--cl;
}
if (mx[i] > r)
return {ans, i};
cl = min(cl, mn[i]);
}
return {INT_MAX, -1};
}
pii try_right() {
int cr = r;
int ans = 0;
Loop (i,r,n) {
while (cr <= i) {
++ans;
++cr;
}
if (mn[i] < l)
return {ans, i};
cr = max(cr, mx[i]);
}
return {INT_MAX, n};
}
void trim(vector<int> &p, int &s) {
int p0 = 0, p1 = p.size()-1;
while (p0 < s && p[p0] == p0)
++p0;
while (s < p1 && p[p1] == p1)
--p1;
vector<int> ans(p1-p0+1);
Loop (i,p0,p1+1)
ans[i-p0] = p[i]-p0;
p = ans;
s = s-p0;
}
long long minimum_walk(std::vector<int> p, int s) {
trim(p, s);
n = p.size();
Loop (i,0,n) {
if (!vis[i])
Do(p, i);
}
ll ans = 0;
l = s;
r = s+1;
pl = s;
pr = s;
go();
for (;;) {
auto [x, xi] = try_left();
auto [y, yi] = try_right();
//cerr << x << ' ' << y << '\n';
if (x == INT_MAX && y == INT_MAX)
break;
if (x < y) {
ans += x;
l = xi;
} else {
ans += y;
r = yi+1;
}
go();
}
while (0 < l) {
--l;
++ans;
go();
}
while (r < n) {
++r;
++ans;
go();
}
ans *= 2;
Loop (i,0,n)
ans += abs(i - p[i]);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
103 ms |
23992 KB |
Output is correct |
31 |
Correct |
106 ms |
20400 KB |
Output is correct |
32 |
Correct |
68 ms |
8064 KB |
Output is correct |
33 |
Correct |
111 ms |
16824 KB |
Output is correct |
34 |
Correct |
111 ms |
16824 KB |
Output is correct |
35 |
Correct |
110 ms |
16808 KB |
Output is correct |
36 |
Correct |
104 ms |
16804 KB |
Output is correct |
37 |
Correct |
86 ms |
16908 KB |
Output is correct |
38 |
Correct |
84 ms |
16948 KB |
Output is correct |
39 |
Correct |
91 ms |
16944 KB |
Output is correct |
40 |
Correct |
85 ms |
17340 KB |
Output is correct |
41 |
Correct |
97 ms |
20276 KB |
Output is correct |
42 |
Correct |
111 ms |
18484 KB |
Output is correct |
43 |
Correct |
109 ms |
16904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
103 ms |
23992 KB |
Output is correct |
31 |
Correct |
106 ms |
20400 KB |
Output is correct |
32 |
Correct |
68 ms |
8064 KB |
Output is correct |
33 |
Correct |
111 ms |
16824 KB |
Output is correct |
34 |
Correct |
111 ms |
16824 KB |
Output is correct |
35 |
Correct |
110 ms |
16808 KB |
Output is correct |
36 |
Correct |
104 ms |
16804 KB |
Output is correct |
37 |
Correct |
86 ms |
16908 KB |
Output is correct |
38 |
Correct |
84 ms |
16948 KB |
Output is correct |
39 |
Correct |
91 ms |
16944 KB |
Output is correct |
40 |
Correct |
85 ms |
17340 KB |
Output is correct |
41 |
Correct |
97 ms |
20276 KB |
Output is correct |
42 |
Correct |
111 ms |
18484 KB |
Output is correct |
43 |
Correct |
109 ms |
16904 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
0 ms |
340 KB |
Output is correct |
50 |
Correct |
0 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
340 KB |
Output is correct |
55 |
Correct |
0 ms |
340 KB |
Output is correct |
56 |
Correct |
0 ms |
340 KB |
Output is correct |
57 |
Correct |
0 ms |
340 KB |
Output is correct |
58 |
Correct |
0 ms |
340 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
0 ms |
340 KB |
Output is correct |
62 |
Correct |
0 ms |
340 KB |
Output is correct |
63 |
Correct |
0 ms |
340 KB |
Output is correct |
64 |
Correct |
98 ms |
16828 KB |
Output is correct |
65 |
Correct |
98 ms |
16816 KB |
Output is correct |
66 |
Correct |
84 ms |
16908 KB |
Output is correct |
67 |
Correct |
83 ms |
16820 KB |
Output is correct |
68 |
Correct |
10 ms |
1988 KB |
Output is correct |
69 |
Correct |
9 ms |
1988 KB |
Output is correct |
70 |
Correct |
9 ms |
1996 KB |
Output is correct |
71 |
Correct |
11 ms |
1980 KB |
Output is correct |
72 |
Correct |
10 ms |
1920 KB |
Output is correct |
73 |
Correct |
8 ms |
1988 KB |
Output is correct |
74 |
Correct |
111 ms |
16820 KB |
Output is correct |
75 |
Correct |
107 ms |
16824 KB |
Output is correct |
76 |
Correct |
100 ms |
16904 KB |
Output is correct |
77 |
Correct |
109 ms |
16948 KB |
Output is correct |
78 |
Correct |
103 ms |
16824 KB |
Output is correct |
79 |
Correct |
110 ms |
16884 KB |
Output is correct |
80 |
Correct |
67 ms |
8064 KB |
Output is correct |
81 |
Correct |
96 ms |
16908 KB |
Output is correct |
82 |
Correct |
105 ms |
16812 KB |
Output is correct |
83 |
Correct |
139 ms |
16816 KB |
Output is correct |
84 |
Correct |
100 ms |
16820 KB |
Output is correct |
85 |
Correct |
90 ms |
16816 KB |
Output is correct |
86 |
Correct |
86 ms |
16904 KB |
Output is correct |
87 |
Correct |
115 ms |
16820 KB |
Output is correct |
88 |
Correct |
128 ms |
16828 KB |
Output is correct |
89 |
Correct |
102 ms |
16928 KB |
Output is correct |
90 |
Correct |
83 ms |
16820 KB |
Output is correct |
91 |
Correct |
84 ms |
16824 KB |
Output is correct |
92 |
Correct |
86 ms |
17032 KB |
Output is correct |