#include<bits/stdc++.h>
#include "books.h"
using namespace std;
#define vi vector<int>
#define REP(i,n) for(int i = 0; i < n; i ++)
#define FOR(i,a,b) for(int i = a; i < b; i ++)
const int MX = 1000005;
int n,S;
int lft,rgt,cur = 0;
int C[MX],L[MX],R[MX];
int solve(int l,int r){
int ll = L[C[l]];
int rr = R[C[r]];
while(ll < l or rr > r){
if(ll < l){
l--;
ll = min(ll,L[C[l]]);
rr = max(rr,R[C[l]]);
}
else{
r++;
rr = max(rr,R[C[r]]);
ll = min(ll,L[C[r]]);
}
}
int costleft = 0,costright = 0;
int indleft = -1,indright = -1;
int mn = l,mx = r;
for(int i = l-1; i >= lft; i --){
if(R[C[i]] > S){
indleft = i;
break;
}
}
for(int i = r+1; i <= rgt; i ++){
if(L[C[i]] < S){
indright = i;
break;
}
}
if(indleft == -1){
for(int i = l-1; i >= lft; i --){
mn = min(mn,L[C[i]]);
if(mn == i) costleft += 2;
}
for(int i = r+1; i <= rgt; i ++){
mx = max(mx,R[C[i]]);
if(mx == i) costright += 2;
}
return costleft+costright;
}
int lcur = l,rcur = r;
while(ll > indleft){
costleft += 2;
ll--;
while(ll < l){
l--;
ll = min(ll,L[C[l]]);
rcur = max(rcur,R[C[l]]);
}
}
while(rr < indright){
costright += 2;
rr++;
while(rr > r){
r++;
rr = max(rr,R[C[r]]);
lcur = min(lcur,L[C[l]]);
}
}
ll = min(lcur,ll);
rr = max(rcur,rr);
while(ll < l or rr > r){
if(ll < l){
l--;
ll = min(ll,L[C[l]]);
rr = max(rr,R[C[l]]);
}
else{
r++;
rr = max(rr,R[C[r]]);
ll = min(ll,L[C[r]]);
}
}
return min(costleft,costright)+solve(ll,rr);
}
long long minimum_walk(vi p, int s) {
S = s;
long long ans = 0;
n = p.size();
lft = s;
rgt = s;
REP(i,n) C[i] = -1;
REP(i,n){
ans += abs(p[i]-i);
if(C[i] == -1){
C[i] = cur;
L[cur] = R[cur] = i;
int j = i;
while(true){
j = p[j];
C[j] = cur;
R[cur] = max(R[cur],j);
if(i == j) break;
}
cur++;
}
if(i != p[i]){
lft = min(lft,i);
rgt = max(rgt,i);
}
}
return ans+solve(s,s);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
272 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
304 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
272 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
304 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
182 ms |
12152 KB |
Output is correct |
31 |
Correct |
171 ms |
12168 KB |
Output is correct |
32 |
Correct |
135 ms |
19908 KB |
Output is correct |
33 |
Correct |
134 ms |
17524 KB |
Output is correct |
34 |
Correct |
154 ms |
17552 KB |
Output is correct |
35 |
Correct |
134 ms |
16376 KB |
Output is correct |
36 |
Correct |
130 ms |
14068 KB |
Output is correct |
37 |
Correct |
163 ms |
12380 KB |
Output is correct |
38 |
Correct |
128 ms |
12228 KB |
Output is correct |
39 |
Correct |
131 ms |
12208 KB |
Output is correct |
40 |
Correct |
164 ms |
12156 KB |
Output is correct |
41 |
Correct |
163 ms |
12136 KB |
Output is correct |
42 |
Correct |
143 ms |
12192 KB |
Output is correct |
43 |
Correct |
135 ms |
17860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
272 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
304 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
182 ms |
12152 KB |
Output is correct |
31 |
Correct |
171 ms |
12168 KB |
Output is correct |
32 |
Correct |
135 ms |
19908 KB |
Output is correct |
33 |
Correct |
134 ms |
17524 KB |
Output is correct |
34 |
Correct |
154 ms |
17552 KB |
Output is correct |
35 |
Correct |
134 ms |
16376 KB |
Output is correct |
36 |
Correct |
130 ms |
14068 KB |
Output is correct |
37 |
Correct |
163 ms |
12380 KB |
Output is correct |
38 |
Correct |
128 ms |
12228 KB |
Output is correct |
39 |
Correct |
131 ms |
12208 KB |
Output is correct |
40 |
Correct |
164 ms |
12156 KB |
Output is correct |
41 |
Correct |
163 ms |
12136 KB |
Output is correct |
42 |
Correct |
143 ms |
12192 KB |
Output is correct |
43 |
Correct |
135 ms |
17860 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
296 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
300 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
3 ms |
332 KB |
Output is correct |
52 |
Correct |
1 ms |
332 KB |
Output is correct |
53 |
Correct |
1 ms |
332 KB |
Output is correct |
54 |
Correct |
1 ms |
304 KB |
Output is correct |
55 |
Correct |
1 ms |
300 KB |
Output is correct |
56 |
Correct |
1 ms |
304 KB |
Output is correct |
57 |
Correct |
1 ms |
332 KB |
Output is correct |
58 |
Correct |
1 ms |
332 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
304 KB |
Output is correct |
61 |
Correct |
1 ms |
332 KB |
Output is correct |
62 |
Correct |
1 ms |
332 KB |
Output is correct |
63 |
Correct |
1 ms |
332 KB |
Output is correct |
64 |
Correct |
177 ms |
24548 KB |
Output is correct |
65 |
Correct |
154 ms |
25136 KB |
Output is correct |
66 |
Correct |
132 ms |
19056 KB |
Output is correct |
67 |
Correct |
133 ms |
18916 KB |
Output is correct |
68 |
Correct |
13 ms |
2308 KB |
Output is correct |
69 |
Correct |
13 ms |
2124 KB |
Output is correct |
70 |
Correct |
15 ms |
2252 KB |
Output is correct |
71 |
Correct |
14 ms |
2480 KB |
Output is correct |
72 |
Correct |
21 ms |
2684 KB |
Output is correct |
73 |
Correct |
14 ms |
1972 KB |
Output is correct |
74 |
Correct |
138 ms |
24168 KB |
Output is correct |
75 |
Correct |
159 ms |
24132 KB |
Output is correct |
76 |
Correct |
150 ms |
24420 KB |
Output is correct |
77 |
Correct |
135 ms |
24556 KB |
Output is correct |
78 |
Correct |
165 ms |
23776 KB |
Output is correct |
79 |
Correct |
146 ms |
23668 KB |
Output is correct |
80 |
Correct |
131 ms |
26568 KB |
Output is correct |
81 |
Correct |
163 ms |
22644 KB |
Output is correct |
82 |
Correct |
136 ms |
22636 KB |
Output is correct |
83 |
Correct |
134 ms |
21736 KB |
Output is correct |
84 |
Correct |
134 ms |
21092 KB |
Output is correct |
85 |
Correct |
144 ms |
19812 KB |
Output is correct |
86 |
Correct |
131 ms |
19176 KB |
Output is correct |
87 |
Correct |
137 ms |
25316 KB |
Output is correct |
88 |
Correct |
144 ms |
24296 KB |
Output is correct |
89 |
Correct |
166 ms |
22376 KB |
Output is correct |
90 |
Correct |
135 ms |
18876 KB |
Output is correct |
91 |
Correct |
131 ms |
18816 KB |
Output is correct |
92 |
Correct |
156 ms |
18796 KB |
Output is correct |