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;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vii;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vii> vvii;
typedef vector<vll> vvll;
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;
#define ffor(i, a, b) for (ll i = (a); i < (ll)(b); i++)
#define fford(i, a, b) for (ll i = (a); i > (ll)(b); i--)
#define rep(i, n) ffor(i, 0, n)
#define forin(x, a) for (auto &x: a)
#define all(a) a.begin(), a.end()
#define cost(a, b) max(0, t[a] - s[b])
#define ocost(a, b) cost(order[a], order[b])
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
int n = (int) s.size();
ll res = 1LL << 62;
vii used(n);
ll curr = 0;
vii order;
function<void(int)> dfs;
dfs = [&](int p) {
if (curr > res) {
return;
}
if ((int)order.size() == n) {
res = min(res, curr);
return;
}
int speed;
if (p == -1) {
speed = 1;
} else {
speed = t[p];
}
rep(i, n) {
if (used[i]) {
continue;
}
if (order.size() == 2 && ocost(0, 1) + cost(order[1], i) > ocost(1, 0) + cost(order[0], i)) {
continue;
}
if (order.size() > 2 && ocost(order.size() - 3, order.size() - 2) + ocost(order.size() - 2, order.size() - 1) + cost(order.back(), i) > ocost(order.size() - 3, order.size() - 1) + ocost(order.size() - 1, order.size() - 2) + cost(order[order.size() - 2], i)) {
continue;
}
used[i] = true;
curr += max(0, speed - s[i]);
order.emplace_back(i);
dfs(i);
order.pop_back();
curr -= max(0, speed - s[i]);
used[i] = false;
}
};
dfs(-1);
return res;
}
# | 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... |