답안 #1082626

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1082626 2024-08-31T19:43:10 Z Boas 가장 긴 여행 (IOI23_longesttrip) C++17
85 / 100
13 ms 720 KB
#include "longesttrip.h"

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

template <typename T1, typename T2>
using indexed_map = tree<T1, T2, less<T1>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using indexed_set = indexed_map<T, null_type>;

#define loop(x, i) for (int i = 0; i < (x); i++)
#define loop1(x, i) for (int i = 1; i <= (x); i++)
#define rev(x, i) for (int i = (int)(x) - 1; i >= 0; i--)
#define itloop(x) for (auto it = begin(x); x != end(x); it++)
#define itrev(x) for (auto it = rbegin(x); x != rend(x); it++)
#define INF32 ((int32_t)(2e9 + 1))
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
#define removeIn(x, l) l.erase(find(ALL(l), x))
#define pb push_back
#define sz(x) (int)(x).size()
#define F first
#define S second
#define var const auto &
#define foreach(l) for (var e : l)

typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<i32> vi32;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vi32> vvi32;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vii> vvii;
typedef vector<viii> vviii;
typedef set<int> si;
typedef set<ii> sii;
typedef set<iii> siii;
typedef vector<si> vsi;
typedef vector<sii> vsii;
typedef vector<vsi> vvsi;
typedef vector<string> vstr;
typedef vector<vector<string>> vvstr;
typedef vector<bool> vb;
typedef vector<vb> vvb;

vi longest_trip(int N, int D)
{
    if (D >= 2)
    {
        vi res(N);
        iota(ALL(res), 0);
        if (D == 3)
        {
            return res;
        }
        si resterend;
        loop1(N - 1, i) resterend.insert(res[i]);
        loop1(N - 1, i)
        {
            auto it = resterend.begin();
            if (are_connected({res[i - 1]}, {*it}))
            {
                res[i] = *it;
                resterend.erase(it);
            }
            else
            {
                if (resterend.size() == 1)
                {
                    res.insert(res.begin(), *it);
                    res.pop_back();
                    return res;
                }
                res[i] = *next(it);
                resterend.erase(next(it));
            }
        }
        return res;
    }
    else
    {
        map<ii, bool> con;
        auto getCon = [&](int a, int b) -> bool
        {
            if (a > b)
                swap(a, b);
            assert(a != b);
            if (con.count({a, b}))
                return con[{a, b}];
            return con[{a, b}] = are_connected({a}, {b});
        };
        deque<int> path1, path2;
        path1.pb(0);
        for (int i = 1; i < N; i++)
        {
            if (getCon(i, path1.back()))
            {
                path1.pb(i);
            }
            else if (!path2.empty() && getCon(i, path2[0]))
            {
                path2.push_front(i);
            }
            else
            {
                for (int v : path2)
                    path1.pb(v);
                path2.clear();
                path2.pb(i);
            }
        }
        assert(sz(path1) + sz(path2) == N);
        vi res;
        if (sz(path1) < sz(path2))
            swap(path1, path2);

        vi uq(ALL(path1)), vq(ALL(path2));
        if (sz(path1) == N)
            return vi(ALL(path1));
        assert(!uq.empty());
        assert(!vq.empty());
        if (are_connected(uq, vq))
        {
            if (!getCon(path1[0], path1.back()))
            {
                if (getCon(path1[0], path2[0]))
                {
                    rev(sz(path1), ix) res.pb(path1[ix]);
                    for (int i : path2)
                        res.pb(i);
                    return res;
                }
                else
                {
                    for (int i : path1)
                        res.pb(i);
                    for (int i : path2)
                        res.pb(i);
                    return res;
                }
            }
            if (sz(path2) > 1 && !getCon(path2[0], path2.back()))
            {
                if (getCon(path2[0], path1[0]))
                {
                    rev(sz(path2), ix) res.pb(path2[ix]);
                    for (int i : path1)
                        res.pb(i);
                    return res;
                }
                else
                {
                    for (int i : path2)
                        res.pb(i);
                    for (int i : path1)
                        res.pb(i);
                    return res;
                }
            }
            int lo = 0, hi = sz(uq) - 1;
            while (hi > lo)
            {
                int mid = (lo + hi) / 2;
                vi uq2(begin(uq), begin(uq) + mid + 1);
                if (are_connected(uq2, vq))
                {
                    hi = mid;
                }
                else
                    lo = mid + 1;
            }
            vi uq2(begin(uq), begin(uq) + lo + 1);
            int tar1 = path1[lo];
            lo = 0, hi = sz(vq) - 1;
            while (hi > lo)
            {
                int mid = (lo + hi) / 2;
                vi vq2(begin(vq), begin(vq) + mid + 1);
                if (are_connected(uq2, vq2))
                {
                    hi = mid;
                }
                else
                    lo = mid + 1;
            }
            int tar2 = path2[lo], s1 = -1, s2 = -1;
            loop(sz(path1), ix)
            {
                if (path1[ix] == tar1)
                {
                    s1 = ix;
                }
            }
            loop(sz(path2), ix)
            {
                if (path2[ix] == tar2)
                {
                    s2 = ix;
                }
            }
            assert(s1 != -1 && s2 != -1);
            for (int i = s1 + 1; i < sz(path1); i++)
            {
                res.pb(path1[i]);
            }
            for (int i = 0; i <= s1; i++)
            {
                res.pb(path1[i]);
            }
            for (int i = s2; i < sz(path2); i++)
            {
                res.pb(path2[i]);
            }
            for (int i = 0; i < s2; i++)
            {
                res.pb(path2[i]);
            }
        }
        else
        {
            if (sz(path1) > sz(path2))
            {
                for (int v : path1)
                    res.pb(v);
            }
            else
            {
                for (int v : path2)
                    res.pb(v);
            }
            loop(sz(res) - 1, i) assert(getCon(res[i], res[i + 1]));
            return res;
        }
        assert(sz(res) == N);
        loop(sz(res) - 1, i) assert(getCon(res[i], res[i + 1]));
        return res;
    }
    throw;
    return {};
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 2 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
6 Correct 5 ms 344 KB Output is correct
7 Correct 5 ms 344 KB Output is correct
8 Correct 5 ms 344 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 5 ms 344 KB Output is correct
11 Correct 6 ms 344 KB Output is correct
12 Correct 6 ms 344 KB Output is correct
13 Correct 6 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 5 ms 436 KB Output is correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 7 ms 344 KB Output is correct
8 Correct 9 ms 344 KB Output is correct
9 Correct 4 ms 344 KB Output is correct
10 Correct 5 ms 344 KB Output is correct
11 Correct 5 ms 344 KB Output is correct
12 Correct 7 ms 440 KB Output is correct
13 Correct 8 ms 344 KB Output is correct
14 Correct 8 ms 636 KB Output is correct
15 Correct 10 ms 344 KB Output is correct
16 Correct 13 ms 344 KB Output is correct
17 Correct 8 ms 344 KB Output is correct
18 Correct 9 ms 344 KB Output is correct
19 Correct 8 ms 344 KB Output is correct
20 Correct 6 ms 344 KB Output is correct
21 Correct 6 ms 460 KB Output is correct
22 Correct 10 ms 344 KB Output is correct
23 Correct 7 ms 344 KB Output is correct
24 Correct 9 ms 344 KB Output is correct
25 Correct 7 ms 344 KB Output is correct
26 Correct 6 ms 344 KB Output is correct
27 Correct 5 ms 344 KB Output is correct
28 Correct 9 ms 344 KB Output is correct
29 Correct 7 ms 344 KB Output is correct
30 Correct 5 ms 344 KB Output is correct
31 Correct 7 ms 344 KB Output is correct
32 Correct 8 ms 344 KB Output is correct
33 Correct 5 ms 344 KB Output is correct
34 Correct 5 ms 344 KB Output is correct
35 Correct 8 ms 512 KB Output is correct
36 Correct 5 ms 344 KB Output is correct
37 Correct 6 ms 436 KB Output is correct
38 Correct 5 ms 344 KB Output is correct
39 Correct 7 ms 344 KB Output is correct
40 Correct 9 ms 344 KB Output is correct
41 Correct 9 ms 448 KB Output is correct
42 Correct 7 ms 344 KB Output is correct
43 Correct 12 ms 464 KB Output is correct
44 Correct 12 ms 344 KB Output is correct
45 Correct 10 ms 344 KB Output is correct
46 Correct 9 ms 344 KB Output is correct
47 Correct 8 ms 344 KB Output is correct
48 Correct 9 ms 344 KB Output is correct
49 Correct 9 ms 344 KB Output is correct
50 Correct 6 ms 344 KB Output is correct
51 Correct 7 ms 344 KB Output is correct
52 Correct 8 ms 600 KB Output is correct
53 Correct 6 ms 344 KB Output is correct
54 Correct 7 ms 344 KB Output is correct
55 Correct 9 ms 600 KB Output is correct
56 Correct 6 ms 456 KB Output is correct
57 Correct 6 ms 344 KB Output is correct
58 Correct 7 ms 344 KB Output is correct
59 Correct 8 ms 472 KB Output is correct
60 Correct 9 ms 344 KB Output is correct
61 Correct 9 ms 344 KB Output is correct
62 Correct 9 ms 600 KB Output is correct
63 Correct 9 ms 344 KB Output is correct
64 Correct 10 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 6 ms 344 KB Output is correct
6 Correct 5 ms 344 KB Output is correct
7 Correct 7 ms 344 KB Output is correct
8 Correct 7 ms 344 KB Output is correct
9 Correct 9 ms 344 KB Output is correct
10 Correct 8 ms 708 KB Output is correct
11 Correct 5 ms 344 KB Output is correct
12 Correct 8 ms 344 KB Output is correct
13 Correct 5 ms 344 KB Output is correct
14 Correct 7 ms 344 KB Output is correct
15 Correct 8 ms 344 KB Output is correct
16 Correct 13 ms 344 KB Output is correct
17 Correct 8 ms 600 KB Output is correct
18 Correct 10 ms 344 KB Output is correct
19 Correct 9 ms 340 KB Output is correct
20 Correct 7 ms 344 KB Output is correct
21 Correct 7 ms 344 KB Output is correct
22 Correct 7 ms 416 KB Output is correct
23 Correct 6 ms 344 KB Output is correct
24 Correct 6 ms 344 KB Output is correct
25 Correct 6 ms 344 KB Output is correct
26 Correct 5 ms 344 KB Output is correct
27 Correct 8 ms 344 KB Output is correct
28 Correct 9 ms 344 KB Output is correct
29 Correct 5 ms 344 KB Output is correct
30 Correct 8 ms 344 KB Output is correct
31 Correct 6 ms 344 KB Output is correct
32 Correct 9 ms 344 KB Output is correct
33 Correct 9 ms 344 KB Output is correct
34 Correct 7 ms 344 KB Output is correct
35 Correct 9 ms 344 KB Output is correct
36 Correct 8 ms 344 KB Output is correct
37 Correct 6 ms 344 KB Output is correct
38 Correct 8 ms 344 KB Output is correct
39 Correct 9 ms 440 KB Output is correct
40 Correct 6 ms 344 KB Output is correct
41 Correct 9 ms 344 KB Output is correct
42 Correct 6 ms 344 KB Output is correct
43 Correct 13 ms 460 KB Output is correct
44 Correct 5 ms 344 KB Output is correct
45 Correct 7 ms 344 KB Output is correct
46 Correct 6 ms 344 KB Output is correct
47 Correct 5 ms 344 KB Output is correct
48 Correct 6 ms 344 KB Output is correct
49 Correct 9 ms 344 KB Output is correct
50 Correct 7 ms 344 KB Output is correct
51 Partially correct 7 ms 344 KB Output is partially correct
52 Correct 11 ms 456 KB Output is correct
53 Correct 8 ms 344 KB Output is correct
54 Correct 9 ms 600 KB Output is correct
55 Correct 9 ms 344 KB Output is correct
56 Correct 4 ms 344 KB Output is correct
57 Partially correct 7 ms 448 KB Output is partially correct
58 Partially correct 6 ms 344 KB Output is partially correct
59 Partially correct 7 ms 344 KB Output is partially correct
60 Correct 10 ms 452 KB Output is correct
61 Correct 8 ms 344 KB Output is correct
62 Correct 6 ms 456 KB Output is correct
63 Partially correct 8 ms 600 KB Output is partially correct
64 Partially correct 9 ms 476 KB Output is partially correct
65 Partially correct 12 ms 344 KB Output is partially correct
66 Partially correct 13 ms 344 KB Output is partially correct
67 Correct 8 ms 468 KB Output is correct
68 Correct 9 ms 344 KB Output is correct
69 Partially correct 9 ms 468 KB Output is partially correct
70 Partially correct 8 ms 472 KB Output is partially correct
71 Correct 7 ms 344 KB Output is correct
72 Partially correct 8 ms 484 KB Output is partially correct
73 Partially correct 9 ms 440 KB Output is partially correct
74 Partially correct 9 ms 344 KB Output is partially correct
75 Correct 10 ms 344 KB Output is correct
76 Correct 10 ms 468 KB Output is correct
77 Correct 6 ms 344 KB Output is correct
78 Partially correct 10 ms 600 KB Output is partially correct
79 Correct 8 ms 720 KB Output is correct
80 Partially correct 8 ms 472 KB Output is partially correct
81 Correct 12 ms 344 KB Output is correct
82 Correct 8 ms 344 KB Output is correct