# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
328320 | arnold518 | Ancient Books (IOI17_books) | C++14 | 1 ms | 512 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;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e6;
int N;
int A[MAXN+10], B[MAXN+10], S;
ll ans;
ll minimum_walk(vector<int> _P, int _S)
{
N=_P.size(); S=_S+1;
for(int i=1; i<=N; i++) A[i]=_P[i-1]+1;
for(int i=1; i<=N; i++) B[A[i]]=i;
for(int i=1; i<=N; i++) ans+=abs(i-A[i]);
vector<pii> V;
for(int i=1; i<=N; i++)
{
int l=min(i, A[i]), r=max(i, A[i]);
V.push_back({l, r});
}
sort(V.begin(), V.end());
int p=0, q=0;
vector<pii> VV;
for(auto it : V)
{
if(q<it.first)
{
VV.push_back({p, q});
p=it.first; q=it.second;
}
q=it.second;
}
if(p) VV.push_back({p, q});
ans+=(VV.size()-1)*2;
for(int i=0; i<VV.size() && VV[i].first==VV[i].second && VV[i].second<S; i++) ans-=2;
for(int i=VV.size()-2; i>=0 && VV[i].first==VV[i].second && VV[i].first>S; i--) ans-=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... |