#include<bits/stdc++.h>
#define MAXN 1000007
using namespace std;
int n,perm[MAXN],br,cnt,last,maxpos,to[MAXN],curr,pos,first;
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]);
}
void dfs2(int x){
li[x]=true;
sum+=abs(x-perm[x]); br++;
update(1,0,n-1,min(x,perm[x])+1,max(x,perm[x]));
if(!li[perm[x]])dfs2(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=s;i<=last;i++){
ans+=add[i];
if(!getpos(1,0,n-1,i) and i<last)ans+=2;
}
for(int i=0;i<4*n;i++)tree[i]=false;
for(int i=0;i<n;i++)li[i]=false;
for(int i=n-1;i>=0;i--){
if(!li[i]){
sum=br=0;
dfs2(i); add[i]=sum;
if(br>1)first=i;
}
}
for(int i=s-1;i>=first;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 |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 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 |
0 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 |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 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 |
0 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 |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
919 ms |
16884 KB |
Output is correct |
31 |
Correct |
896 ms |
16880 KB |
Output is correct |
32 |
Correct |
103 ms |
24820 KB |
Output is correct |
33 |
Correct |
320 ms |
24728 KB |
Output is correct |
34 |
Correct |
306 ms |
24712 KB |
Output is correct |
35 |
Correct |
369 ms |
24780 KB |
Output is correct |
36 |
Correct |
433 ms |
24772 KB |
Output is correct |
37 |
Correct |
521 ms |
24748 KB |
Output is correct |
38 |
Correct |
609 ms |
22988 KB |
Output is correct |
39 |
Correct |
590 ms |
22468 KB |
Output is correct |
40 |
Correct |
709 ms |
18752 KB |
Output is correct |
41 |
Correct |
898 ms |
17128 KB |
Output is correct |
42 |
Correct |
740 ms |
17464 KB |
Output is correct |
43 |
Correct |
343 ms |
24684 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: '1046' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 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 |
0 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 |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
919 ms |
16884 KB |
Output is correct |
31 |
Correct |
896 ms |
16880 KB |
Output is correct |
32 |
Correct |
103 ms |
24820 KB |
Output is correct |
33 |
Correct |
320 ms |
24728 KB |
Output is correct |
34 |
Correct |
306 ms |
24712 KB |
Output is correct |
35 |
Correct |
369 ms |
24780 KB |
Output is correct |
36 |
Correct |
433 ms |
24772 KB |
Output is correct |
37 |
Correct |
521 ms |
24748 KB |
Output is correct |
38 |
Correct |
609 ms |
22988 KB |
Output is correct |
39 |
Correct |
590 ms |
22468 KB |
Output is correct |
40 |
Correct |
709 ms |
18752 KB |
Output is correct |
41 |
Correct |
898 ms |
17128 KB |
Output is correct |
42 |
Correct |
740 ms |
17464 KB |
Output is correct |
43 |
Correct |
343 ms |
24684 KB |
Output is correct |
44 |
Incorrect |
1 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '1046' |
45 |
Halted |
0 ms |
0 KB |
- |