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>
#define PRINT(x) cerr<<#x<<'='<<x<<endl;
#define NL(x) " \n"[(x)]
#define lld long long
#define pil pair<int,lld>
#define pli pair<lld,int>
#define pll pair<lld,lld>
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define mid (l+r)/2
#define endl '\n'
#define all(a) begin(a),end(a)
#define sz(a) int((a).size())
#define LINF 1000000000000000LL
#define INF 1000000000
#define EPS 1e-9
using namespace std;
#define MAXN 1000010
lld N, posl, pos[MAXN], a[MAXN];
void dfs(int node) {
pos[node] = true;
if(!pos[a[node]]) dfs(a[node]);
}
long long minimum_walk(std::vector<int> p, int s) {
lld rez = 0;
N = sz(p);
for(int i = 0; i < N; i++) a[i] = p[i], rez += abs(p[i]-i);
for(int i = 0; i < N; i++) if(i == p[i]) pos[i] = true;
for(int i = 0; i < N; i++) {
if(!pos[i]) {
posl = i;
dfs(i);
}
}
rez += 2*posl;
return rez;
}
# | 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... |