Submission #714909

#TimeUsernameProblemLanguageResultExecution timeMemory
714909dxz05Fun Tour (APIO20_fun)C++17
26 / 100
262 ms37848 KiB
#include "fun.h"
#include <bits/stdc++.h>

using namespace std;

const int MaxN = 100005;

map<int, int> mem[MaxN];
int ask(int i, int j){
    if (i > j) swap(i, j);
    if (i == j) return 0;
    if (mem[i].find(j) != mem[i].end()) return mem[i][j];
    return mem[i][j] = hoursRequired(i, j);
}

vector<int> createFunTour(int N, int Q) {
    int x = 0, y = 0;
    for (int i = 0; i < N; i++){
        for (int j = 0; j < N; j++){
            if (ask(i, j) > ask(x, y)) x = i, y = j;
        }
    }

    vector<int> p(N);

    function<bool()> check = [&](){
        vector<bool> used(N);
        used[p[0]] = used[p[1]] = true;

        for (int i = 1; i + 1 < N; i++){
            int v = p[i];
            for (int j = 0; j < N; j++){
                if (!used[j] && ask(p[i], j) > ask(p[i], v) && ask(p[i], j) <= ask(p[i], p[i - 1])){
                    v = j;
                }
            }
            if (v == p[i]) return false;

            p[i + 1] = v;
            used[v] = true;
        }

        return true;
    };

    p[0] = x, p[1] = y;

    if (!check()){
        swap(p[0], p[1]);
        check();
    }

    return p;
}
#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...