# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
228042 | AaronNaidu | Ancient Books (IOI17_books) | C++14 | 5 ms | 384 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>
using namespace std;
typedef long long ll;
int pos[300000];
ll minimum_walk(vector<int> p, int s) {
int trueLength = p.size();
for (int i = 0; i < p.size(); i++)
{
pos[p[i]] = i;
}
for (int i = p.size() - 1; i >= 0; i--)
{
if (p[i] == i)
{
trueLength--;
}
else
{
break;
}
}
ll toRet = 0;
int startPoint = 0;
int maxSoFar = -1;
int groups = 0;
for (int i = 0; i < trueLength; i++)
{
toRet += abs(i - p[i]);
maxSoFar = max(maxSoFar, p[i]);
if (maxSoFar == i)
{
// cout << "Breaking at " << i << "\n";
groups++;
if (i < trueLength - 1)
{
// cout << "Adding " << abs(pos[startPoint] - (i+1)) << " for travelling\n";
//toRet += abs(pos[startPoint] - (i+1));
startPoint = i+1;
}
else
{
//cout << "Adjustment adding " << abs(startPoint - p[startPoint]) << " for " << startPoint << "\n";
//toRet += abs(pos[startPoint] - p[pos[startPoint]]);
}
}
}
//cout << "Adding " << startPoint << " at end\n";
//toRet += startPoint;
toRet += 2 * (groups - 1);
return toRet;
}
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... |