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;
#define ll long long
const int MX = 2e5 + 5;
const int inf = 1e9 + 69;
int par[MX];
int find(int nw){
return par[nw] = (par[nw] == nw ? nw : find(par[nw]));
}
bool uni(int u, int v){
u = find(u); v = find(v);
par[u] = v; return (u != v);
}
ll plan_roller_coaster(vector<int> s, vector<int> t){
int n = (int)s.size();
for(int i = 0; i < n; i ++) par[i] = i;
vector<pair<int, pair<int, int> > > li, edges;
for(int i = 0; i < n; i ++){ // edge si -> ti
li.push_back({s[i], {1, i}});
li.push_back({t[i], {-1, i}});
} // edge inf -> 1
li.push_back({inf, {1, n}});
li.push_back({1, {-1, n}});
sort(li.begin(), li.end());
int balance = 0;
ll ans = 0ll;
for(int i = 0; i + 1 < li.size(); i ++){
balance += li[i].second.first;
ans += max(0, balance) * 1ll * (li[i + 1].first - li[i].first);
edges.push_back({li[i + 1].first - li[i].first, {li[i + 1].second.second, li[i].second.second}});
if(li[i + 1].first == li[i].first || balance != 0)
uni(li[i + 1].second.second, li[i].second.second);
}
sort(edges.begin(), edges.end());
for(auto i : edges) if(uni(i.second.first, i.second.second)) ans += i.first;
return ans;
}
Compilation message (stderr)
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:34:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int i = 0; i + 1 < li.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... |