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 <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <set>
#include <map>
#include <queue>
#include <cstring>
#include <cmath>
#include <bitset>
#include <iomanip>
#include <functional>
using namespace std;
template<typename T> void uin(T &a, T b) {
if (b < a) a = b;
}
#define all(v) v.begin(), v.end()
const long long INF = 1e18 + 228;
const int OGO = 1000 * 1000 * 1000 + 228;
const int N = 400 * 1000 + 228;
long long dp[(1 << 17) + 17][16];
vector<int> crd;
vector<int> g[N], gr[N];
int pref[N];
bool used[N];
void dfs(int v) {
used[v] = 1;
for (int to : gr[v]) {
if (!used[to]) {
dfs(to);
}
}
}
int Get(int i) {
return lower_bound(all(crd), i) - crd.begin() + 1;
}
void AddEdge(int from, int to) {
from = Get(from);
to = Get(to);
g[from].emplace_back(to);
++pref[from];
--pref[to];
gr[from].emplace_back(to);
gr[to].emplace_back(from);
}
long long plan_roller_coaster(vector<int> s, vector<int> t) {
int n = (int)s.size();
if (n <= 15) {
for (int i = 0; i < (1 << n); ++i) {
for (int j = 0; j < n; ++j) {
dp[i][j] = INF;
}
}
for (int fir = 0; fir < n; ++fir) {
dp[1 << fir][fir] = 0;
}
for (int mask = 0; mask < (1 << n); ++mask) {
for (int last = 0; last < n; ++last) {
if (dp[mask][last] != INF) {
for (int new_last = 0; new_last < n; ++new_last) {
if ((mask >> new_last) & 1) continue;
long long cur = dp[mask][last] + max(0, t[last] - s[new_last]);
uin(dp[mask | (1 << new_last)][new_last], cur);
}
}
}
}
long long answer = INF;
for (int last = 0; last < n; ++last) {
uin(answer, dp[(1 << n) - 1][last]);
}
return answer;
} else {
// subtask 3
crd.clear();
for (int i = 0; i < n; ++i) {
crd.emplace_back(s[i]);
crd.emplace_back(t[i]);
}
crd.emplace_back(OGO);
sort(crd.begin(), crd.end());
crd.erase(unique(crd.begin(), crd.end()), crd.end());
int sz = (int)crd.size();
for (int i = 0; i < n; ++i) {
AddEdge(s[i], t[i]);
}
AddEdge(OGO, 1);
for (int i = 1; i < sz; ++i) {
pref[i] += pref[i - 1];
if (pref[i] > 0) {
return 10;
}
if (pref[i] != 0) {
gr[i].emplace_back(i + 1);
gr[i + 1].emplace_back(i);
}
}
int comps = 0;
for (int i = 1; i <= sz; ++i) {
if (!used[i]) {
dfs(i);
++comps;
}
}
if (comps > 1) {
return 20;
} else {
return 0;
}
}
return 0;
}
# | 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... |