This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of Allah
#include <bits/stdc++.h>
#include "railroad.h"
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef complex<ld> cld;
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
#define F first
#define S second
#define pb push_back
#define sep ' '
#define endl '\n'
#define Mp make_pair
#define kill(x) cout << x << '\n', exit(0)
#define set_dec(x) cout << fixed << setprecision(x);
#define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 2e5 + 4;
const ll oo = 1e18 + 4;
int n;
pll A1[maxn], A2[maxn];
int p[maxn], sz[maxn], num;
vector<pair<ll, pii>> E;
int get(int a) {
return (p[a] == a) ? a : p[a] = get(p[a]);
}
void merge(int a, int b) {
a = get(a); b = get(b);
if (a == b) return ;
if (sz[a] > sz[b]) swap(a, b);
p[a] = b; sz[b] += sz[a]; sz[a] = 0; num--;
}
ll plan_roller_coaster(vector<int> s, vector<int> t) {
n = len(s);
for (int i = 0; i < n; i++) {
A1[i] = Mp(s[i], i); A2[i] = Mp(t[i], i);
}
A1[n] = Mp(oo, n); A2[n] = Mp(-oo, n); n++;
sort(A1, A1 + n); sort(A2, A2 + n);
iota(p, p + n, 0); fill(sz, sz + n, 1); num = n;
ll ans = 0;
for (int i = 0; i < n; i++) {
merge(A1[i].S, A2[i].S);
ans += max(0ll, A2[i].F - A1[i].F);
if (i + 1 < n) {
ll w = max(0ll, A2[i + 1].F - A1[i].F) + max(0ll, A2[i].F - A1[i + 1].F);
w -= max(0ll, A2[i].F - A1[i].F) + max(0ll, A2[i + 1].F - A1[i + 1].F);
E.pb(Mp(w, Mp(A1[i].S, A2[i + 1].S)));
}
}
sort(all(E));
for (auto f : E) {
if (num == 1) break;
int u = f.S.F, v = f.S.S; ll w = f.F;
if (get(u) != get(v)) {
merge(u, v); ans += w;
}
}
return ans;
}
# | 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... |