이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "longesttrip.h"
//#include "grader.cpp"
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
vector<int> add(vector<int> a, vector<int> b) {
for (auto j : b) {
a.push_back(j);
}
return a;
}
vector<int> longest_trip(int N, int D) {
int n = N;
vector<int> a, b, c;
auto ask = [&](int x, int y) -> bool {
return are_connected(vector<int>{x}, vector<int>{y});
};
for (int i = 1; i < n; ++i) {
b.push_back(i);
}
a.push_back(0);
while (true) {
vector<int> last{a.back()};
if (c.size() && are_connected(last, c)) {
int l = 1, r = c.size();
while (l <= r) {
int mid = (l + r) / 2;
if (are_connected(last, vector<int>(c.begin(), c.begin() + mid))) {
r = mid - 1;
} else {
l = mid + 1;
}
}
++r;
a.emplace_back(c[r - 1]);
a = add(a, vector<int>(c.begin(), c.begin() + r - 1));
a = add(a, vector<int>(c.begin() + r, c.end()));
c.clear();
continue;
}
while (b.size() && are_connected(last, vector<int>{b.back()}) == 0) {
c.push_back(b.back());
b.pop_back();
}
if (b.size()) {
a.push_back(b.back());
b.pop_back();
} else {
if (c.size() && are_connected(a, c) == 0) {
if (a.size() < c.size()) {
swap(a, c);
}
} else {
if (c.size()) {
int l = 1, r = c.size();
while (l <= r) {
int mid = (l + r) / 2;
if (are_connected(a, vector<int>(c.begin(), c.begin() + mid))) {
r = mid - 1;
} else {
l = mid + 1;
}
}
++r;
int x = c[r - 1];
while (ask(a[0], x) == 0 && ask(a.back(), x) == 0) {
a.push_back(a[0]);
a.erase(a.begin());
}
if (ask(a[0], x)) {
reverse(a.begin(), a.end());
}
a.emplace_back(x);
a = add(a, vector<int>(c.begin(), c.begin() + r - 1));
a = add(a, vector<int>(c.begin() + r, c.end()));
c.clear();
}
}
break;
}
}
return a;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |