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 "fun.h"
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
vector<int> createFunTour(int N, int Q) {
int n = N;
int cen = 0;
if (n == 2)
return {0, 1};
for (int i = 1;i < n;i++) {
if (attractionsBehind(cen, i) * 2 >= n)
cen = i;
}
vector<int> dis(n);
for (int i = 0;i < n;i++) {
if (i == cen)
continue;
dis[i] = hoursRequired(cen, i);
}
vector<int> p;
for (int i = 0;i < n;i++) {
if (dis[i] == 1) {
p.push_back(i);
}
}
int m = p.size();
vector<int> ch[m];
for (int i = 0;i < n;i++) {
if (i == cen)
continue;
for (int j = 0;j < m;j++) {
if (j == m - 1) {
ch[j].push_back(i);
}
else {
int x = hoursRequired(p[j], i);
if (x + 1 == dis[i]) {
ch[j].push_back(i);
break;
}
}
}
}
for (int i = 0;i < m;i++) {
sort(ch[i].begin(), ch[i].end(), [&](int x, int y) {
return dis[x] < dis[y];
});
}
vector<int> ans;
auto Resi2 = [&](vector<int> s1, vector<int> s2) {
if (s1.size() > s2.size())
swap(s1, s2);
while (s2.size() + s1.size() > 0) {
ans.push_back(s2.back());
s2.pop_back();
swap(s1, s2);
}
};
if (m == 2) {
Resi2(ch[0], ch[1]);
}
else {
int lst = -1, z = 0;
while (1) {
int u = 0, mx = 0;
for (int i = 0;i < m;i++) {
mx = max(mx, (int)ch[i].size());
u += ch[i].size();
}
if (mx == u - mx || mx == 0)
break;
mx = 0;
z++;
int node = -1;
for (int j = 0;j < m;j++) {
if (j == lst)
continue;
if (!ch[j].empty() && dis[ch[j].back()] >= mx) {
mx = dis[ch[j].back()];
node = j;
}
}
assert(node != -1);
if (node != -1) {
ans.push_back(ch[node].back());
ch[node].pop_back();
lst = node;
}
else
break;
}
int bst = 0;
for (int i = 0;i < m;i++) {
if (ch[i].size() >= ch[bst].size())
bst = i;
}
vector<int> all;
int pos = -1;
int mx = 0;
for (int i = 0;i < m;i++) {
if (i == lst)
continue;
if (!ch[i].empty() && dis[ch[i].back()] > mx) {
pos = i;
mx = dis[ch[i].back()];
}
}
if (z > 1 && pos != bst && dis[ch[pos].back()] > dis[ch[bst].back()]) {
ans.push_back(ch[pos].back());
ch[pos].pop_back();
}
while (!ch[bst].empty()) {
ans.push_back(ch[bst].back());
ch[bst].pop_back();
int lst = -1;
for (int i = 0; i < m; i++) {
if (i == bst || ch[i].empty()) {
continue;
}
if (lst == -1 || dis[ch[lst].back()] < dis[ch[i].back()]) {
lst = i;
}
}
if (lst != -1) {
ans.push_back(ch[lst].back());
ch[lst].pop_back();
}
}
}
ans.push_back(cen);
return ans;
}
# | 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... |