# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1113012 | SalihSahin | Ancient Books (IOI17_books) | C++14 | 1 ms | 504 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 <bits/stdc++.h>
#define pb push_back
#include "books.h"
using namespace std;
long long minimum_walk(vector<int> p, int s) {
long long ans = 0;
int n = p.size();
int mxs = 0;
vector<int> vis(n);
vector<array<int, 2> > cyc;
for(int i = 0; i < n; i++){
if(vis[i] || p[i] == i) continue;
int l = i, r = i;
int x = i;
long long add = 0;
while(!vis[x]){
vis[x] = 1;
add += abs(x - p[x]);
x = p[x];
r = max(r, x);
}
cyc.pb({l, r});
ans += add;
}
int rval = 0;
for(int i = 0; i < cyc.size(); i++){
if(cyc[i][0] > rval) mxs = max(mxs, cyc[i][0]);
rval = cyc[i][1];
}
ans += mxs*2;
return ans;
}
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... |