# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
906806 | mathnetic | Roller Coaster Railroad (IOI16_railroad) | C++17 | 505 ms | 33172 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 <cassert>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include "railroad.h"
using namespace std;
typedef pair<int,int> ii;
typedef long long ll;
unordered_map<int, int> uf;
int fnd(int x){
if(uf[x]==0) uf[x]=x;
if(x==uf[x]) return x;
else return uf[x]=fnd(uf[x]);
}
void un(int x, int y){
uf[fnd(x)]=fnd(y);
}
bool cmp(ii a, ii b){
return (a.second-a.first)<(b.second-b.first);
}
ll plan_roller_coaster(vector<int> s, vector<int> t){
ll sol=0;
s.push_back(1e9);
t.push_back(1);
int n = (int) s.size();
vector<ii> e, ed;
for(int i=0;i<n;++i){
un(s[i], t[i]);
e.emplace_back(s[i], 1);
e.emplace_back(t[i], -1);
}
sort(e.begin(), e.end());
int b=0;
for(int i=0, j=0;i<e.size();i=j){
for(;j<e.size() && e[j].first==e[i].first;++j){
b+=e[j].second;
}
if(j<e.size()){
if(b>0){
sol+=(ll)b*(e[j].first-e[i].first);
}
if(b==0){
ed.emplace_back(e[i].first, e[j].first);
}else{
un(e[j].first, e[i].first);
}
}
}
sort(ed.begin(), ed.end(), cmp);
for(auto i:ed){
if(fnd(i.first)!=fnd(i.second)){
sol+=i.second-i.first;
un(i.first, i.second);
}
}
return sol;
}
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... |