Submission #717175

#TimeUsernameProblemLanguageResultExecution timeMemory
717175LittleCubeFun Tour (APIO20_fun)C++14
10 / 100
92 ms16916 KiB
#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, tree[3];

    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];
    };

    for (int i = 0; i < N; i++)
        if (i != c)
        {
            if (tree[0].empty() || !diffSubtree(tree[0].front(), i))
                tree[0].emplace_back(i);
            else if (tree[1].empty() || !diffSubtree(tree[1].front(), i))
                tree[1].emplace_back(i);
            else
                tree[2].emplace_back(i);
        }
    if (tree[2].size() > tree[1].size())
        tree[1].emplace_back(c);
    else
        tree[2].emplace_back(c);

    for (int i = 0; i < 3; i++)
        sort(tree[i].begin(), tree[i].end(), [&](int i, int j)
             { return d[i] > d[j]; });

    vector<int> color(N);
    for (int i = 0; i < 3; i++)
        for (int j : tree[i])
            color[j] = i;
    auto solve = [&](vector<int> A, vector<int> B, vector<int> C)
    {
        vector<int> res(N);
        int L = (N + 1) / 2, R = N / 2;
        reverse(B.begin(), B.end());
        while (A.size() < L)
        {
            A.emplace_back(C.back());
            C.pop_back();
        }
        while (B.size() < R)
        {
            B.emplace_back(C.back());
            C.pop_back();
        }
        reverse(B.begin(), B.end());
        stable_sort(A.begin(), A.end(), [&](int i, int j)
                    { return d[i] > d[j]; });
        stable_sort(B.begin(), B.end(), [&](int i, int j)
                    { return d[i] > d[j]; });
        for (int i = 0; i < L; i++)
            res[i * 2] = A[i];
        for (int i = 0; i < R; i++)
            res[i * 2 + 1] = B[i];
        for (int i = 0; i + 1 < N; i++)
            if (color[res[i]] == color[res[i + 1]])
                return vector<int>{};
        return res;
    };

    sol = solve(tree[0], tree[1], tree[2]);
    if (sol.size())
        return sol;
    sol = solve(tree[0], tree[2], tree[1]);
    if (sol.size())
        return sol;
    sol = solve(tree[1], tree[0], tree[2]);
    if (sol.size())
        return sol;
    sol = solve(tree[1], tree[2], tree[0]);
    if (sol.size())
        return sol;
    sol = solve(tree[2], tree[1], tree[0]);
    if (sol.size())
        return sol;
    sol = solve(tree[2], tree[0], tree[1]);
    if (sol.size())
        return sol;
    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: In lambda function:
fun.cpp:68:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   68 |         while (A.size() < L)
      |                ~~~~~~~~~^~~
fun.cpp:73:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   73 |         while (B.size() < R)
      |                ~~~~~~~~~^~~
#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...