#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int>vi;
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define FOR(i,a,b) for(int i=a; i<b; i++)
void dbgv(vi vec){for(int x: vec) cout << x << ' '; cout << endl; }
//---------------------------
int N;
vi a,p;
int check(){
FOR(i,0,N) if(a[i]==-1 || p[a[i]]!=i) return 0;
return 1;
}
int dist(int i, int j){
return abs(i-j);
}
ll minimum_walk(vi p, int s) {
::p=p;
N=sz(p);
a.resize(N);
iota(all(a),0);
vi vis(N,0);
ll ans=0;
int cur=s;
FOR(i,0,N) if(!vis[i]){
ans+=dist(cur,i);
cur=i;
while(!vis[cur]){
vis[cur]=1;
ans+=dist(cur,p[cur]);
cur=p[cur];
}
}
ans+=cur;
return ans;
}
/*
4 0
0 2 3 1
*/
/*
3 0
2 0 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 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 |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 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 |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 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 |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '5161' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '8' |
3 |
Halted |
0 ms |
0 KB |
- |