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>
#define FOR(i,p,k) for(int i=(p);i<=(k);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) (int((x).size()))
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
ll minimum_walk(vector<int> perm, int start){
int n = ssize(perm);
ll wyn = 0ll;
REP(i, n) wyn += abs(i-perm[i]);
set<int> ciekawe;
REP(i, n) if(perm[i] != i) ciekawe.emplace(i);
vector<int> lewo, prawo;
vector<int> odw(n, -1);
REP(i, n) if(odw[i] < 0){
int mini = i, maks = i;
for(int w = i; odw[w] < 0; w = perm[w]){
odw[w] = ssize(lewo);
mini = min(mini, w);
maks = max(maks, w);
}
lewo.emplace_back(mini);
prawo.emplace_back(maks);
}
bool cokolwiek = 0;
int l = start, p = start;
while(ssize(ciekawe)){
auto it = ciekawe.lower_bound(l);
if(it != ciekawe.end() && *it <= p){
l = min(l, lewo[odw[*it]]);
p = max(p, prawo[odw[*it]]);
ciekawe.erase(it);
continue;
}
if(cokolwiek){
if(it != ciekawe.end()) wyn += (*it-p)<<1, p = prawo[odw[*it]], ciekawe.erase(it);
else wyn += (l-*prev(it))<<1, l = lewo[odw[*prev(it)]], ciekawe.erase(prev(it));
continue;
}
if(l <= *ciekawe.begin() || *prev(ciekawe.end()) <= p){
cokolwiek = 1;
continue;
}
bool git = 0;
int koszt_lewo = 0;
int nowe_lewo = l;
int iter = l;
while(iter > 0){
if(iter == nowe_lewo) --nowe_lewo, ++koszt_lewo;
--iter;
if(iter != perm[iter]){
nowe_lewo = min(nowe_lewo, lewo[odw[iter]]);
if(prawo[odw[iter]] > p){git = 1; break;}
}
}
if(!git){
cokolwiek = 1;
continue;
}
git = 0;
int koszt_prawo = 0;
int nowe_prawo = p;
iter = p;
while(iter < n-1){
if(iter == nowe_prawo) ++nowe_prawo, ++koszt_prawo;
++iter;
if(iter != perm[iter]){
nowe_prawo = max(nowe_prawo, prawo[odw[iter]]);
if(lewo[odw[iter]] < l){git = 1; break;}
}
}
if(!git){
cokolwiek = 1;
continue;
}
if(koszt_lewo < koszt_prawo) wyn += koszt_lewo<<1, l = nowe_lewo;
else wyn += koszt_prawo<<1, p = nowe_prawo;
}
return wyn;
}
# | 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... |