#include "books.h"
#include<bits/stdc++.h>
using namespace std;
int n;
vector<int> father;
vector<long long> dist;
int root(int node){
if(father[node] == node) return node;
return father[node] = root(father[node]);
}
void mergeComponents(int actNode, int destiny){
int rAct = root(actNode);
int rNext = root(destiny);
father[rAct] = rNext;
if(rAct != rNext) dist[rNext] += (long long)dist[rAct];
dist[rNext] += (long long)abs(destiny - actNode);
}
bool already(vector<int> p){
for(int i = 0; i < n; ++i){
if(p[i] != i) return false;
}
return true;
}
long long minimum_walk(vector<int> p, int s) {
n = p.size();
if(already(p)) return 0;
father = vector<int>(n);
for(int i = 0; i < n; ++i) father[i] = i;
dist = vector<long long int>(n, 0);
for(int i = 0; i < n; ++i){
mergeComponents(i, p[i]);
}
if(root(s) == s and dist[s] == 0){
if(s > 0){
mergeComponents(s, s-1);
dist[root(s)]++;
}
else{
mergeComponents(s, s+1);
dist[root(s)]++;
}
}
for(int i = 0; i < n-1; ++i){
int rAct = root(i);
if(rAct == i and dist[i] == 0) continue;
int rNext = root(i+1);
if(rNext == i+1 and dist[i+1]==0)continue;
if(rAct == rNext) continue;
mergeComponents(i, i+1);
dist[root(rAct)]++;
}
return dist[root(s)];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
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: '12' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '8', found: '10' |
7 |
Halted |
0 ms |
0 KB |
- |