#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
ll minimum_walk(vector<int> p, int s){
int n = p.size();
ll ans = 0;
vector<int> cycle_id(n, -1);
int k = 0;
for(int i = 0; i < n; i++){
if(cycle_id[i] != -1) continue;
int x = p[i];
cycle_id[i] = k;
ans+=abs(i-x);
while(x != i){
cycle_id[x] = k;
ans+=abs(p[x]-x);
x = p[x];
}
k++;
}
vector<int> cnt(k, 0);
for(int i = 0; i < n; i++) cnt[cycle_id[i]]++;
vector<bool> nested(k, 0);
set<int> st;
for(int i = 0; i < n; i++){
st.insert(cycle_id[i]);
if(st.size() > 2) nested[cycle_id[i]] = 1;
cnt[cycle_id[i]]--;
if(cnt[cycle_id[i]] == 0) st.erase(cycle_id[i]);
}
ans+=k-1;
for(int i = 1; i < k; i++) if(!nested[i]) ans++;
return ans;
}
# |
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: '4110' |
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 |
- |