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 ll;
vector<int> comp[400000];
int id[400000];
int sz[400000];
bool mg(int a, int b){
if(id[a]==id[b]){
return false;
}
int ca = id[a];
int cb = id[b];
if(sz[ca] > sz[cb]){
swap(ca,cb);
}
for(int i = 0; i<comp[ca].size(); i++){
comp[cb].push_back(comp[ca][i]);
id[comp[ca][i]] = cb;
sz[cb]++;
}
comp[ca].clear();
return true;
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
int n = (int) s.size();
vector<int> all;
int point = 0;
map<int, int> m;
for(int i = 0; i<n; i++){
all.push_back(s[i]);
all.push_back(t[i]);
}
sort(all.begin(),all.end());
vector<int> rev;
for(int i = 0; i<2*n; i++){
if(i==0 || all[i-1]!=all[i]){
m[all[i]] = point++;
rev.push_back(all[i]);
}
}
int nn = point;
int bal[nn];
//in - out
// bal = 3 -> (((
for(int i= 0; i<n; i++){
s[i] = m[s[i]];
t[i] = m[t[i]];
}
for(int i = 0; i<nn; i++){
bal[i] = 0;
}
for(int i = 0; i<n; i++){
bal[s[i]]--;
bal[t[i]]++;
}
vector<pair<int, bool> > li;
//ind, open
vector<pair<int, int> > did;
for(int i = 0; i<nn; i++){
if(bal[i]>0){
for(int j = 1; j<=bal[i]; j++){
li.push_back(make_pair(i,true));
}
}
else{
for(int j = -1; j>=bal[i]; j--){
if(li.size()>0 && li[li.size()-1].second){
did.push_back(make_pair(li[li.size()-1].first,i));
li.pop_back();
}
else{
li.push_back(make_pair(i,false));
}
}
}
}
int p1 = 1;
int p2 = li.size()-2;
ll ans = 0LL;
if(p1<p2){
did.push_back(make_pair(li[p1].first,li[p2].first));
}
sort(did.begin(),did.end());
while(p1<p2){
ans += (ll)(rev[li[p2].first] - rev[li[p1].first]);
p1++;
p2--;
}
bool imp[nn];
for(int i = 0; i<nn; i++){
imp[i] = false;
comp[i].push_back(i);
id[i] = i;
sz[i] = 1;
}
for(int i = 0; i<n; i++){
imp[s[i]] = true;
imp[t[i]] = true;
}
vector<int> allimp;
for(int i = 0; i<nn; i++){
if(imp[i]){
allimp.push_back(i);
}
}
int st = -1;
int en = -1;
for(int i = 0; i<did.size(); i++){
if(st==-1){
st = did[i].first;
en = did[i].second;
}
else if(did[i].first <= en){
en = max(en,did[i].second);
}
else{
for(int j = st+1; j<=en; j++){
mg(st,j);
}
st = did[i].first;
en = did[i].second;
}
}
if(st!=-1){
for(int j = st+1; j<=en; j++){
mg(st,j);
}
}
for(int i = 0; i<n; i++){
mg(s[i],t[i]);
}
vector<pair<ll, pair<int, int> > > eds;
for(int i = 1; i<allimp.size(); i++){
eds.push_back(make_pair(rev[allimp[i]]-rev[allimp[i-1]],make_pair(allimp[i-1],allimp[i])));
}
sort(eds.begin(),eds.end());
for(int i = 0; i<eds.size(); i++){
bool use = mg(eds[i].second.first,eds[i].second.second);
if(use){
ans += eds[i].first;
}
}
return ans;
}
Compilation message (stderr)
railroad.cpp: In function 'bool mg(int, int)':
railroad.cpp:18:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i<comp[ca].size(); i++){
~^~~~~~~~~~~~~~~~
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:111:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i<did.size(); i++){
~^~~~~~~~~~~
railroad.cpp:136:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i<allimp.size(); i++){
~^~~~~~~~~~~~~~
railroad.cpp:140:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i<eds.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... |