이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "books.h"
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
vector<bool> vis(n);
vector<long long> cost(n);
vector<int> to(n);
long long ans = 0;
for (int i = 0; i < n; i++) if (i != p[i] && !vis[i]) {
int right = i;
int tmp = i;
while (!vis[tmp]) {
ckmax(right, tmp);
ans += abs(tmp - p[tmp]), vis[tmp] = 1, tmp = p[tmp];
}
to[i] = right;
}
int on = 0;
fill(all(vis), 0);
for (int i = 0; i < n; i++) if (i != p[i] && !vis[i]) {
int tmp = i;
while (!vis[tmp]) vis[tmp] = 1, tmp = p[tmp];
if (i <= on) ckmax(on, to[i]);
else ans += 2 * (i - on), on = to[i];
}
return ans;
}
# | 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... |