# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
821309 | vjudge1 | Ancient Books (IOI17_books) | C++17 | 1 ms | 468 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 "books.h"
#include<bits/stdc++.h>
using namespace std;
long long minimum_walk(vector<int> p, int s) {
int n = (int)p.size();
bool us[n] = {};
vector<vector<int>> cmps;
int sum = 0;
for(int i = 0; i < n; i++) {
sum += abs(p[i] - i);
if(us[i]) continue;
int x = i;
cmps.push_back({});
while(!us[x]) {
us[x] = 1;
cmps.back().push_back(x);
x = p[x];
}
}
if((int)cmps.size() == 1)
return sum;
if(n == 2)
return 0;
if(n == 3) {
int l = 0;
while(l < n && p[l] == l) l++;
return sum + 2 * l;
}
if(n == 4) {
if((int)cmps.size() == 4)
return 0;
if((int)cmps.size() == 3) {
int l = 0;
while(l < n && p[l] == l) l++;
return sum + 2 * l;
}
if((int)cmps.size() == 1)
return sum;
int mx = 0;
for(auto v : cmps)
mx = max(mx, (int)v.size());
if(mx > 2) {
int l = 0;
while(l < n && p[l] == l) l++;
return sum + 2 * l;
}
if(cmps[0].back() < cmps[1][0])
return sum + 2;
return sum + 1;
}
}
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... |