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>
#define fr first
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define sc second
#define all(s) s.begin(), s.end()
#define rc(s) return cout << s, 0
using namespace std;
const int nmax = 400005;
int n, par[nmax], viz[nmax], componente;
long long ans;
unordered_map<int,vector<int>>mp_in;
unordered_map<int,vector<int>>mp_out;
unordered_map<int,vector<int>>mp_intz;
unordered_map<int,vector<int>>mp_outtz;
vector<long long>compress;
vector<pair<long long,pair<int,int>>>edges;
void dfs(int s){
//cout << "CHECK " << s << ' ' << componente << '\n';
viz[s] = componente;
for(auto it : mp_out[compress[s]]){
auto it2 = lower_bound(all(compress), it) - compress.begin();
if(!viz[it2]) dfs(it2);
}
}
int findpar(int x){
if(x != par[x]){
par[x] = findpar(par[x]);
}
return par[x];
}
long long plan_roller_coaster(vector<int> s, vector<int> t) {
int maxi = 0;
for(int i=0;i<(int)s.size();i++){
maxi = max({maxi, s[i], t[i]});
}
s.push_back(maxi);
t.push_back(1);
n = (int)s.size();
for(int i=0;i<n;i++){
// cout << "BLEAAEAAA" << s[i] << ' ' << t[i] << '\n';
mp_out[s[i]].push_back(t[i]);
mp_in[t[i]].push_back(s[i]);
compress.push_back(s[i]);
compress.push_back(t[i]);
}
sort(all(compress));
compress.resize(unique(all(compress)) - compress.begin());
int gratis = 0, plata = 0;
for(int i=0;i<(int)compress.size();i++){
// cout << ans << ' ' << cu_plata << '\n';
auto it = compress[i];
// cout << "CHECK " << it << '\n';
if(i >= 1){
if(gratis > 0) mp_outtz[compress[i-1]].push_back(compress[i]);
if(plata > 0) mp_outtz[compress[i]].push_back(compress[i-1]);
ans += plata * (compress[i] - compress[i - 1]);
}
int x = mp_out[it].size() - mp_in[it].size();
// cout << "CHECK " << x << ' ' << mp_out[it].size() << ' ' << mp_in[it].size() << '\n';
if(x > 0){
int lol = min(gratis, x);
gratis -= lol;
x -= lol;
plata += x;
}
else if(x < 0){
int y = abs(x);
int lol = min(y, plata);
plata -= lol;
y -= lol;
gratis += y;
}
// cout << "BLEA " << gratis << '\n';
// cout << "BLEA " << plata << '\n';
// cout << ans << ' ' << it << ' ' << gratis << ' ' << plata << '\n';
}
//cout << ans << '\n';
//assert(gratis == 0 && cu_plata == 0);
for(auto it : compress){
for(auto it1 : mp_outtz[it]){
mp_out[it].push_back(it1);
}
}
for(int i=0;i<(int)compress.size();i++){
if(!viz[i]){
++componente;
dfs(i);
}
}
// cout << ans << ' ' << componente << '\n';
for(int i=1;i<=componente;i++) par[i] = i;
for(int i=1;i<(int)compress.size();i++){
edges.push_back({compress[i] - compress[i-1], {viz[i-1], viz[i]}});
}
sort(all(edges));
for(auto it : edges){
int x = findpar(it.sc.fr);
int y = findpar(it.sc.sc);
if(x != y){
ans += it.fr;
par[x] = y;
}
}
return ans;
}
// 1. Solve the problem
// 2. ???
// 3. Profit
# | 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... |