이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "railroad.h"
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fi first
#define se second
#define pll pair<long long, long long>
#define pii pair<int, int>
using namespace std;
const ll INF = 1e18;
int n;
bool cmp(pii x, pii y) {
return x.se > y.se;
}
ll case3(vector <int> s, vector <int> t) {
vector <pii> s1(n);
vector <int> pos(n), l(n);
for (int i = 0; i < n; i++)
s1[i] = {s[i], i};
sort(s1.begin(), s1.end());
for (int i = 0; i < n; i++)
pos[s1[i].se] = i;
// for (auto to : s1)
// cout << to.fi << " " << to.se << endl;
// cout << endl;
for (int i = 0; i < n; i++) {
int L, R, M;
L = 0; R = n - 1;
l[i] = n;
while (L <= R) {
M = (L + R) / 2;
if (s1[M].fi >= t[i]) {
l[i] = min(l[i], M);
R = M - 1;
} else
L = M + 1;
}
// cout << i << " " << t[i] << " " << l[i] << endl;
}
// return 0;
vector < vector <pii> > lhere(n);
for (int i = 0; i < n; i++)
if (l[i] < n)
lhere[l[i]].pb({i, pos[i]});
for (int i = 0; i < n; i++)
sort(lhere[i].begin(), lhere[i].end(), cmp);
set <int> notvis = {};
for (int i = 0; i < n; i++)
notvis.insert(i);
vector <int> nx(n, -1);
set <int>::iterator it;
for (int i = 0; i < n; i++) {
for (auto to : lhere[i]) {
// cout << "Cur " << to.fi << " " << to.se << endl;
int u = to.fi, p = to.se;
it = notvis.lower_bound(i);
if (it == notvis.end())
continue;
int val = *it;
if (s1[val].se == u) {
it = notvis.lower_bound(val + 1);
}
if (it == notvis.end())
continue;
val = *it;
nx[u] = s1[val].se;
notvis.erase(val);
}
}
if (notvis.size() == 1)
return 0;
return 1;
}
ll plan_roller_coaster(vector <int> s, vector <int> t) {
n = s.size();
if (n > 16) {
return case3(s, t);
}
vector < vector <ll> > w;
vector < vector <ll> > dp;
w.resize(n);
for (int i = 0; i < n; i++) {
w[i].resize(n);
for (int j = 0; j < n; j++) {
if (i == j)
continue;
w[i][j] = max(0, t[i] - s[j]);
}
}
dp.resize((1 << n));
for (int mask = 0; mask < (1 << n); mask++) {
dp[mask].resize(n);
for (int i = 0; i < n; i++)
dp[mask][i] = INF;
}
for (int i = 0; i < n; i++)
dp[0][i] = 0;
vector <int> b;
for (int mask = 1; mask < (1 << n); mask++) {
b.clear();
for (int i = 0; i < n; i++)
if (1 & (mask >> i))
b.pb(i);
if (b.size() == 1) {
dp[mask][b[0]] = 0;
continue;
}
for (int i = 0; i < b.size(); i++)
for (int j = 0; j < b.size(); j++) {
if (i == j)
continue;
int mask1 = (mask ^ (1 << b[i]));
dp[mask][b[i]] = min(dp[mask][b[i]], dp[mask1][b[j]] + w[b[j]][b[i]]);
}
}
ll ans = INF;
for (int i = 0; i < n; i++)
ans = min(ans, dp[(1 << n) - 1][i]);
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
railroad.cpp: In function 'long long int case3(std::vector<int>, std::vector<int>)':
railroad.cpp:60:19: warning: unused variable 'p' [-Wunused-variable]
60 | int u = to.fi, p = to.se;
| ^
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:114:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for (int i = 0; i < b.size(); i++)
| ~~^~~~~~~~~~
railroad.cpp:115:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for (int j = 0; j < b.size(); j++) {
| ~~^~~~~~~~~~
# | 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... |