#include<bits/stdc++.h>
#define MAXN 1000007
using namespace std;
int n,perm[MAXN],br,cnt,last,maxpos,to[MAXN],curr,pos;
bool li[MAXN],ok,used[MAXN];
long long sum,add[MAXN],ans;
bool tree[4*MAXN];
void update(int v,int l,int r,int ll,int rr){
if(ll>rr)return;
if(l==ll and r==rr){
tree[v]=true;
}else{
int tt=(l+r)/2;
update(2*v,l,tt,ll,min(tt,rr));
update(2*v+1,tt+1,r,max(tt+1,ll),rr);
}
}
bool getpos(int v,int l,int r,int pos){
if(l==r)return tree[v];
int tt=(l+r)/2;
if(pos<=tt)return getpos(2*v,l,tt,pos) or tree[v];
else return getpos(2*v+1,tt+1,r,pos) or tree[v];
}
void dfs(int x){
li[x]=true;
sum+=abs(x-perm[x]); br++;
update(1,0,n-1,min(x,perm[x]),max(x,perm[x])-1);
if(!li[perm[x]])dfs(perm[x]);
}
long long minimum_walk(vector<int> p, int s){
n=int(p.size());
for(int i=0;i<n;i++)perm[i]=p[i];
for(int i=0;i<n;i++){
if(!li[i]){
sum=br=0;
dfs(i); add[i]=sum;
if(br>1)last=i;
}
}
for(int i=0;i<=last;i++){
ans+=add[i];
if(!getpos(1,0,n-1,i) and i<last)ans+=2;
}
return ans;
}
/*
int main(){
cout<<minimum_walk({0,2,3,1}, 0)<<"\n";
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
224 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 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 |
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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
224 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 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 |
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 |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
224 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 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 |
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 |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
474 ms |
15216 KB |
Output is correct |
31 |
Correct |
486 ms |
21880 KB |
Output is correct |
32 |
Correct |
111 ms |
27508 KB |
Output is correct |
33 |
Correct |
246 ms |
29432 KB |
Output is correct |
34 |
Correct |
242 ms |
29544 KB |
Output is correct |
35 |
Correct |
265 ms |
29496 KB |
Output is correct |
36 |
Correct |
308 ms |
29648 KB |
Output is correct |
37 |
Correct |
343 ms |
29632 KB |
Output is correct |
38 |
Correct |
384 ms |
27124 KB |
Output is correct |
39 |
Correct |
393 ms |
26736 KB |
Output is correct |
40 |
Correct |
425 ms |
23156 KB |
Output is correct |
41 |
Correct |
464 ms |
22016 KB |
Output is correct |
42 |
Correct |
454 ms |
22124 KB |
Output is correct |
43 |
Correct |
263 ms |
29684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
224 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 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 |
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 |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
474 ms |
15216 KB |
Output is correct |
31 |
Correct |
486 ms |
21880 KB |
Output is correct |
32 |
Correct |
111 ms |
27508 KB |
Output is correct |
33 |
Correct |
246 ms |
29432 KB |
Output is correct |
34 |
Correct |
242 ms |
29544 KB |
Output is correct |
35 |
Correct |
265 ms |
29496 KB |
Output is correct |
36 |
Correct |
308 ms |
29648 KB |
Output is correct |
37 |
Correct |
343 ms |
29632 KB |
Output is correct |
38 |
Correct |
384 ms |
27124 KB |
Output is correct |
39 |
Correct |
393 ms |
26736 KB |
Output is correct |
40 |
Correct |
425 ms |
23156 KB |
Output is correct |
41 |
Correct |
464 ms |
22016 KB |
Output is correct |
42 |
Correct |
454 ms |
22124 KB |
Output is correct |
43 |
Correct |
263 ms |
29684 KB |
Output is correct |
44 |
Incorrect |
1 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |