이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//Pantyhose(black) + glasses = infinity
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " = " << x << '\n';
#define BP() cerr << "OK!\n";
#define PR(A, n) {cerr << #A << " = "; for (int _=1; _<=n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define PR0(A, n) {cerr << #A << " = "; for (int _=0; _<n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define FILE_NAME "data"
const int64_t INF = 1e18;
int n;
vector<pair<int, int> > a;
int64_t f[1<<16][16];
int64_t dp(int mask, int last) {
if (__builtin_popcount(mask)==n)
return 0;
if (f[mask][last]>-1)
return f[mask][last];
int64_t res = INF;
for (int i=0; i<n; ++i) {
if (((mask>>i) & 1)==0) {
int new_mask = mask | (1<<i);
// if (mask==11 && last==1 && i==2)
// debug();
res = min(res, dp(new_mask, i) + max(0, a[last].second - a[i].first));
}
}
return f[mask][last] = res;
}
bool cmp(pair<int, int> l1, pair<int, int> l2) {
return l1 > l2;
}
bool check() {
vector<int> L(n);
for (int i=0; i<n; ++i)
L[i] = upper_bound(a.begin(), a.end(), make_pair(a[i].second, 0), cmp) - a.begin() - 1;
// PR0(L, L.size());
set<int> avail;
for (int i=0; i<n; ++i)
avail.insert(i);
int cur = n-1;
avail.erase(n-1);
for (int i=0; i+1<n; ++i) {
// debug(cur);
auto it = avail.upper_bound(L[cur]);
if (it==avail.begin())
return false;
--it;
cur = *it;
avail.erase(*it);
}
return true;
}
int64_t plan_roller_coaster(vector<int> s, vector<int> t) {
n = s.size();
a.resize(n);
for (int i=0; i<n; ++i)
a[i] = {s[i], t[i]};
sort(a.begin(), a.end(), greater<pair<int, int> >());
if (n<=16) {
memset(f, -1, sizeof(f));
// debug(dp(1, 0));
int64_t res = INF;
for (int i=0; i<n; ++i)
res = min(res, dp(1<<i, i));
return res;
}
return check();
}
# | 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... |