#include<bits/stdc++.h>
#define MAXN 1000007
using namespace std;
int n,perm[MAXN],br,cnt,last,maxpos,to[MAXN],curr;
bool li[MAXN],ok;
long long sum,add[MAXN],ans;
void dfs(int x){
li[x]=true; maxpos=max(maxpos,x);
sum+=abs(x-perm[x]); br++;
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=maxpos=0;
dfs(i); add[i]=sum;
to[i]=maxpos;
if(br>1)last=i;
}
}
for(int i=0;i<=last;i++){
ans+=add[i];
if(to[i]>=last)ok=false;
if(i<last and ok)ans++;
}
ans+=last;
return ans;
}
/*
int main(){
cout<<minimum_walk({0, 2, 3, 1}, 0)<<"\n";
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3736' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |