Submission #228026

#TimeUsernameProblemLanguageResultExecution timeMemory
228026AaronNaiduAncient Books (IOI17_books)C++14
12 / 100
5 ms384 KiB
#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;
    for (int i = 0; i < trueLength; i++)
    {
        maxSoFar = max(maxSoFar, p[i]);
        if (maxSoFar == i)
        {
        //    cout << "Breaking at " << i << "\n";
            for (int j = startPoint+1; j <= i; j++)
            {
             //   cout << "Adding " << abs(j - p[j]) << " for " << j << "\n";
                toRet += abs(pos[j] - p[pos[j]]);
            }
            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;
    return toRet;
}

Compilation message (stderr)

books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:9:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < p.size(); i++)
                     ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...