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 "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
uint64_t randomAddress() {
char *p = new char;
delete p;
return uint64_t(p);
}
uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count() * (randomAddress() | 1);
// mt19937 rng(SEED);
void concat(vector<int> &t1, vector<int> &t2) {
if(t1.size() < t2.size()) {
swap(t1, t2);
}
while(!t2.empty()) {
t1.emplace_back(t2.back());
t2.pop_back();
}
}
std::vector<int> longest_trip(int n, int D) {
srand(SEED);
vector<int> res, ord(n);
iota(ord.begin(), ord.end(), 0);
random_shuffle(ord.begin(), ord.end());
if(D == 3) {
for(const auto &u: ord) {
res.emplace_back(u);
}
} else if(D == 2) {
deque<int> dq;
dq.emplace_back(ord.back());
ord.pop_back();
for(vector<int>::iterator it = ord.begin(); it != ord.end(); it++) {
if(are_connected({dq.back()}, {*it})) {
dq.emplace_back(*it);
ord.erase(it);
break;
}
}
assert((int) dq.size() == 2);
for(const auto &u: ord) {
if(are_connected({u}, {dq.back()})) {
dq.emplace_back(u);
} else {
assert(are_connected({u}, {dq.front()}));
dq.emplace_front(u);
}
}
for(const auto &u: dq) {
res.emplace_back(u);
}
} else {
vector<int> t1, t2;
t1.emplace_back(ord.back());
ord.pop_back();
t2.emplace_back(ord.back());
ord.pop_back();
for(const auto &u: ord) {
if(are_connected({u}, {t1.back()})) {
t1.emplace_back(u);
} else if(are_connected({u}, {t2.back()})) {
t2.emplace_back(u);
} else {
concat(t1, t2);
t2.emplace_back(u);
}
}
if(t1.size() < t2.size()) {
swap(t1, t2);
}
for(const auto &u: t1) {
res.emplace_back(u);
}
}
return res;
}
# | 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... |