# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
319370 | VROOM_VARUN | Roller Coaster Railroad (IOI16_railroad) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
ID: varunra2
LANG: C++
TASK: railroad
*/
#include<bits/stdc++.h>
#include "railroad.h"
using namespace std;
#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif
#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second
const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions
struct dsu{
VI par;
VI siz;
void init(int n) {
siz.assign(n, 1);
par.resize(n);
iota(all(par), 0);
}
int find(int x) {
while(par[x] != par[par[x]]) par[x] = par[par[x]];
return par[x];
}
bool merge(int x, int y) {
x = find(x);
y = find(y);
if(x == y) return false;
if(siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
};
ll plan_roller_coaster(VI& s, VI& t) {
s.PB(INF);
t.PB(1);
int n = sz(s);
dsu dsu1;
dsu1.init(n);
VVI events;
VVI edgs;
for(int i = 0; i < n; i++) {
events.PB({s[i], 1, i});
events.PB({t[i], -1, i});
}
sort(all(events));
ll ret = 0;
ll tot = 0;
for(int i = 0; i < sz(events) - 1; i++) {
tot += events[i][1];
ll dist = events[i + 1][0] - events[i][0];
ret += dist * max(tot, 0ll);
edgs.PB({(int)dist, events[i][2], events[i + 1][2]});
if(dist == 0 or tot != 0) {
dsu1.merge(events[i][2], events[i + 1][2]);
}
}
sort(all(edgs));
for(auto& x: edgs) {
if(dsu1.merge(x[1], x[2])) ret += x[0];
}
return ret;
}
// int main() {
// #ifndef ONLINE_JUDGE
// freopen("railroad.in", "r", stdin);
// freopen("railroad.out", "w", stdout);
// #endif
// cin.sync_with_stdio(0); cin.tie(0);
// return 0;
// }