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 FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define f first
#define s second
#define pb push_back
typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<pii> vii;
const int MAXN = 1000100;
const ll INF = 1e16;
int pref[MAXN];
int par[MAXN];
ll balance[MAXN];
int find (int a) {return par[a] = par[a]==a ? a : find(par[a]);}
bool same (int a, int b) {return find(a) == find(b);}
void unite (int a, int b) {
a = find(a), b = find(b);
if (a == b) return;
par[b] = a;
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
int n = (int) s.size();
s.pb((int)1000 * 1000 * 1000);
t.pb(1);
set<int> cur;
map<int, int> toId, toCoords;
FOR(i, 0, n) cur.insert(s[i]);
FOR(i, 0, n) cur.insert(t[i]);
int curId = 0;
for (int x : cur) {
toId[x] = ++curId;
toCoords[curId] = x;
}
FOR(i, 0, curId+5) par[i] = i;
FOR(i, 0, n) s[i] = toId[s[i]];
FOR(i, 0, n) t[i] = toId[t[i]];
//FOR(i, 0, n) cout << " i = " << i << " s[i] = " << s[i] << " t[i] = " << t[i] << endl;
FOR(i, 0, n) pref[s[i]]++, pref[t[i]]--;
// FOR(i, 1, curId) cout <<" i = " << i<< " pref: " << pref[i] << endl;
int ss = 0;
FOR(i, 1, curId-1) {
ss += pref[i];
balance[i] = ss;
// cout << " i = " << i << " balance: " << balance[i] << endl;
}
FOR(i, 0, n) unite(s[i], t[i]);
ll sum = 0ll;
FOR(i, 1, curId-1) {
if (balance[i] > 0) {
unite(i, i+1);
sum += balance[i] * (toCoords[i+1] - toCoords[i]);
}
if (balance[i] < 0) unite(i, i+1);
}
vector<pair<ll, pii>> edges;
FOR(i, 1, curId-1) {
if (!same(i, i+1)) {
edges.pb({(toCoords[i+1] - toCoords[i]), {i, i+1}});
}
}
sort(edges.begin(), edges.end());
for (auto e : edges) {
ll w = e.f;
int u = e.s.f, v = e.s.s;
if (!same (u,v)) {
sum += w;
unite(u,v);
}
}
return sum;
}
# | 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... |