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;
const int MX=4e5+5;
long long deg[MX];
int vis[MX];
vector<int> a[MX];
int p[MX];
int f(int x){
return x==p[x] ? x:p[x]=f(p[x]);
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
int n = (int) s.size();
vector<long long> v;
for(int i=0 ; i<n ; i++){
v.push_back(s[i]);
v.push_back(t[i]);
}
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
for(int i=0 ; i<n ; i++){
deg[lower_bound(v.begin(),v.end(),s[i])-v.begin()]++;
deg[lower_bound(v.begin(),v.end(),t[i])-v.begin()]--;
a[lower_bound(v.begin(), v.end(), s[i]) - v.begin()].push_back(lower_bound(v.begin(), v.end(), t[i]) - v.begin());
}
if(v.size()==1)return 0;
long long ans=0;
if(deg[0]>1){
a[1].push_back(0);
ans+=(v[1]-v[0])*(deg[0]-1);
}
if(deg[0]<=0)a[0].push_back(1);
deg[1]+=deg[0]-1;
for(int i=1 ; i<(int)v.size()-1 ; i++){
if(deg[i]<0){
a[i].push_back(i+1);
}
if(deg[i]>0){
a[i+1].push_back(i);
ans+=deg[i]*(v[i+1]-v[i]);
}
deg[i+1]+=deg[i];
}
for(int i=0 ; i<v.size() ; i++){
p[i]=i;
}
for(int i=0 ; i<v.size() ; i++){
for(auto x: a[i]){
p[f(x)]=f(i);
}
}
vector<pair<long long,int>> b;
for(int i=0 ; i<v.size()-1 ; i++){
b.push_back({v[i+1]-v[i],i});
}
sort(b.begin(),b.end());
for(auto x: b){
int i=x.second;
if(f(i)!=f(i+1)){
p[f(i)]=f(i+1);
ans+=v[i+1]-v[i];
}
}
return ans;
}
Compilation message (stderr)
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:50:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int i=0 ; i<v.size() ; i++){
| ~^~~~~~~~~
railroad.cpp:53:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i=0 ; i<v.size() ; i++){
| ~^~~~~~~~~
railroad.cpp:59:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i=0 ; i<v.size()-1 ; i++){
| ~^~~~~~~~~~~
# | 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... |