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;
// Input
long long N;
long long S[1 << 18], T[1 << 18];
long long dp[1 << 16][16];
// Graph
bool used[1 << 19];
int cnt[1 << 19];
int col[1 << 19], cntv;
vector<int> ord;
vector<int> X[1 << 19];
vector<int> Y[1 << 19];
class UnionFind {
public:
vector<int> par;
void init(int sz) {
par.resize(sz, -1);
}
int root(int pos) {
if (par[pos] == -1) return pos;
par[pos] = root(par[pos]);
return par[pos];
}
void unite(int u, int v) {
u = root(u);
v = root(v);
if (u != v) par[u] = v;
}
bool same(int u, int v) {
if (root(u) == root(v)) return true;
return false;
}
};
void dfs1(int pos) {
used[pos] = true;
for (int i : X[pos]) {
if (used[i] == true) continue;
dfs1(i);
}
ord.push_back(pos);
}
void dfs2(int pos) {
col[pos] = cntv;
for (int i : Y[pos]) {
if (col[i] >= 1) continue;
dfs2(i);
}
}
long long solve_subtask4() {
long long FinalAns = 0;
// ZAATS
vector<long long> vec;
vec.push_back(0);
vec.push_back(1000000007);
for (int i = 0; i < N; i++) vec.push_back(S[i]);
for (int i = 0; i < N; i++) vec.push_back(T[i]);
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
for (int i = 0; i < N; i++) S[i] = lower_bound(vec.begin(), vec.end(), S[i]) - vec.begin();
for (int i = 0; i < N; i++) T[i] = lower_bound(vec.begin(), vec.end(), T[i]) - vec.begin();
// GRAPH
cnt[0] += 1;
cnt[vec.size() - 1] -= 1;
X[vec.size() - 1].push_back(0);
Y[0].push_back(vec.size() - 1);
for (int i = 0; i < N; i++) X[S[i]].push_back(T[i]);
for (int i = 0; i < N; i++) Y[T[i]].push_back(S[i]);
for (int i = 0; i < N; i++) cnt[S[i]] -= 1;
for (int i = 0; i < N; i++) cnt[T[i]] += 1;
// HANTE
for (int i = 1; i < (int)vec.size(); i++) cnt[i] += cnt[i - 1];
for (int i = 0; i < (int)vec.size(); i++) {
if (cnt[i] <= -1) {
FinalAns -= 1LL * cnt[i] * (long long)(vec[i + 1] - vec[i]);
X[i + 1].push_back(i);
Y[i].push_back(i + 1);
}
if (cnt[i] >= 1) {
X[i].push_back(i + 1);
Y[i + 1].push_back(i);
}
}
// SCC
for (int i = 0; i < (int)vec.size(); i++) {
if (used[i] == true) continue;
dfs1(i);
}
for (int i = ord.size() - 1; i >= 0; i--) {
if (col[ord[i]] >= 1) continue;
cntv++;
dfs2(ord[i]);
}
// PRFNL
vector<tuple<long long, int, int>> tup;
for (int i = 0; i < (int)vec.size() - 1; i++) {
if (cnt[i] >= 1) continue;
tup.push_back(make_tuple(vec[i + 1] - vec[i], col[i], col[i + 1]));
}
sort(tup.begin(), tup.end());
// FINAL
UnionFind UF; UF.init(vec.size() + 2);
for (int i = 0; i < (int)tup.size(); i++) {
long long cost = get<0>(tup[i]), a1 = get<1>(tup[i]), a2 = get<2>(tup[i]);
if (UF.same(a1, a2) == false) {
UF.unite(a1, a2);
FinalAns += cost;
}
}
return FinalAns;
}
long long solve_subtask2() {
// PRCLC
for (int i = 0; i < (1 << N); i++) {
for (int j = 0; j < N; j++) dp[i][j] = (1LL << 60);
}
for (int i = 0; i < N; i++) dp[1 << i][i] = 0;
// CALC
for (int i = 0; i < (1 << N); i++) {
for (int j = 0; j < N; j++) {
if (dp[i][j] == (1LL << 60)) continue;
for (int k = 0; k < N; k++) {
if ((i / (1 << k)) % 2 == 1) continue;
long long cost = max(0LL, T[j] - S[k]);
dp[i + (1 << k)][k] = min(dp[i + (1 << k)][k], dp[i][j] + cost);
}
}
}
// FINAL
long long Answer = (1LL << 60);
for (int i = 0; i < N; i++) Answer = min(Answer, dp[(1 << N) - 1][i]);
return Answer;
}
int cnts = 0;
long long plan_roller_coaster(vector<int> s, vector<int> t) {
N = s.size();
for (int i = 0; i < N; i++) S[i] = s[i];
for (int i = 0; i < N; i++) T[i] = t[i];
cnts += 1;
return solve_subtask4();
}
# | 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... |