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 ll int
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
#define sz(a) ((ll)(a).size())
using namespace std;
const ll maxn=100005;
ll dist(ll a, ll b) {return hoursRequired(a, b);}
ll subcnt(ll a, ll b) {return attractionsBehind(a, b);}
vector<int> createFunTour(int n, int Q)
{
ll g=0;
// findcentroid
{
ll cr=n;
for (ll j=1; j<n; j++)
{
ll x=subcnt(0, j);
if (x*2>=n && x<=cr)
g=j, cr=x;
}
}
vector <ll> d(n, 0);
vector <pll> dis;
vector <vector <pll>> branch;
for (ll i=0; i<n; i++)
if (i!=g)
{
dis.pb({dist(g, i), i}), d[i]=dis.back().fi;
if (dis.back().fi==1) branch.pb(vector <pll> {{1, i}}), dis.pop_back();
}
for (auto [d, i]:dis)
for (ll j=0; j<sz(branch); j++)
{
if (j+1==sz(branch)) branch[j].pb({d, i});
else if (dist(i, branch[j][0].se)+1==d) {branch[j].pb({d, i}); break;}
}
for (ll j=0; j<sz(branch); j++)
sort(branch[j].begin(), branch[j].end(), greater<pll>());
ll first=0;
vector <ll> tour;
if (sz(branch)==3)
{
// case 2
ll ptr[3]={}, last=-1, fat=0;
while (1)
{
// check
{
ll sum=0, ok=1;
for (ll i=0; i<3; i++)
sum+=sz(branch[i])-ptr[i];
for (ll i=0; i<3; i++)
if ((sz(branch[i])-ptr[i])*2>=sum) fat=i, ok=0;
if (!ok || !sum) break;
}
ll big=-1;
for (ll i=0; i<3; i++)
if (i!=last && ptr[i]<sz(branch[i]))
if (big==-1 || branch[big][ptr[big]].fi<branch[i][ptr[i]].fi)
big=i;
tour.pb(branch[big][ptr[big]].se), ptr[big]++, last=big;
}
vector <pll> b1, b2;
for (ll i=0; i<3; i++)
{
if (i==fat) b1.insert(b1.end(), branch[i].begin()+ptr[i], branch[i].end());
else b2.insert(b2.end(), branch[i].begin()+ptr[i], branch[i].end());
}
sort(b2.begin(), b2.end(), greater<pll>());
branch[0]=b1, branch[1]=b2;
if (last==fat) swap(branch[0], branch[1]);
}
else if (sz(branch[0])<sz(branch[1])) swap(branch[0], branch[1]);
if (sz(tour)>=2 && d[tour.back()]<branch[1][0].fi) swap(branch[0], branch[1]);
for (ll ptr[2]={}, t=0; ptr[0]<sz(branch[0]) || ptr[1]<sz(branch[1]); ptr[t]++, t^=1)
tour.pb(branch[t][ptr[t]].se);
tour.pb(g); return tour;
}
Compilation message (stderr)
fun.cpp: In function 'std::vector<int> createFunTour(int, int)':
fun.cpp:47:8: warning: unused variable 'first' [-Wunused-variable]
47 | ll first=0;
| ^~~~~
# | 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... |