#include "books.h"
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
int f[maxn],cnt,d[maxn],num;
long long minimum_walk(std::vector<int> p, int s) {
long long ans=0;
int n=(int)p.size();
for(int i=0;i<n;i++){
if(!f[i]){
int u=i;cnt++;
while(!f[u]) f[u]=cnt,u=p[u];
}
ans+=abs(i-p[i]);
}
int mn=n,r=0;
for(int i=0;i<n;i++){
r=max(r,i);
while(r<n && num<cnt){
num-=!!d[f[r]];
d[f[r]]++;
num+=!!d[f[r]];
r++;
}
if(num==cnt) mn=min(mn,max(r-1,s)-min(i,s));
num-=!!d[f[i]];
d[f[i]]--;
num+=!!d[f[i]];
}
return ans+2*mn;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4734' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |