# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
355060 | kylych03 | Ancient Books (IOI17_books) | C++14 | 197 ms | 22872 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 "grader.cpp"
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int vis[1000001];
long long minimum_walk(std::vector<int> p, int s) {
ll ans=0;
vector <pair<ll, ll>> vec;
for(int i = 0 ; i < p.size(); i++){
ans+= abs( i - p[i]);
int j = i;
int l = i , r = i;
while(vis[j]==0){
l=min(l,j);
r = max(r,j);
vis[j]=1;
j = p[j];
}
if(l!=r)
vec.push_back(make_pair(l,r));
}
vec.push_back(make_pair(0,0));
//cout << vec.size()<<endl;
sort( vec.begin(), vec.end() );
for(int i = 1; i < vec.size();i++){
ans += (2 * max(0ll, vec[i].first - vec[i-1].second));
vec[i].second = max(vec[i].second, vec[i-1].second);
}
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... |