# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1023520 | HappyCapybara | Ancient Books (IOI17_books) | C++17 | 1 ms | 348 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "books.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int> par, siz;
int find(int a){
if (par[a] == a) return a;
return par[a] = find(par[a]);
}
void merge(int a, int b){
a = find(a);
b = find(b);
if (a == b) return;
else par[a] = b;
siz[b] += siz[a];
siz[a] = 0;
}
ll minimum_walk(vector<int> p, int s){
int n = p.size();
par.resize(n);
siz.resize(n, 1);
for (int i=0; i<n; i++) par[i] = i;
for (int i=0; i<n; i++) merge(i, p[i]);
int res = -2;
for (int i=0; i<n; i++){
res += abs(p[i]-i);
if (siz[i] > 1) res += 2;
}
if (siz[0] = 1) res += 2;
return res;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |