답안 #1067635

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1067635 2024-08-20T22:12:20 Z golf 가장 긴 여행 (IOI23_longesttrip) C++17
70 / 100
17 ms 600 KB
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;

vector<int> solve_d3(int N, int D)
{
    vector<int> res(N);
    iota(res.begin(), res.end(), 0);
    return res;
}

vector<int> solve_d2(int N, int D)
{
    queue<int> stops;
    for (int i = 1; i < N; i++)
        stops.push(i);

    deque<int> trip = {0};
    while (stops.size() > 1)
    {
        int next = stops.front();
        stops.pop();
        int last = trip.back();

        if (are_connected({next}, {last}))
        {
            trip.push_back(next);
        }
        else
        {
            trip.push_back(stops.front());
            stops.pop();
            trip.push_back(next);
        }
    }

    if (stops.size() == 1)
    {
        int next = stops.front();
        int last = trip.back();
        if (are_connected({next}, {last}))
        {
            trip.push_back(next);
        }
        else
        {
            trip.push_front(stops.front());
            stops.pop();
        }
    }

    vector<int> res;
    for (int x : trip)
        res.push_back(x);
    return res;
}

int pop(queue<int> &q)
{
    int x = q.front();
    q.pop();
    return x;
}

const bool maybe = true;

vector<int> longest_trip(int N, int D)
{
    if (D == 3)
        return solve_d3(N, D);
    if (D == 2)
        return solve_d2(N, D);

    queue<int> nodes;
    for (int i = 0; i < N; i++)
        nodes.push(i);

    vector<int> a, b;
    a.push_back(pop(nodes));

    bool ends_connected = maybe;
    // false = a and b are not connected
    // true  = a and b are maybe connected
    while (!nodes.empty())
    {
        if (b.empty())
        {
            int n = pop(nodes);
            if (are_connected({a.back()}, {n}))
            {
                a.push_back(n);
            }
            else
            {
                // missed optimization
                // we could cache that a and b are not connected
                b.push_back(n);
                ends_connected = false;
            }
            continue;
        }

        // both a and b are non-empty
        int n = pop(nodes);

        if (are_connected({a.back()}, {n}))
        {
            a.push_back(n);
            ends_connected = maybe;
        }
        else if (are_connected({b.back()}, {n}))
        {
            b.push_back(n);
            ends_connected = false;
        }
        else
        {
            // ends of a and b are definitely connected
            while (!b.empty())
            {
                a.push_back(b.back());
                b.pop_back();
            }

            assert(b.empty());
            b.push_back(n);
        }
    }

    if (b.empty())
    {
        return a;
    }

    if (ends_connected == maybe && are_connected({a.back()}, {b.back()}))
    {
        while (!b.empty())
        {
            a.push_back(b.back());
            b.pop_back();
        }

        return a;
    }

    if (!are_connected(a, b))
    {
        if (a.size() > b.size())
        {
            return a;
        }
        else
        {
            return b;
        }
    }

    if (a.size() < b.size())
        swap(a, b);

    assert(a.size() >= b.size());

    bool a_circle = are_connected({a.front()}, {a.back()});

    if (!a_circle)
    {
        for (int x : a)
            b.push_back(x);
        return b;
    }

    // a is a circle

    bool b_circle = b.size() == 1 || are_connected({b.front()}, {b.back()});

    if (!b_circle) {
        for (int x : b) a.push_back(x);
        return a;
    }

    // both a and b are circles
    deque<int> A, B;
    for (int x : a) A.push_back(x);
    for (int x : b) B.push_back(x);

    while (true) {
        int e = A.front(); A.pop_front();
        A.push_back(e);
        if (are_connected({e}, b)) break;
    }

    while (true) {
        int e = B.back(); B.pop_back();
        B.push_front(e);
        if (are_connected({A.back()}, {e})) break;
    }

    vector<int> res;
    for (int x : A) res.push_back(x);
    for (int x : B) res.push_back(x);
    return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 3 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 356 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 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 7 ms 344 KB Output is correct
4 Correct 5 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 4 ms 344 KB Output is correct
8 Correct 4 ms 340 KB Output is correct
9 Correct 5 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 4 ms 344 KB Output is correct
13 Correct 5 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 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 6 ms 344 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 6 ms 436 KB Output is correct
8 Correct 5 ms 344 KB Output is correct
9 Correct 7 ms 344 KB Output is correct
10 Correct 6 ms 344 KB Output is correct
11 Correct 6 ms 344 KB Output is correct
12 Correct 4 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 11 ms 344 KB Output is correct
16 Correct 11 ms 344 KB Output is correct
17 Correct 8 ms 344 KB Output is correct
18 Correct 6 ms 344 KB Output is correct
19 Correct 6 ms 344 KB Output is correct
20 Correct 6 ms 344 KB Output is correct
21 Correct 7 ms 344 KB Output is correct
22 Correct 6 ms 344 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 8 ms 344 KB Output is correct
27 Correct 7 ms 344 KB Output is correct
28 Correct 6 ms 344 KB Output is correct
29 Correct 9 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 9 ms 344 KB Output is correct
33 Correct 6 ms 344 KB Output is correct
34 Correct 5 ms 344 KB Output is correct
35 Correct 8 ms 344 KB Output is correct
36 Correct 5 ms 344 KB Output is correct
37 Correct 4 ms 344 KB Output is correct
38 Correct 5 ms 344 KB Output is correct
39 Correct 6 ms 344 KB Output is correct
40 Correct 7 ms 344 KB Output is correct
41 Correct 9 ms 344 KB Output is correct
42 Correct 9 ms 344 KB Output is correct
43 Correct 8 ms 344 KB Output is correct
44 Correct 7 ms 344 KB Output is correct
45 Correct 8 ms 344 KB Output is correct
46 Correct 10 ms 344 KB Output is correct
47 Correct 9 ms 344 KB Output is correct
48 Correct 12 ms 344 KB Output is correct
49 Correct 7 ms 344 KB Output is correct
50 Correct 7 ms 344 KB Output is correct
51 Correct 10 ms 344 KB Output is correct
52 Correct 10 ms 344 KB Output is correct
53 Correct 7 ms 344 KB Output is correct
54 Correct 11 ms 344 KB Output is correct
55 Correct 7 ms 344 KB Output is correct
56 Correct 7 ms 344 KB Output is correct
57 Correct 7 ms 344 KB Output is correct
58 Correct 11 ms 344 KB Output is correct
59 Correct 12 ms 344 KB Output is correct
60 Correct 12 ms 348 KB Output is correct
61 Correct 8 ms 344 KB Output is correct
62 Correct 12 ms 344 KB Output is correct
63 Correct 11 ms 344 KB Output is correct
64 Correct 12 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 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 7 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 7 ms 344 KB Output is correct
11 Correct 5 ms 344 KB Output is correct
12 Correct 5 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 7 ms 344 KB Output is correct
16 Correct 8 ms 344 KB Output is correct
17 Correct 7 ms 344 KB Output is correct
18 Correct 6 ms 344 KB Output is correct
19 Correct 6 ms 344 KB Output is correct
20 Correct 9 ms 344 KB Output is correct
21 Correct 7 ms 344 KB Output is correct
22 Correct 11 ms 344 KB Output is correct
23 Correct 6 ms 344 KB Output is correct
24 Correct 8 ms 344 KB Output is correct
25 Correct 6 ms 344 KB Output is correct
26 Correct 4 ms 340 KB Output is correct
27 Correct 7 ms 344 KB Output is correct
28 Correct 6 ms 344 KB Output is correct
29 Correct 5 ms 344 KB Output is correct
30 Correct 9 ms 344 KB Output is correct
31 Correct 6 ms 344 KB Output is correct
32 Correct 8 ms 344 KB Output is correct
33 Correct 13 ms 344 KB Output is correct
34 Correct 9 ms 344 KB Output is correct
35 Correct 8 ms 344 KB Output is correct
36 Correct 12 ms 432 KB Output is correct
37 Correct 7 ms 344 KB Output is correct
38 Correct 10 ms 344 KB Output is correct
39 Correct 15 ms 344 KB Output is correct
40 Correct 7 ms 344 KB Output is correct
41 Correct 6 ms 344 KB Output is correct
42 Correct 8 ms 344 KB Output is correct
43 Correct 4 ms 344 KB Output is correct
44 Correct 6 ms 344 KB Output is correct
45 Correct 6 ms 344 KB Output is correct
46 Correct 7 ms 436 KB Output is correct
47 Correct 5 ms 344 KB Output is correct
48 Correct 6 ms 344 KB Output is correct
49 Correct 6 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 6 ms 344 KB Output is correct
53 Correct 5 ms 344 KB Output is correct
54 Correct 8 ms 344 KB Output is correct
55 Correct 6 ms 344 KB Output is correct
56 Correct 5 ms 344 KB Output is correct
57 Partially correct 9 ms 344 KB Output is partially correct
58 Partially correct 7 ms 344 KB Output is partially correct
59 Partially correct 6 ms 344 KB Output is partially correct
60 Correct 8 ms 344 KB Output is correct
61 Correct 7 ms 344 KB Output is correct
62 Partially correct 7 ms 344 KB Output is partially correct
63 Partially correct 9 ms 344 KB Output is partially correct
64 Partially correct 10 ms 344 KB Output is partially correct
65 Partially correct 14 ms 344 KB Output is partially correct
66 Partially correct 17 ms 344 KB Output is partially correct
67 Partially correct 10 ms 344 KB Output is partially correct
68 Partially correct 12 ms 344 KB Output is partially correct
69 Partially correct 10 ms 444 KB Output is partially correct
70 Partially correct 11 ms 344 KB Output is partially correct
71 Partially correct 8 ms 344 KB Output is partially correct
72 Partially correct 10 ms 344 KB Output is partially correct
73 Partially correct 17 ms 344 KB Output is partially correct
74 Partially correct 14 ms 344 KB Output is partially correct
75 Partially correct 14 ms 440 KB Output is partially correct
76 Partially correct 12 ms 344 KB Output is partially correct
77 Partially correct 9 ms 428 KB Output is partially correct
78 Partially correct 16 ms 436 KB Output is partially correct
79 Partially correct 11 ms 344 KB Output is partially correct
80 Partially correct 8 ms 344 KB Output is partially correct
81 Partially correct 10 ms 344 KB Output is partially correct
82 Partially correct 11 ms 344 KB Output is partially correct