#include<bits/stdc++.h>
using namespace std;
#define _ int v, int tl, int tr, int l, int r
#define tm (tl+tr >> 1)
#define sol v+v,tl,tm,l,r
#define sag v+v+1,tm+1,tr,l,r
#define mp make_pair
#define pb push_back
#define st first
#define nd second
//typedef long long ll;
//typedef pair < ll , ll > pp;
const int mod = 1e9 + 7;
const int N = 1e6 + 6;
int H[N],T[N],A[N],B[N],n,i,j,l,r,ll,rr,t1,t2,t,a,b,lll,rrr;
long long x;
void relax(int &l, int &r, int &a, int &b){
for(;l > a || r < b;){
if(l > a) { l--; a = min(a,A[l]); b = max(b,B[l]); }
if(r < b) { r++; a = min(a,A[r]); b = max(b,B[r]); }
}
}
long long minimum_walk(vector < int > p, int s){
x = 0;
n = p.size();
for(i=0;i<n;i++){
x += abs(p[i] - i);
if(H[i]) continue;
r = i;
for(j=i; !H[j] ; j = p[j]){
H[j] = 1;
r = max(r , j);
}
for(j=i; !T[j] ; j = p[j]){
T[j] = 1; A[j] = i; B[j] = r;
}
}
for(i=0;i<n && p[i] == i; i++);
for(j=n-1;j>=0 && p[j] == j; j--);
l = r = s; a = A[s]; b = B[s];
relax(l,r,a,b);
for(; l > i || r < j ;){
t1 = 0;
ll = lll = l; rr = rrr = r;
if(l > i){
for(a=l, b=r ; rr == r && ll > i ; )
{ ll--; t1++; a = min(A[ll],a); b = max(B[ll],b); relax(ll,rr,a,b); }
if(r >= j) { t += t1; break; }
}
t2 = 0;
if(r < j){
for(a = l, b = r ; lll == l && rrr < j ; )
{ rrr++; t2++; a = min(A[rrr],a); b = max(B[rrr],b); relax(lll,rrr,a,b); }
if(l <= i) { t += t2; break; }
}
if(rr == r) { if(lll != l) assert(0); t += t1+t2; break; }
t += min(t1,t2);
l = ll; r = rr;
if(lll != ll || rrr != rr) assert(0);
}
return x + t+t;
}
/*
int main(){
cout << minimum_walk({1,0,2,3} , 2);
} */
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
1 ms |
376 KB |
Output is correct |
3 |
Correct |
1 ms |
424 KB |
Output is correct |
4 |
Correct |
1 ms |
624 KB |
Output is correct |
5 |
Correct |
1 ms |
624 KB |
Output is correct |
6 |
Correct |
1 ms |
668 KB |
Output is correct |
7 |
Correct |
2 ms |
668 KB |
Output is correct |
8 |
Correct |
1 ms |
668 KB |
Output is correct |
9 |
Correct |
1 ms |
668 KB |
Output is correct |
10 |
Correct |
1 ms |
668 KB |
Output is correct |
11 |
Correct |
1 ms |
668 KB |
Output is correct |
12 |
Correct |
1 ms |
732 KB |
Output is correct |
13 |
Correct |
1 ms |
732 KB |
Output is correct |
14 |
Correct |
1 ms |
732 KB |
Output is correct |
15 |
Correct |
1 ms |
732 KB |
Output is correct |
16 |
Correct |
1 ms |
732 KB |
Output is correct |
17 |
Correct |
1 ms |
732 KB |
Output is correct |
18 |
Correct |
1 ms |
732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
1 ms |
376 KB |
Output is correct |
3 |
Correct |
1 ms |
424 KB |
Output is correct |
4 |
Correct |
1 ms |
624 KB |
Output is correct |
5 |
Correct |
1 ms |
624 KB |
Output is correct |
6 |
Correct |
1 ms |
668 KB |
Output is correct |
7 |
Correct |
2 ms |
668 KB |
Output is correct |
8 |
Correct |
1 ms |
668 KB |
Output is correct |
9 |
Correct |
1 ms |
668 KB |
Output is correct |
10 |
Correct |
1 ms |
668 KB |
Output is correct |
11 |
Correct |
1 ms |
668 KB |
Output is correct |
12 |
Correct |
1 ms |
732 KB |
Output is correct |
13 |
Correct |
1 ms |
732 KB |
Output is correct |
14 |
Correct |
1 ms |
732 KB |
Output is correct |
15 |
Correct |
1 ms |
732 KB |
Output is correct |
16 |
Correct |
1 ms |
732 KB |
Output is correct |
17 |
Correct |
1 ms |
732 KB |
Output is correct |
18 |
Correct |
1 ms |
732 KB |
Output is correct |
19 |
Correct |
1 ms |
732 KB |
Output is correct |
20 |
Correct |
1 ms |
732 KB |
Output is correct |
21 |
Correct |
1 ms |
732 KB |
Output is correct |
22 |
Correct |
1 ms |
732 KB |
Output is correct |
23 |
Correct |
1 ms |
732 KB |
Output is correct |
24 |
Correct |
1 ms |
732 KB |
Output is correct |
25 |
Correct |
1 ms |
732 KB |
Output is correct |
26 |
Correct |
1 ms |
732 KB |
Output is correct |
27 |
Correct |
1 ms |
732 KB |
Output is correct |
28 |
Correct |
1 ms |
732 KB |
Output is correct |
29 |
Correct |
1 ms |
732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
1 ms |
376 KB |
Output is correct |
3 |
Correct |
1 ms |
424 KB |
Output is correct |
4 |
Correct |
1 ms |
624 KB |
Output is correct |
5 |
Correct |
1 ms |
624 KB |
Output is correct |
6 |
Correct |
1 ms |
668 KB |
Output is correct |
7 |
Correct |
2 ms |
668 KB |
Output is correct |
8 |
Correct |
1 ms |
668 KB |
Output is correct |
9 |
Correct |
1 ms |
668 KB |
Output is correct |
10 |
Correct |
1 ms |
668 KB |
Output is correct |
11 |
Correct |
1 ms |
668 KB |
Output is correct |
12 |
Correct |
1 ms |
732 KB |
Output is correct |
13 |
Correct |
1 ms |
732 KB |
Output is correct |
14 |
Correct |
1 ms |
732 KB |
Output is correct |
15 |
Correct |
1 ms |
732 KB |
Output is correct |
16 |
Correct |
1 ms |
732 KB |
Output is correct |
17 |
Correct |
1 ms |
732 KB |
Output is correct |
18 |
Correct |
1 ms |
732 KB |
Output is correct |
19 |
Correct |
1 ms |
732 KB |
Output is correct |
20 |
Correct |
1 ms |
732 KB |
Output is correct |
21 |
Correct |
1 ms |
732 KB |
Output is correct |
22 |
Correct |
1 ms |
732 KB |
Output is correct |
23 |
Correct |
1 ms |
732 KB |
Output is correct |
24 |
Correct |
1 ms |
732 KB |
Output is correct |
25 |
Correct |
1 ms |
732 KB |
Output is correct |
26 |
Correct |
1 ms |
732 KB |
Output is correct |
27 |
Correct |
1 ms |
732 KB |
Output is correct |
28 |
Correct |
1 ms |
732 KB |
Output is correct |
29 |
Correct |
1 ms |
732 KB |
Output is correct |
30 |
Correct |
269 ms |
24044 KB |
Output is correct |
31 |
Correct |
264 ms |
24044 KB |
Output is correct |
32 |
Correct |
143 ms |
24044 KB |
Output is correct |
33 |
Correct |
156 ms |
24044 KB |
Output is correct |
34 |
Correct |
159 ms |
24044 KB |
Output is correct |
35 |
Correct |
160 ms |
24044 KB |
Output is correct |
36 |
Correct |
157 ms |
24044 KB |
Output is correct |
37 |
Correct |
155 ms |
24044 KB |
Output is correct |
38 |
Correct |
162 ms |
24044 KB |
Output is correct |
39 |
Correct |
162 ms |
24044 KB |
Output is correct |
40 |
Correct |
184 ms |
24172 KB |
Output is correct |
41 |
Correct |
243 ms |
24172 KB |
Output is correct |
42 |
Correct |
211 ms |
24172 KB |
Output is correct |
43 |
Correct |
166 ms |
24172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
24172 KB |
Output is correct |
2 |
Correct |
2 ms |
24172 KB |
Output is correct |
3 |
Correct |
1 ms |
24172 KB |
Output is correct |
4 |
Correct |
1 ms |
24172 KB |
Output is correct |
5 |
Correct |
2 ms |
24172 KB |
Output is correct |
6 |
Correct |
1 ms |
24172 KB |
Output is correct |
7 |
Correct |
1 ms |
24172 KB |
Output is correct |
8 |
Correct |
2 ms |
24172 KB |
Output is correct |
9 |
Correct |
1 ms |
24172 KB |
Output is correct |
10 |
Correct |
1 ms |
24172 KB |
Output is correct |
11 |
Correct |
1 ms |
24172 KB |
Output is correct |
12 |
Correct |
1 ms |
24172 KB |
Output is correct |
13 |
Correct |
1 ms |
24172 KB |
Output is correct |
14 |
Correct |
1 ms |
24172 KB |
Output is correct |
15 |
Correct |
1 ms |
24172 KB |
Output is correct |
16 |
Correct |
1 ms |
24172 KB |
Output is correct |
17 |
Correct |
1 ms |
24172 KB |
Output is correct |
18 |
Correct |
2 ms |
24172 KB |
Output is correct |
19 |
Correct |
1 ms |
24172 KB |
Output is correct |
20 |
Correct |
2 ms |
24172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
1 ms |
376 KB |
Output is correct |
3 |
Correct |
1 ms |
424 KB |
Output is correct |
4 |
Correct |
1 ms |
624 KB |
Output is correct |
5 |
Correct |
1 ms |
624 KB |
Output is correct |
6 |
Correct |
1 ms |
668 KB |
Output is correct |
7 |
Correct |
2 ms |
668 KB |
Output is correct |
8 |
Correct |
1 ms |
668 KB |
Output is correct |
9 |
Correct |
1 ms |
668 KB |
Output is correct |
10 |
Correct |
1 ms |
668 KB |
Output is correct |
11 |
Correct |
1 ms |
668 KB |
Output is correct |
12 |
Correct |
1 ms |
732 KB |
Output is correct |
13 |
Correct |
1 ms |
732 KB |
Output is correct |
14 |
Correct |
1 ms |
732 KB |
Output is correct |
15 |
Correct |
1 ms |
732 KB |
Output is correct |
16 |
Correct |
1 ms |
732 KB |
Output is correct |
17 |
Correct |
1 ms |
732 KB |
Output is correct |
18 |
Correct |
1 ms |
732 KB |
Output is correct |
19 |
Correct |
1 ms |
732 KB |
Output is correct |
20 |
Correct |
1 ms |
732 KB |
Output is correct |
21 |
Correct |
1 ms |
732 KB |
Output is correct |
22 |
Correct |
1 ms |
732 KB |
Output is correct |
23 |
Correct |
1 ms |
732 KB |
Output is correct |
24 |
Correct |
1 ms |
732 KB |
Output is correct |
25 |
Correct |
1 ms |
732 KB |
Output is correct |
26 |
Correct |
1 ms |
732 KB |
Output is correct |
27 |
Correct |
1 ms |
732 KB |
Output is correct |
28 |
Correct |
1 ms |
732 KB |
Output is correct |
29 |
Correct |
1 ms |
732 KB |
Output is correct |
30 |
Correct |
269 ms |
24044 KB |
Output is correct |
31 |
Correct |
264 ms |
24044 KB |
Output is correct |
32 |
Correct |
143 ms |
24044 KB |
Output is correct |
33 |
Correct |
156 ms |
24044 KB |
Output is correct |
34 |
Correct |
159 ms |
24044 KB |
Output is correct |
35 |
Correct |
160 ms |
24044 KB |
Output is correct |
36 |
Correct |
157 ms |
24044 KB |
Output is correct |
37 |
Correct |
155 ms |
24044 KB |
Output is correct |
38 |
Correct |
162 ms |
24044 KB |
Output is correct |
39 |
Correct |
162 ms |
24044 KB |
Output is correct |
40 |
Correct |
184 ms |
24172 KB |
Output is correct |
41 |
Correct |
243 ms |
24172 KB |
Output is correct |
42 |
Correct |
211 ms |
24172 KB |
Output is correct |
43 |
Correct |
166 ms |
24172 KB |
Output is correct |
44 |
Correct |
1 ms |
24172 KB |
Output is correct |
45 |
Correct |
2 ms |
24172 KB |
Output is correct |
46 |
Correct |
1 ms |
24172 KB |
Output is correct |
47 |
Correct |
1 ms |
24172 KB |
Output is correct |
48 |
Correct |
2 ms |
24172 KB |
Output is correct |
49 |
Correct |
1 ms |
24172 KB |
Output is correct |
50 |
Correct |
1 ms |
24172 KB |
Output is correct |
51 |
Correct |
2 ms |
24172 KB |
Output is correct |
52 |
Correct |
1 ms |
24172 KB |
Output is correct |
53 |
Correct |
1 ms |
24172 KB |
Output is correct |
54 |
Correct |
1 ms |
24172 KB |
Output is correct |
55 |
Correct |
1 ms |
24172 KB |
Output is correct |
56 |
Correct |
1 ms |
24172 KB |
Output is correct |
57 |
Correct |
1 ms |
24172 KB |
Output is correct |
58 |
Correct |
1 ms |
24172 KB |
Output is correct |
59 |
Correct |
1 ms |
24172 KB |
Output is correct |
60 |
Correct |
1 ms |
24172 KB |
Output is correct |
61 |
Correct |
2 ms |
24172 KB |
Output is correct |
62 |
Correct |
1 ms |
24172 KB |
Output is correct |
63 |
Correct |
2 ms |
24172 KB |
Output is correct |
64 |
Correct |
180 ms |
24172 KB |
Output is correct |
65 |
Correct |
191 ms |
24172 KB |
Output is correct |
66 |
Correct |
166 ms |
30840 KB |
Output is correct |
67 |
Correct |
209 ms |
37652 KB |
Output is correct |
68 |
Correct |
17 ms |
37652 KB |
Output is correct |
69 |
Correct |
17 ms |
37652 KB |
Output is correct |
70 |
Correct |
16 ms |
37652 KB |
Output is correct |
71 |
Correct |
16 ms |
37652 KB |
Output is correct |
72 |
Correct |
16 ms |
37652 KB |
Output is correct |
73 |
Correct |
28 ms |
37652 KB |
Output is correct |
74 |
Correct |
158 ms |
47932 KB |
Output is correct |
75 |
Correct |
194 ms |
54664 KB |
Output is correct |
76 |
Correct |
162 ms |
61404 KB |
Output is correct |
77 |
Correct |
178 ms |
68092 KB |
Output is correct |
78 |
Correct |
161 ms |
74892 KB |
Output is correct |
79 |
Correct |
161 ms |
81636 KB |
Output is correct |
80 |
Correct |
148 ms |
88436 KB |
Output is correct |
81 |
Correct |
152 ms |
95128 KB |
Output is correct |
82 |
Correct |
152 ms |
101868 KB |
Output is correct |
83 |
Correct |
163 ms |
108596 KB |
Output is correct |
84 |
Correct |
170 ms |
115268 KB |
Output is correct |
85 |
Correct |
173 ms |
122112 KB |
Output is correct |
86 |
Correct |
161 ms |
128800 KB |
Output is correct |
87 |
Correct |
182 ms |
135544 KB |
Output is correct |
88 |
Correct |
169 ms |
142336 KB |
Output is correct |
89 |
Correct |
164 ms |
149080 KB |
Output is correct |
90 |
Correct |
162 ms |
155812 KB |
Output is correct |
91 |
Correct |
169 ms |
162480 KB |
Output is correct |
92 |
Correct |
224 ms |
169312 KB |
Output is correct |