# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
542828 | zggf | 고대 책들 (IOI17_books) | C++14 | 1 ms | 212 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;
vector<int> parent, ranga;
int find(int x){
if(parent[x]==-1) return x;
parent[x] = find(parent[x]);
return parent[x];
}
void join(int a, int b){
a = find(a);
b = find(b);
if(a==b) return;
if(ranga[a]>ranga[b]){
parent[b] = a;
}else if(ranga[b]>ranga[a]){
parent[a] = b;
}else{
parent[b] = a;
ranga[a]++;
}
}
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
vector<int> odw(n);
parent.resize(n, -1);
ranga.resize(n);
long long wyn = 0;
vector<pair<int, int>> inp;
for(int i = 0; i < n; i++){
int pt = i;
if(!odw[pt]){
if(p[pt]!=pt){
int mini = 1e9;
int maxi = -1e9;
while(!odw[pt]){
mini = min(mini, pt);
maxi = max(maxi, pt);
odw[pt] = true;
wyn+=abs(p[pt]-pt);
pt = p[pt];
}
inp.push_back({mini, maxi});
}else{
odw[pt] = true;
}
}
}
inp.push_back({s, s});
sort(inp.begin(), inp.end());
int dal = 0;
for(int i = 0; i < inp.size()-1; i++){
dal = max(dal, inp[i].second);
while(inp[i+1].first<dal){
i++;
dal = max(dal, inp[i].second);
if(i==n-1) break;
}
if(i<n-1){
wyn+=2*(inp[i+1].first-dal);
}
}
return wyn;
}
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... |