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;
#define int long long
#define pii pair<int, int>
#define ff first
#define ss second
vector<int> s, t;
int n;
int BruteDP() {
int dp[(1 << n)][n];
for (int conf = 0; conf < (1 << n); conf++) {
for (int i = 0; i < n; i++) {
dp[conf][i] = (conf == (1 << i) ? 0 : 1e18);
}
}
for (int bit = 2; bit <= n; bit++) {
for (int conf = 0; conf < (1 << n); conf++) {
if (__builtin_popcountll(conf) != bit) continue;
for (int i = 0; i < n; i++) {
if (conf & (1 << i)) {
for (int j = 0; j < n; j++) {
if (!bool(conf & (1 << j)) || i == j) continue;
dp[conf][i] = min(dp[conf][i], dp[conf ^ (1 << i)][j] + max(0LL, t[j] - s[i]));
}
}
}
}
}
int ans = 1e18;
for (int i = 0; i < n; i++) ans = min(ans, dp[(1 << n) - 1][i]);
return ans;
}
bool cmp2(pii &lhs, pii &rhs) {
if (lhs.ss != rhs.ss) return lhs.ss < rhs.ss;
return lhs.ff > rhs.ff;
}
long long plan_roller_coaster(vector<int32_t> S, vector<int32_t> T) {
n = S.size(); s.resize(n); t.resize(n);
for (int i = 0; i < n; i++) {
s[i] = S[i]; t[i] = T[i];
}
// for (int i = 0; i < n; i++) cerr << s[i] << ' '; cerr << endl;
// for (int i = 0; i < n; i++) cerr << t[i] << ' '; cerr << endl;
// if (n <= 16) return BruteDP();
pii maxi = {-1, -1};
int maxidx = -1;
vector<pii> v(n);
for (int i = 0; i < n; i++) {
v[i] = {s[i], t[i]};
}
set<int> s;
bool ans = 1;
int cur;
sort(v.begin(), v.end(), greater<pii>());
s.clear();
for (int i = 0; i < n; i++) s.insert(i);
ans = 0;
cur = n - 1;
for (int turns = 0; turns < n; turns++) {
// cerr << cur << ' ';
s.erase(s.find(cur));
if (turns == n - 1) break;
int tar = v[cur].ss;
int lb = 0, rb = n - 1;
while (lb < rb) {
int mid = (lb + rb + 1) >> 1;
if (v[mid].ff >= tar) lb = mid;
else rb = mid - 1;
}
int pos = lb;
set<int>::iterator it = s.upper_bound(pos);
if (it == s.begin()) {
ans = 1;
break;
}
cur = *(--it);
}
if (!ans) return 0;
sort(v.begin(), v.end(), cmp2);
s.clear();
for (int i = 0; i < n; i++) s.insert(i);
ans = 0;
cur = n - 1;
for (int turns = 0; turns < n; turns++) {
// cerr << cur << ' ';
s.erase(s.find(cur));
if (turns == n - 1) break;
int tar = v[cur].ff;
int lb = 0, rb = n - 1;
while (lb < rb) {
int mid = (lb + rb + 1) >> 1;
if (v[mid].ss <= tar) lb = mid;
else rb = mid - 1;
}
int pos = lb;
set<int>::iterator it = s.upper_bound(pos);
if (it == s.begin()) {
ans = 1;
break;
}
cur = *(--it);
}
if (!ans) return 0;
return 1;
}
#undef int
Compilation message (stderr)
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:53:6: warning: variable 'maxi' set but not used [-Wunused-but-set-variable]
53 | pii maxi = {-1, -1};
| ^~~~
railroad.cpp:54:6: warning: unused variable 'maxidx' [-Wunused-variable]
54 | int maxidx = -1;
| ^~~~~~
# | 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... |