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;
long long N;
long long cnt[1 << 18];
bool used[1 << 18];
long long minimum_walk(vector<int> p, int s) {
N = p.size();
for (int i = 0; i < N; i++) {
if (used[i] == true || p[i] == i) continue;
int cx = i;
while (used[cx] == false) {
int e1 = cx, e2 = p[cx]; if (e1 > e2) swap(e1, e2);
cnt[e1] += 1;
cnt[e2] -= 1;
used[cx] = true;
cx = p[cx];
}
}
for (int i = 1; i < N; i++) cnt[i] += cnt[i - 1];
long long sum = 0;
for (int i = 0; i < N; i++) sum += cnt[i];
for (int i = 1; i < N; i++) {
if (cnt[i - 1] == 0LL && cnt[i] >= 1LL) sum += 2LL * i;
}
return sum;
}
# | 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... |