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"
#ifndef EVAL
#include "grader.cpp"
#endif
#include <bits/stdc++.h>
#define pii pair<int, int>
#define F first
#define S second
using namespace std;
vector<int> createFunTour(int N, int Q)
{
priority_queue<pii> pq;
for (int i = 1; i < N; i++)
pq.push(pii(attractionsBehind(0, i), i)); // N - 1
int c = 0;
while (!pq.empty() && pq.top().F > N / 2)
{
auto [sz, u] = pq.top();
pq.pop();
if (hoursRequired(c, u) == 1) // <= (N - 1) / 2
c = u;
}
vector<int> d(N), order(N), sol(2 * N, -1);
vector<int> tmp[2];
for (int i = 0; i < N; i++)
d[i] = hoursRequired(c, i), order[i] = i;
sort(order.begin(), order.end(), [&](int i, int j)
{ return d[i] < d[j]; });
auto diffSubtree = [&](int i, int j)
{
if (i == c || j == c)
return true;
return hoursRequired(i, j) == d[i] + d[j];
};
int t = 0;
for (int i = 0; i < N; i++)
{
if (tmp[t ^ 1].empty() || diffSubtree(order[i], tmp[t ^ 1].back()))
{
tmp[t].emplace_back(order[i]);
if (tmp[t].size() + t > tmp[t ^ 1].size())
t ^= 1;
}
else
tmp[t ^ 1].emplace_back(order[i]);
}
for (int i = 0; i < tmp[0].size(); i++)
sol[i * 2] = tmp[0][i];
for (int i = 0; i < tmp[1].size(); i++)
sol[i * 2 + 1] = tmp[1][i];
tmp[0].clear(), tmp[1].clear();
int over = -1, cnt = 0, last = 0;
for (int i = 2 * N - 1; i > 0; i--)
{
if (sol[i] == -1 && sol[i - 1] >= 0)
{
over = sol[i - 1], cnt++;
tmp[1].emplace_back(sol[i - 1]);
last = i - 2;
}
}
// cerr << c << '\n';
// for (int i = 0; i < 2 * N; i++)
// cerr << sol[i] << " \n"[i == 2 * N - 1];
sol.resize(N);
for (; cnt >= 0; last--)
{
if (diffSubtree(sol[last], over))
{
tmp[0].emplace_back(sol[last]);
cnt--;
}
else
{
tmp[1].emplace_back(sol[last]);
last--;
tmp[0].emplace_back(sol[last]);
}
}
sort(tmp[0].begin(), tmp[0].end(), [&](int i, int j)
{ return d[i] > d[j]; });
sort(tmp[1].begin(), tmp[1].end(), [&](int i, int j)
{ return d[i] > d[j]; });
reverse(sol.begin(), sol.end());
for (int i = 0; i < tmp[0].size(); i++)
sol[i * 2] = tmp[0][i];
for (int i = 0; i < tmp[1].size(); i++)
sol[i * 2 + 1] = tmp[1][i];
// for (int i = 0; i < N; i++)
// cerr << sol[i] << " \n"[i == 2 * N - 1];
return sol;
}
Compilation message (stderr)
fun.cpp: In function 'std::vector<int> createFunTour(int, int)':
fun.cpp:19:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
19 | auto [sz, u] = pq.top();
| ^
fun.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int i = 0; i < tmp[0].size(); i++)
| ~~^~~~~~~~~~~~~~~
fun.cpp:53:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for (int i = 0; i < tmp[1].size(); i++)
| ~~^~~~~~~~~~~~~~~
fun.cpp:92:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int i = 0; i < tmp[0].size(); i++)
| ~~^~~~~~~~~~~~~~~
fun.cpp:94:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (int i = 0; i < tmp[1].size(); i++)
| ~~^~~~~~~~~~~~~~~
# | 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... |