# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
840542 | arbuzick | Closing Time (IOI23_closing) | C++17 | 0 ms | 0 KiB |
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 <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
vector<int> longest_trip(int n, int d) {
if (d == 3) {
vector<int> ans(n);
iota(ans.begin(), ans.end(), 0);
return ans;
}
if (d == 2) {
vector<int> ans = {0};
vector<int> used(n);
used[0] = 1;
while (ans.size() < n) {
int nxt = -1;
for (int i = 0; i < n; ++i) {
if (!used[i] && are_connected({ans.back()}, {i})) {
nxt = i;
used[i] = 1;
break;
}
}
if (nxt == -1) {
reverse(ans.begin(), ans.end());
for (int i = 0; i < n; ++i) {
if (!used[i] && are_connected({ans.back()}, {i})) {
nxt = i;
used[i] = 1;
break;
}
}
}
ans.push_back(nxt);
}
return ans;
}
if (d == 1) {
vector<int> ans = {0};
vector<int> used(n);
used[0] = 1;
auto try_to_add = [&]() {
vector<int> vals;
for (int i = 0; i < n; ++i) {
if (!used[i]) {
vals.push_back(i);
}
}
if (vals.empty()) {
return false;
}
if (!are_connected({ans.back()}, vals)) {
return false;
}
while (vals.size() > 1) {
int m = vals.size() / 2;
vector<int> vals1, vals2;
for (int i = 0; i < (int)vals.size(); ++i) {
if (i < m) {
vals1.push_back(vals[i]);
} else {
vals2.push_back(vals[i]);
}
}
if (are_connected({ans.back()}, vals1)) {
vals = vals1;
} else {
vals = vals2;
}
}
used[vals[0]] = 1;
ans.push_back(vals[0]);
return true;
};
while (true) {
if (!try_to_add()) {
break;
}
}
if (ans.size() < n) {
reverse(ans.begin(), ans.end());
if (try_to_add()) {
while (true) {
if (!try_to_add()) {
break;
}
}
} else {
for (int _ = 0; _ < (int)ans.size(); ++_) {
reverse(ans.begin(), ans.end());
int vl = ans.back();
ans.pop_back();
reverse(ans.begin(), ans.end());
ans.push_back(vl);
if (try_to_add()) {
while (true) {
if (!try_to_add()) {
break;
}
}
break;
}
}
}
}
if (ans.size() * 2 < n) {
ans.clear();
for (int i = 0; i < n; ++i) {
if (!used[i]) {
ans.push_back(i);
}
}
}
return ans;
}
}