# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
635352 | Bruteforceman | Roller Coaster Railroad (IOI16_railroad) | C++11 | 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.
#include <bits/stdc++.h>
long long sub12(int n, std::vector <int> s, std::vector <int> t) {
t.push_back(1);
long long dp[1 << 16][17];
for (int mask = 0; mask < (1 << 16); mask++) {
for (int i = 0; i < 17; i++) {
dp[mask][i] = 1LL << 60;
}
}
for (int i = 0; i < 17; i++) {
dp[(1 << n) - 1][i] = 0;
}
auto f = [&] (auto&& self, int mask, int last) -> long long {
if (dp[mask][last] < (1LL << 60)) {
return dp[mask][last];
}
for (int i = 0; i < n; i++) {
if (mask & (1 << i)) {
continue;
}
dp[mask][last] = std::min(dp[mask][last], self(self, mask | (1 << i), i) + std::max(0, t[last] - s[i]));
}
return dp[mask][last];
};
return f(f, 0, n);
}
long long plan_roller_coaster(std::vector <int> s, std::vector <int> t) {
int n = s.size();
if (n <= 16) {
return sub12(n, s, t);
}
std::vector <int> ep { 0, (int) 1e9 };
for (int i = 0; i < n; i++) {
ep.push_back(s[i]);
ep.push_back(t[i]);
}
std::sort(ep.begin(), ep.end());
{
std::vector <int> tmp;
for (int x : ep) {
if (tmp.empty() || x != tmp.back()) {
tmp.push_back(x);
}
}
ep = tmp;
}
std::vector <std::vector <int>> g(ep.size()), h(ep.size());
std::vector <int> din(ep.size(), 0), dou(ep.size(), 0);
g[(int) ep.size() - 1].push_back(0);
h[0].push_back((int) ep.size() - 1);
din[0]++;
dou[(int) ep.size() - 1]++;
for (int i = 0; i < n; i++) {
int u = std::lower_bound(ep.begin(), ep.end(), s[i]) - ep.begin();
int v = std::lower_bound(ep.begin(), ep.end(), t[i]) - ep.begin();
g[u].push_back(v);
h[v].push_back(u);
din[v]++;
dou[u]++;
}
for (int i = 0; i < (int) ep.size() - 1; i++) {
if (din[i] < dou[i]) {
return 1;
}
if (din[i] > dou[i]) {
g[i].push_back(i + 1);
h[i + 1].push_back(i);
din[i + 1] += din[i] - dou[i];
dou[i] += din[i] - dou[i];
}
}
auto dfs = [&] (auto&& self, int node, const std::vector <std::vector <int>>& gg, std::vector <bool>& vis) -> int {
int r = 1;
vis[node] = true;
for (int x : gg[node]) {
if (!vis[x]) {
r += self(self, x, gg, vis);
}
}
return r;
};
std::vector <bool> vis1(ep.size(), false);
std::vector <bool> vis2(ep.size(), false);
if (dfs(dfs, 0, g, vis1) < (int) ep.size() || dfs(dfs, 0, h, vis2) < (int) ep.size()) {
return 1;
}
return 0;
}