제출 #320181

#제출 시각아이디문제언어결과실행 시간메모리
320181VEGAnn즐거운 행로 (APIO20_fun)C++14
0 / 100
1 ms492 KiB
#include "fun.h"
#include <bits/stdc++.h>
#define PB push_back
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define i2 array<int,2>
using namespace std;
const int oo = 2e9;
const int N = 100100;
vector<int> ans, chld;
vector<i2> elms[3];
int dst[N];

void srt(){
    if (sz(elms[0]) < sz(elms[1])) swap(elms[0], elms[1]);
    if (sz(chld) == 3 && sz(elms[1]) < sz(elms[2])) swap(elms[1], elms[2]);
    if (sz(elms[0]) < sz(elms[1])) swap(elms[0], elms[1]);
}

vector<int> createFunTour(int n, int Q) {
    if (n == 2) return {0, 1};

    int cen = -1, mn = oo, hlf = (n + 1) / 2;

    for (int i = 0; i < n; i++){
        int now = attractionsBehind(0, i);

        if (now < mn && now >= hlf){
            mn = now;
            cen = i;
        }
    }

    chld.clear();

    for (int i = 0; i < n; i++){
        if (i == cen) continue;

        int now = hoursRequired(cen, i);

        dst[i] = now;

        if (now == 1)
            chld.PB(i);
    }

    for (int i = 0; i < n; i++){
        if (i == cen) continue;

        int who = 0;

        for (int it = 1; it < sz(chld); it++){
            int now = hoursRequired(chld[it], i);

            if (now + 1 == dst[i]){
                who = it;
                break;
            }
        }

        elms[who].PB({dst[i], i});
    }

    for (int i = 0; i < sz(chld); i++){
        sort(all(elms[i]));
    }

    srt();

    ans.clear();

    if (sz(chld) == 3 && sz(elms[0]) >= sz(elms[1]) + sz(elms[2])){
        chld.pop_back();

        for (i2 cr : elms[2])
            elms[1].PB(cr);

        sort(all(elms[1]));
    }

    if (sz(chld) == 3){
        int lst = -1;

        while (1){
            int bst = -1, id = -1;

            for (int it = 0; it < 3; it++){
                if (it == lst) continue;

                if (elms[it].back()[0] > bst){
                    bst = elms[it].back()[0];
                    id = it;
                }
            }

            ans.PB(elms[id].back()[1]);
            elms[id].pop_back();

            lst = id;

            if (sz(elms[0]) == sz(elms[1]) + sz(elms[2])) break;
            if (sz(elms[1]) == sz(elms[0]) + sz(elms[2])) break;
            if (sz(elms[2]) == sz(elms[0]) + sz(elms[1])) break;
        }

        int bst = -1, id = -1;

        for (int it = 0; it < 3; it++){
            if (it == lst || sz(elms[it]) == 0) continue;

            if (elms[it].back()[0] > bst){
                bst = elms[it].back()[0];
                id = it;
            }
        }

        ans.PB(elms[id].back()[1]);
        elms[id].pop_back();

        srt();

        for (i2 cr : elms[2])
            elms[1].PB(cr);

        sort(all(elms[1]));

        while (sz(elms[1]) > 0){
            ans.PB(elms[0].back()[1]);
            ans.PB(elms[1].back()[1]);

            elms[0].pop_back();
            elms[1].pop_back();
        }

        if (sz(elms[0]) > 0)
            ans.PB(elms[0][0][1]);
    } else {
        while (sz(elms[1]) > 0){
            ans.PB(elms[0].back()[1]);
            ans.PB(elms[1].back()[1]);

            elms[0].pop_back();
            elms[1].pop_back();
        }

        if (sz(elms[0]) > 0)
            ans.PB(elms[0][0][1]);
    }

    ans.PB(cen);

    return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...