# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
139335 | KLPP | Roller Coaster Railroad (IOI16_railroad) | C++14 | 350 ms | 33880 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 "railroad.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef pair<lld,lld> pii;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define trav(a,v) for(auto a:v)
class DSU{
int parent[1000000];
int size[1000000];
public:
void init(int n){
rep(i,0,n){
parent[i]=i;
size[i]=1;
}
}
int root(int node){
if(parent[node]==node)return node;
parent[node]=root(parent[node]);
return parent[node];
}
bool merge(int a, int b){
a=root(a);
b=root(b);
if(a==b)return false;
if(size[b]>size[a])swap(a,b);
parent[b]=a;
size[a]+=size[b];
return true;
}
};
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
s.push_back(1000000001);
t.push_back(1);
int n = (int) s.size();
vector<lld >v;
rep(i,0,n){
v.push_back(s[i]);
v.push_back(t[i]);
}
sort(v.begin(),v.end());
vector<lld> comp;
rep(i,0,v.size()){
if(comp.size()==0 || v[i]!=comp[comp.size()-1]){
comp.push_back(v[i]);
}
}
/*rep(i,0,comp.size())cout<<comp[i]<<" ";
cout<<endl;*/
lld edges[n][2];
lld sections[comp.size()];
rep(i,0,comp.size())sections[i]=0;
rep(i,0,n){
int lo,hi;
lo=-1;hi=comp.size();
while(hi-lo>1){
int mid=(hi+lo)/2;
if(comp[mid]<=s[i])lo=mid;
else hi=mid;
}
edges[i][0]=lo;
lo=-1;hi=comp.size();
while(hi-lo>1){
int mid=(hi+lo)/2;
if(comp[mid]<=t[i])lo=mid;
else hi=mid;
}
edges[i][1]=lo;
rep(j,0,2){
if(edges[i][j]>0){
sections[edges[i][j]-1]+=1-2*j;
}
}
//cout<<edges[i][0]<<" "<<edges[i][1]<<endl;
}
for(int i=comp.size()-1;i>0;i--){
sections[i-1]+=sections[i];
}
vector<pii> edgelist;
lld ans=0;
DSU *D=new DSU();
D->init(comp.size());
rep(i,0,comp.size()-1){
if(sections[i]==0){
edgelist.push_back(pii(comp[i+1]-comp[i],i));
}else D->merge(i,i+1);
if(sections[i]<0){
ans-=(comp[i+1]-comp[i])*sections[i];
}
}
rep(i,0,n){
D->merge(edges[i][0],edges[i][1]);
}
//cout<<ans<<endl;
sort(edgelist.begin(),edgelist.end());
rep(i,0,edgelist.size()){
if(D->merge(edgelist[i].second,edgelist[i].second+1)){
ans+=edgelist[i].first;
}
}
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... |