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 << 20], maxp;
bool used[1 << 20];
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);
maxp = max(maxp, 1LL * cx);
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 = 0; i < maxp; i++) { if (cnt[i] == 0) sum += 2LL; }
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... |