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;
#include "railroad.h"
#define int long long
int pr[400001];
int find(int x){
if(x==pr[x])return x;
return pr[x] = find(pr[x]);
}
bool mergegroup(int a,int b){
a = find(a); b = find(b);
if(a==b)return 0;
pr[a] = b;
return 1;
}
long long plan_roller_coaster(vector<int32_t> s, vector<int32_t> t){
set<int> ss;
for(auto i:s)ss.insert(i);
for(auto i:t)ss.insert(i);
map<int,int> sav;
int rev[s.size()+t.size()+1];
int cnt = 0;
for(auto i:ss){
sav[i] = ++cnt;
rev[cnt] = i;
}
int in[cnt+1] = {0};
int out[cnt+1] = {0};
for(int i = 1;i<=cnt;i++)pr[i] = i;
for(int i = 0;i<s.size();i++){
in[sav[s[i]]]++;
out[sav[t[i]]]++;
mergegroup(sav[s[i]],sav[t[i]]);
}
in[cnt]++;
out[1]++;
mergegroup(1,cnt);
long long ans = 0;
vector<array<int,3>> ed;
for(int i = cnt;i>1;i--){
if(out[i]>in[i]){
ans+=(out[i]-in[i])*(rev[i]-rev[i-1]);
out[i-1]+=(out[i]-in[i]);
mergegroup(i,i-1);
}else if(in[i]>out[i]){
in[i-1]+=(in[i]-out[i]);
mergegroup(i,i-1);
}
ed.push_back({rev[i]-rev[i-1],i,i-1});
}
sort(ed.begin(),ed.end());
for(auto i:ed){
if(mergegroup(i[1],i[2]))ans+=i[0];
}
return ans;
}
Compilation message (stderr)
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:30:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i = 0;i<s.size();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... |