# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1047215 | Ahmed57 | Ancient Books (IOI17_books) | C++17 | 77 ms | 15976 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;
vector<int> P;
int vis[1000001];
int mn = 1e9 ,mx = -1e9;
void dfs(int i){
vis[i] = 1;
mn = min(mn,i);
mx = max(mx,i);
if(!vis[P[i]])dfs(P[i]);
}
long long minimum_walk(vector<int> p, int s){
int ans = 0;
int n = p.size();
P.clear();
for(int i = 0;i<n;i++){
vis[i] = 0;
P.push_back(p[i]);
ans+=abs(i-p[i]);
}
vector<pair<int,int>> lol;
for(int i = 0;i<n;i++){
if(!vis[i]){
mn = 1e9 , mx = -1e9;
dfs(i);
lol.push_back({mn,mx});
}
}
sort(lol.begin(),lol.end());
int R = -1e9;
int L = -1e9;
int sz = 0;
vector<pair<int,int>> nah;
for(int i = 0;i<lol.size();i++){
if(R<lol[i].first){
if(R>=0){
nah.push_back({L,R});
}
R = lol[i].second;
L = lol[i].first;
sz++;
}else{
R = max(R,lol[i].second);
}
}
nah.push_back({L,R});
for(int i = nah.size()-1;i>=0;i--){
if(nah[i].first==nah[i].second){
sz--;
}else break;
}
ans+=max(0,(sz-1))*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... |