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);
}
struct MS {
std::map <std::pair <int, int>, int> mp;
inline void insert(std::pair <int, int> p) {
mp[p]++;
}
inline void erase(std::pair <int, int> p) {
auto it = mp.find(p);
if (!(--it->second)) {
mp.erase(it);
}
}
inline std::pair <int, int> begin() {
return mp.begin()->first;
}
};
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);
}
MS st, ts;
for (int i = 0; i < n; i++) {
st.insert({ s[i], t[i] });
st.insert({ t[i], s[i] });
}
bool fl = false;
for (int i = 0; i < n - 1; i++) {
auto p = st.begin();
st.erase(p);
//std::cerr << i << ": " << p.first << ", " << p.second << std::endl;
ts.erase({ p.second, p.first });
std::pair <int, int> pp;
if ((pp = ts.begin()).first > p.first) {
if (fl) {
return 1;
}
p.first = 1 << 30;
st.erase({ pp.second, pp.first });
ts.erase(pp);
st.insert(p);
ts.insert({ p.second, p.first });
fl = true;
continue;
}
p.first = p.second;
st.erase({ pp.second, pp.first });
ts.erase(pp);
st.insert(p);
ts.insert({ p.second, p.first });
}
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... |