Submission #839743

# Submission time Handle Problem Language Result Execution time Memory
839743 2023-08-30T15:49:43 Z model_code Longest Trip (IOI23_longesttrip) C++17
70 / 100
86 ms 464 KB
// partially_correct/sol_na_D1_nlogn.cpp

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

bool are_connected2(std::vector<int> a, std::vector<int> b)
{
    // for(auto i:a) std::cerr<<i<<" ";std::cerr<<"a\n";
    // for(auto i:b) std::cerr<<i<<" ";std::cerr<<"b\n";
    return are_connected(a, b);
}

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::vector<std::deque<int>> lst;
    int i;
    auto make = [&]() -> std::vector<int>
    {
        if (lst.back().size() == 1)
            return {lst.back()[0]};
        return {lst.back()[0], lst.back().back()};
    };
    auto rest = [&]() -> std::vector<int>
    {
        std::vector<int> ans;
        for (int j = i + 1; j < N; ++j)
        {
            ans.push_back(ids[j]);
        }
        return ans;
    };
    for (i = 0; i < N; i++)
    {
        if (i > 0)
        {
            auto x = rest();
            x.push_back(ids[i]);
            lst.push_back(std::deque<int>(x.begin(), x.end()));
            break;
        }
        int id = ids[i];
        lst.push_back({id});
        while (!rest().empty() && are_connected2(make(), rest()))
        {
            auto l = rest();
            int until = int(l.size()) - 1;
            for (int j = 20; j >= 0; j--)
            {
                if (until - (1 << j) >= 0)
                {
                    auto other = std::vector<int>(l.begin(), l.begin() + until - (1 << j) + 1);
                    if (are_connected2(make(), other))
                    {
                        until -= (1 << j);
                    }
                }
            }
            if (are_connected2({lst.back()[0]}, {l[until]}))
            {
                lst.back().push_front(l[until]);
            }
            else
            {
                lst.back().push_back(l[until]);
            }
            std::swap(ids[i + 1], ids[i + 1 + until]);

            i++;
            if (i == N)
            {
                break;
            }
        }
    }

    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 (lst.size() == 1 ? std::vector<int>(lst[0].begin(), lst[0].end()) : solve(lst[0], lst[1]));
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 13 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 208 KB Output is correct
2 Correct 14 ms 208 KB Output is correct
3 Correct 34 ms 208 KB Output is correct
4 Correct 42 ms 316 KB Output is correct
5 Correct 67 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 208 KB Output is correct
2 Correct 30 ms 208 KB Output is correct
3 Correct 36 ms 208 KB Output is correct
4 Correct 47 ms 208 KB Output is correct
5 Correct 67 ms 348 KB Output is correct
6 Correct 14 ms 208 KB Output is correct
7 Correct 27 ms 208 KB Output is correct
8 Correct 35 ms 208 KB Output is correct
9 Correct 41 ms 320 KB Output is correct
10 Correct 57 ms 348 KB Output is correct
11 Correct 59 ms 336 KB Output is correct
12 Correct 54 ms 336 KB Output is correct
13 Correct 57 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 208 KB Output is correct
2 Correct 29 ms 208 KB Output is correct
3 Correct 34 ms 208 KB Output is correct
4 Correct 51 ms 208 KB Output is correct
5 Correct 56 ms 344 KB Output is correct
6 Correct 13 ms 208 KB Output is correct
7 Correct 26 ms 208 KB Output is correct
8 Correct 34 ms 256 KB Output is correct
9 Correct 40 ms 208 KB Output is correct
10 Correct 54 ms 356 KB Output is correct
11 Correct 59 ms 336 KB Output is correct
12 Correct 56 ms 352 KB Output is correct
13 Correct 60 ms 336 KB Output is correct
14 Correct 14 ms 208 KB Output is correct
15 Correct 15 ms 208 KB Output is correct
16 Correct 31 ms 208 KB Output is correct
17 Correct 38 ms 308 KB Output is correct
18 Correct 35 ms 284 KB Output is correct
19 Correct 37 ms 304 KB Output is correct
20 Correct 47 ms 308 KB Output is correct
21 Correct 49 ms 348 KB Output is correct
22 Correct 57 ms 356 KB Output is correct
23 Correct 58 ms 340 KB Output is correct
24 Correct 58 ms 348 KB Output is correct
25 Correct 20 ms 208 KB Output is correct
26 Correct 13 ms 208 KB Output is correct
27 Correct 25 ms 208 KB Output is correct
28 Correct 26 ms 208 KB Output is correct
29 Correct 22 ms 208 KB Output is correct
30 Correct 28 ms 300 KB Output is correct
31 Correct 37 ms 208 KB Output is correct
32 Correct 38 ms 208 KB Output is correct
33 Correct 29 ms 304 KB Output is correct
34 Correct 46 ms 208 KB Output is correct
35 Correct 41 ms 292 KB Output is correct
36 Correct 40 ms 352 KB Output is correct
37 Correct 67 ms 332 KB Output is correct
38 Correct 74 ms 336 KB Output is correct
39 Correct 76 ms 336 KB Output is correct
40 Correct 59 ms 336 KB Output is correct
41 Correct 56 ms 464 KB Output is correct
42 Correct 61 ms 336 KB Output is correct
43 Correct 55 ms 336 KB Output is correct
44 Correct 62 ms 336 KB Output is correct
45 Correct 16 ms 208 KB Output is correct
46 Correct 12 ms 208 KB Output is correct
47 Correct 24 ms 208 KB Output is correct
48 Correct 16 ms 208 KB Output is correct
49 Correct 30 ms 208 KB Output is correct
50 Correct 54 ms 304 KB Output is correct
51 Correct 40 ms 208 KB Output is correct
52 Correct 47 ms 208 KB Output is correct
53 Correct 45 ms 308 KB Output is correct
54 Correct 49 ms 208 KB Output is correct
55 Correct 45 ms 312 KB Output is correct
56 Correct 62 ms 432 KB Output is correct
57 Correct 69 ms 344 KB Output is correct
58 Correct 76 ms 344 KB Output is correct
59 Correct 75 ms 356 KB Output is correct
60 Correct 73 ms 336 KB Output is correct
61 Correct 65 ms 328 KB Output is correct
62 Correct 61 ms 332 KB Output is correct
63 Correct 83 ms 340 KB Output is correct
64 Correct 59 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 208 KB Output is correct
2 Correct 25 ms 208 KB Output is correct
3 Correct 25 ms 336 KB Output is correct
4 Partially correct 49 ms 208 KB Output is partially correct
5 Partially correct 54 ms 336 KB Output is partially correct
6 Correct 13 ms 208 KB Output is correct
7 Correct 20 ms 208 KB Output is correct
8 Correct 28 ms 208 KB Output is correct
9 Partially correct 38 ms 208 KB Output is partially correct
10 Partially correct 52 ms 292 KB Output is partially correct
11 Partially correct 52 ms 336 KB Output is partially correct
12 Partially correct 64 ms 336 KB Output is partially correct
13 Partially correct 53 ms 336 KB Output is partially correct
14 Correct 16 ms 208 KB Output is correct
15 Correct 20 ms 208 KB Output is correct
16 Correct 30 ms 208 KB Output is correct
17 Correct 31 ms 308 KB Output is correct
18 Correct 32 ms 284 KB Output is correct
19 Partially correct 38 ms 208 KB Output is partially correct
20 Partially correct 44 ms 292 KB Output is partially correct
21 Correct 15 ms 208 KB Output is correct
22 Correct 15 ms 208 KB Output is correct
23 Correct 21 ms 208 KB Output is correct
24 Correct 26 ms 208 KB Output is correct
25 Correct 27 ms 208 KB Output is correct
26 Correct 45 ms 208 KB Output is correct
27 Correct 38 ms 208 KB Output is correct
28 Correct 32 ms 208 KB Output is correct
29 Partially correct 47 ms 212 KB Output is partially correct
30 Partially correct 37 ms 208 KB Output is partially correct
31 Partially correct 53 ms 208 KB Output is partially correct
32 Correct 15 ms 208 KB Output is correct
33 Correct 22 ms 332 KB Output is correct
34 Correct 24 ms 208 KB Output is correct
35 Correct 23 ms 208 KB Output is correct
36 Correct 28 ms 208 KB Output is correct
37 Correct 32 ms 304 KB Output is correct
38 Correct 52 ms 208 KB Output is correct
39 Correct 43 ms 208 KB Output is correct
40 Partially correct 44 ms 208 KB Output is partially correct
41 Partially correct 44 ms 208 KB Output is partially correct
42 Partially correct 49 ms 308 KB Output is partially correct
43 Partially correct 62 ms 336 KB Output is partially correct
44 Partially correct 69 ms 336 KB Output is partially correct
45 Partially correct 53 ms 336 KB Output is partially correct
46 Partially correct 60 ms 344 KB Output is partially correct
47 Partially correct 55 ms 332 KB Output is partially correct
48 Partially correct 73 ms 332 KB Output is partially correct
49 Partially correct 58 ms 336 KB Output is partially correct
50 Partially correct 79 ms 332 KB Output is partially correct
51 Partially correct 57 ms 336 KB Output is partially correct
52 Partially correct 62 ms 392 KB Output is partially correct
53 Partially correct 61 ms 336 KB Output is partially correct
54 Partially correct 55 ms 336 KB Output is partially correct
55 Partially correct 55 ms 336 KB Output is partially correct
56 Partially correct 55 ms 340 KB Output is partially correct
57 Partially correct 47 ms 356 KB Output is partially correct
58 Partially correct 73 ms 336 KB Output is partially correct
59 Partially correct 54 ms 336 KB Output is partially correct
60 Partially correct 55 ms 336 KB Output is partially correct
61 Partially correct 61 ms 340 KB Output is partially correct
62 Partially correct 73 ms 336 KB Output is partially correct
63 Partially correct 63 ms 340 KB Output is partially correct
64 Partially correct 65 ms 336 KB Output is partially correct
65 Partially correct 73 ms 384 KB Output is partially correct
66 Partially correct 59 ms 340 KB Output is partially correct
67 Partially correct 67 ms 324 KB Output is partially correct
68 Partially correct 62 ms 336 KB Output is partially correct
69 Partially correct 71 ms 324 KB Output is partially correct
70 Partially correct 86 ms 336 KB Output is partially correct
71 Partially correct 65 ms 332 KB Output is partially correct
72 Partially correct 70 ms 332 KB Output is partially correct
73 Partially correct 63 ms 344 KB Output is partially correct
74 Partially correct 62 ms 336 KB Output is partially correct
75 Partially correct 66 ms 340 KB Output is partially correct
76 Partially correct 59 ms 344 KB Output is partially correct
77 Partially correct 48 ms 336 KB Output is partially correct
78 Partially correct 77 ms 336 KB Output is partially correct
79 Partially correct 62 ms 356 KB Output is partially correct
80 Partially correct 64 ms 336 KB Output is partially correct
81 Partially correct 68 ms 332 KB Output is partially correct
82 Partially correct 66 ms 336 KB Output is partially correct