#include "longesttrip.h"
//#include "grader.cpp"
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
vector<int> operator+=(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 += vector<int>(c.begin(), c.begin() + r - 1);
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 (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 += vector<int>(c.begin(), c.begin() + r - 1);
a += vector<int>(c.begin() + r, c.end());
c.clear();
}
}
break;
}
}
return a;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
2 ms |
344 KB |
invalid array |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
invalid array |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
invalid array |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
invalid array |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
invalid array |
2 |
Halted |
0 ms |
0 KB |
- |