Submission #839723

# Submission time Handle Problem Language Result Execution time Memory
839723 2023-08-30T15:48:57 Z model_code Longest Trip (IOI23_longesttrip) C++17
100 / 100
21 ms 348 KB
// correct/sol_na_D1_x.cpp

#include "longesttrip.h"
#include <deque>
#include <functional>
#include <algorithm>
#include <iostream>
#include <cassert>

std::vector<int> longest_trip(int N, int D)
{
    srand(time(0));
    std::vector<int> ids(N);
    for (int i = 0; i < N; i++)
        ids[i] = i;
    random_shuffle(ids.begin(), ids.end());

    std::deque<int> A, B;
    bool x = false;
    for (int i = 0; i < N;)
    {
        int id = ids[i];
        if (A.size() < B.size())
            A.swap(B);
        if (x)
        {
            if (are_connected({A.back()}, {id}))
            {
                A.push_back(id);
                i++;
            }
            else
            {
                B.push_back(id);
                i++;
            }

            x = false;
        }
        else
        {
            if (A.empty())
            {
                A = {id};
                i++;
                continue;
            }

            if (are_connected({A.back()}, {id}))
            {
                A.push_back(id);
                i++;
            }
            else if (B.empty() || are_connected({B.back()}, {id}))
            {
                B.push_back(id);
                i++;
                x = true;
            }
            else
            {
                while (!A.empty())
                {
                    B.push_back(A.back());
                    A.pop_back();
                }
                A = {id};
                i++;
            }
        }
    }

    std::function<std::vector<int>(std::deque<int>, std::deque<int>)> solve;
    solve = [&](std::deque<int> A, std::deque<int> B) -> std::vector<int>
    {
        if (A.size() > B.size())
            A.swap(B);
        if (A.size() == 0)
            return std::vector<int>(B.begin(), B.end());

        bool x = are_connected({A.back()}, {B.front()});
        bool y = are_connected({A.back()}, {B.back()});

        if (x)
        {
            while (!A.empty())
            {
                B.push_front(A.back());
                A.pop_back();
            }
            return std::vector<int>(B.begin(), B.end());
        }
        else if (y)
        {
            while (!A.empty())
            {
                B.push_back(A.back());
                A.pop_back();
            }
            return std::vector<int>(B.begin(), B.end());
        }
        else
        {
            if (are_connected(std::vector<int>(A.begin(), A.end()), std::vector<int>(B.begin(), B.end())))
            {
                int L = 0, R = A.size() - 1;
                auto BB = std::vector<int>(B.begin(), B.end());
                while (L < R)
                {
                    int mid = (L + R) / 2;
                    if (are_connected(std::vector<int>(A.begin(), A.begin() + mid + 1), BB))
                    {
                        R = mid;
                    }
                    else
                    {
                        L = mid + 1;
                    }
                }
                int firstA = L;
                L = 0;
                R = B.size() - 1;
                auto AA = std::vector<int>(A.begin(), A.begin() + firstA + 1);
                while (L < R)
                {
                    int mid = (L + R) / 2;
                    if (are_connected(AA, std::vector<int>(B.begin(), B.begin() + mid + 1)))
                    {
                        R = mid;
                    }
                    else
                    {
                        L = mid + 1;
                    }
                }
                int firstB = L;
                while (firstB > 0)
                {
                    B.push_back(B.front());
                    B.pop_front();
                    firstB--;
                }

                std::deque<int> lst;
                if (firstA >= (int)A.size() / 2)
                {
                    while (firstA >= 0)
                    {
                        lst.push_back(A.front());
                        A.pop_front();
                        firstA--;
                    }
                }
                else
                {
                    while (firstA < (int)A.size())
                    {
                        lst.push_front(A.back());
                        A.pop_back();
                    }
                    reverse(lst.begin(), lst.end());
                }
                while (!lst.empty())
                {
                    B.push_front(lst.back());
                    lst.pop_back();
                }
                return solve(A, B);
            }
            else
            {
                return std::vector<int>(B.begin(), B.end());
            }
        }
    };

    return solve(A, B);
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 3 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 208 KB Output is correct
2 Correct 9 ms 208 KB Output is correct
3 Correct 9 ms 208 KB Output is correct
4 Correct 11 ms 208 KB Output is correct
5 Correct 11 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 256 KB Output is correct
2 Correct 10 ms 208 KB Output is correct
3 Correct 9 ms 208 KB Output is correct
4 Correct 8 ms 208 KB Output is correct
5 Correct 5 ms 304 KB Output is correct
6 Correct 10 ms 208 KB Output is correct
7 Correct 10 ms 208 KB Output is correct
8 Correct 7 ms 208 KB Output is correct
9 Correct 8 ms 208 KB Output is correct
10 Correct 7 ms 208 KB Output is correct
11 Correct 8 ms 208 KB Output is correct
12 Correct 6 ms 208 KB Output is correct
13 Correct 9 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 208 KB Output is correct
2 Correct 4 ms 208 KB Output is correct
3 Correct 7 ms 208 KB Output is correct
4 Correct 8 ms 208 KB Output is correct
5 Correct 10 ms 300 KB Output is correct
6 Correct 15 ms 208 KB Output is correct
7 Correct 9 ms 208 KB Output is correct
8 Correct 5 ms 208 KB Output is correct
9 Correct 11 ms 208 KB Output is correct
10 Correct 8 ms 348 KB Output is correct
11 Correct 7 ms 208 KB Output is correct
12 Correct 9 ms 208 KB Output is correct
13 Correct 7 ms 304 KB Output is correct
14 Correct 17 ms 208 KB Output is correct
15 Correct 21 ms 208 KB Output is correct
16 Correct 9 ms 208 KB Output is correct
17 Correct 10 ms 208 KB Output is correct
18 Correct 6 ms 296 KB Output is correct
19 Correct 8 ms 300 KB Output is correct
20 Correct 11 ms 208 KB Output is correct
21 Correct 13 ms 304 KB Output is correct
22 Correct 8 ms 236 KB Output is correct
23 Correct 8 ms 208 KB Output is correct
24 Correct 10 ms 208 KB Output is correct
25 Correct 11 ms 296 KB Output is correct
26 Correct 21 ms 256 KB Output is correct
27 Correct 11 ms 208 KB Output is correct
28 Correct 11 ms 208 KB Output is correct
29 Correct 10 ms 208 KB Output is correct
30 Correct 4 ms 208 KB Output is correct
31 Correct 12 ms 208 KB Output is correct
32 Correct 12 ms 208 KB Output is correct
33 Correct 8 ms 208 KB Output is correct
34 Correct 6 ms 208 KB Output is correct
35 Correct 6 ms 208 KB Output is correct
36 Correct 9 ms 208 KB Output is correct
37 Correct 8 ms 296 KB Output is correct
38 Correct 11 ms 208 KB Output is correct
39 Correct 11 ms 208 KB Output is correct
40 Correct 10 ms 208 KB Output is correct
41 Correct 14 ms 324 KB Output is correct
42 Correct 11 ms 208 KB Output is correct
43 Correct 13 ms 304 KB Output is correct
44 Correct 8 ms 208 KB Output is correct
45 Correct 15 ms 208 KB Output is correct
46 Correct 15 ms 208 KB Output is correct
47 Correct 12 ms 348 KB Output is correct
48 Correct 12 ms 208 KB Output is correct
49 Correct 16 ms 256 KB Output is correct
50 Correct 11 ms 304 KB Output is correct
51 Correct 13 ms 244 KB Output is correct
52 Correct 15 ms 208 KB Output is correct
53 Correct 10 ms 208 KB Output is correct
54 Correct 11 ms 304 KB Output is correct
55 Correct 13 ms 308 KB Output is correct
56 Correct 7 ms 324 KB Output is correct
57 Correct 10 ms 296 KB Output is correct
58 Correct 12 ms 208 KB Output is correct
59 Correct 9 ms 312 KB Output is correct
60 Correct 12 ms 208 KB Output is correct
61 Correct 13 ms 312 KB Output is correct
62 Correct 14 ms 208 KB Output is correct
63 Correct 10 ms 208 KB Output is correct
64 Correct 14 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 208 KB Output is correct
2 Correct 10 ms 208 KB Output is correct
3 Correct 10 ms 208 KB Output is correct
4 Correct 8 ms 208 KB Output is correct
5 Correct 8 ms 208 KB Output is correct
6 Correct 12 ms 208 KB Output is correct
7 Correct 9 ms 208 KB Output is correct
8 Correct 9 ms 208 KB Output is correct
9 Correct 6 ms 208 KB Output is correct
10 Correct 6 ms 208 KB Output is correct
11 Correct 8 ms 208 KB Output is correct
12 Correct 11 ms 304 KB Output is correct
13 Correct 7 ms 208 KB Output is correct
14 Correct 9 ms 208 KB Output is correct
15 Correct 12 ms 208 KB Output is correct
16 Correct 12 ms 208 KB Output is correct
17 Correct 10 ms 208 KB Output is correct
18 Correct 15 ms 208 KB Output is correct
19 Correct 5 ms 208 KB Output is correct
20 Correct 9 ms 208 KB Output is correct
21 Correct 13 ms 208 KB Output is correct
22 Correct 18 ms 208 KB Output is correct
23 Correct 12 ms 208 KB Output is correct
24 Correct 9 ms 208 KB Output is correct
25 Correct 9 ms 208 KB Output is correct
26 Correct 9 ms 208 KB Output is correct
27 Correct 11 ms 208 KB Output is correct
28 Correct 12 ms 208 KB Output is correct
29 Correct 12 ms 256 KB Output is correct
30 Correct 6 ms 208 KB Output is correct
31 Correct 8 ms 208 KB Output is correct
32 Correct 14 ms 208 KB Output is correct
33 Correct 11 ms 208 KB Output is correct
34 Correct 10 ms 208 KB Output is correct
35 Correct 10 ms 300 KB Output is correct
36 Correct 13 ms 208 KB Output is correct
37 Correct 10 ms 208 KB Output is correct
38 Correct 9 ms 208 KB Output is correct
39 Correct 11 ms 208 KB Output is correct
40 Correct 9 ms 308 KB Output is correct
41 Correct 8 ms 208 KB Output is correct
42 Correct 8 ms 304 KB Output is correct
43 Correct 10 ms 304 KB Output is correct
44 Correct 7 ms 208 KB Output is correct
45 Correct 13 ms 336 KB Output is correct
46 Correct 9 ms 312 KB Output is correct
47 Correct 8 ms 208 KB Output is correct
48 Correct 7 ms 208 KB Output is correct
49 Correct 11 ms 208 KB Output is correct
50 Correct 12 ms 236 KB Output is correct
51 Correct 8 ms 208 KB Output is correct
52 Correct 12 ms 208 KB Output is correct
53 Correct 12 ms 208 KB Output is correct
54 Correct 13 ms 304 KB Output is correct
55 Correct 17 ms 252 KB Output is correct
56 Correct 7 ms 208 KB Output is correct
57 Correct 7 ms 208 KB Output is correct
58 Correct 10 ms 208 KB Output is correct
59 Correct 7 ms 208 KB Output is correct
60 Correct 10 ms 208 KB Output is correct
61 Correct 12 ms 208 KB Output is correct
62 Correct 10 ms 208 KB Output is correct
63 Correct 11 ms 308 KB Output is correct
64 Correct 6 ms 208 KB Output is correct
65 Correct 11 ms 208 KB Output is correct
66 Correct 9 ms 308 KB Output is correct
67 Correct 10 ms 208 KB Output is correct
68 Correct 13 ms 308 KB Output is correct
69 Correct 13 ms 296 KB Output is correct
70 Correct 13 ms 208 KB Output is correct
71 Correct 9 ms 324 KB Output is correct
72 Correct 9 ms 316 KB Output is correct
73 Correct 11 ms 332 KB Output is correct
74 Correct 8 ms 208 KB Output is correct
75 Correct 12 ms 300 KB Output is correct
76 Correct 11 ms 304 KB Output is correct
77 Correct 8 ms 316 KB Output is correct
78 Correct 9 ms 208 KB Output is correct
79 Correct 12 ms 208 KB Output is correct
80 Correct 9 ms 316 KB Output is correct
81 Correct 14 ms 332 KB Output is correct
82 Correct 13 ms 296 KB Output is correct