이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "fun.h"
#include <bits/stdc++.h>
using namespace std;
pair<int, vector<int>> solve(int N, vector<pair<int, vector<pair<int, int>>>> sub, int root, bool merge_later) {
int prv = -1;
vector<int> res;
int merge_time = -1;
for (int i = 0; i < N - 1; i++) {
if (sub[0].second.size() > sub[1].second.size()) {
swap(sub[0], sub[1]);
}
if (sub[1].second.size() > sub[2].second.size()) {
swap(sub[1], sub[2]);
}
if (sub[0].second.size() > sub[2].second.size()) {
swap(sub[0], sub[2]);
}
pair<pair<int, int>, int> best = {{-1, -1}, -1};
bool will_merge = false;
if (merge_time == -1 && sub[0].second.size() + sub[1].second.size() <= sub[2].second.size()) {
will_merge = true;
merge_time = i;
}
if (will_merge && !merge_later) {
if (prv == sub[0].first || prv == sub[1].first) {
prv = 0;
}
else if (prv == sub[1].first) {
prv = 0;
}
else {
prv = 1;
}
sub[0].first = 0;
sub[1].first = 0;
sub[2].first = 1;
}
for (int k = 0; k < 2; k++) {
if (best.second != -1) {
break;
}
for (int j = 0; j < 3; j++) {
if (k == 0 && prv == sub[j].first) {
continue;
}
if (!sub[j].second.empty()) {
best = max(best, make_pair(sub[j].second.back(), j));
}
}
}
res.push_back(sub[best.second].second.back().second);
sub[best.second].second.pop_back();
prv = sub[best.second].first;
if (will_merge && merge_later) {
if (prv == sub[0].first || prv == sub[1].first) {
prv = 0;
}
else if (prv == sub[1].first) {
prv = 0;
}
else {
prv = 1;
}
sub[0].first = 0;
sub[1].first = 0;
sub[2].first = 1;
}
}
res.push_back(root);
return {merge_time, res};
}
vector<int> createFunTour(int N, int Q) {
pair<int, int> best = {N + 1, -1};
for (int i = 0; i < N; i++) {
int sz = attractionsBehind(0, i);
if (sz * 2 >= N) {
best = min(best, {sz, i});
}
}
int root = best.second;
vector<int> ch;
vector<int> depth(N);
for (int i = 0; i < N; i++) {
depth[i] = hoursRequired(root, i);
if (depth[i] == 1) {
ch.push_back(i);
}
}
bool merge = (int)(ch.size()) <= 2;
ch.resize(3, -1);
vector<pair<int, vector<pair<int, int>>>> sub(3);
for (int i = 0; i < 3; i++) {
sub[i].first = i;
}
for (int i = 0; i < N; i++) {
if (i == root) {
continue;
}
int id = 2;
for (int j = 0; j < 2; j++) {
if (hoursRequired(ch[j], i) + 1 == depth[i]) {
id = j;
break;
}
}
sub[id].second.push_back({depth[i], i});
}
for (int i = 0; i < 3; i++) {
sort(sub[i].second.begin(), sub[i].second.end());
}
auto p = solve(N, sub, root, false);
int pos = p.first;
vector<int> res = p.second;
if (pos == -1) {
return res;
}
int prv = N + 1;
for (int i = max(0, pos - 5); i < min(N - 1, pos + 5); i++) {
int cur = hoursRequired(res[i], res[i + 1]);
if (prv < cur) {
return solve(N, sub, root, true).second;
}
prv = cur;
}
return res;
}
컴파일 시 표준 에러 (stderr) 메시지
fun.cpp: In function 'std::vector<int> createFunTour(int, int)':
fun.cpp:91:7: warning: unused variable 'merge' [-Wunused-variable]
91 | bool merge = (int)(ch.size()) <= 2;
| ^~~~~
# | 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... |