Submission #839742

# Submission time Handle Problem Language Result Execution time Memory
839742 2023-08-30T15:49:41 Z model_code Longest Trip (IOI23_longesttrip) C++17
85 / 100
17 ms 420 KB
// partially_correct/sol_na_D1.cpp

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

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 = {ids[0]}, B;
    for (int j = 1; j < N; ++j)
    {
        int i = ids[j];
        if (are_connected({A.back()}, {i}))
        {
            A.push_back(i);
        }
        else if (B.empty() || are_connected({B.back()}, {i}))
        {
            B.push_back(i);
        }
        else
        {
            while (!B.empty())
            {
                A.push_back(B.back());
                B.pop_back();
            }
            B = {i};
        }
    }
    if (A.size() > B.size())
        A.swap(B);

    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 (B.size() == N)
            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);
}

Compilation message

longesttrip.cpp: In lambda function:
longesttrip.cpp:47:22: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   47 |         if (B.size() == N)
      |             ~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 2 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 208 KB Output is correct
2 Correct 5 ms 208 KB Output is correct
3 Correct 7 ms 208 KB Output is correct
4 Correct 7 ms 220 KB Output is correct
5 Correct 4 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 208 KB Output is correct
2 Correct 6 ms 208 KB Output is correct
3 Correct 7 ms 296 KB Output is correct
4 Correct 8 ms 208 KB Output is correct
5 Correct 5 ms 208 KB Output is correct
6 Correct 14 ms 260 KB Output is correct
7 Correct 8 ms 208 KB Output is correct
8 Correct 8 ms 208 KB Output is correct
9 Correct 8 ms 208 KB Output is correct
10 Correct 6 ms 288 KB Output is correct
11 Correct 8 ms 208 KB Output is correct
12 Correct 7 ms 208 KB Output is correct
13 Correct 7 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 208 KB Output is correct
2 Correct 7 ms 208 KB Output is correct
3 Correct 6 ms 208 KB Output is correct
4 Correct 6 ms 208 KB Output is correct
5 Correct 10 ms 308 KB Output is correct
6 Correct 9 ms 208 KB Output is correct
7 Correct 10 ms 208 KB Output is correct
8 Correct 8 ms 304 KB Output is correct
9 Correct 10 ms 208 KB Output is correct
10 Correct 6 ms 208 KB Output is correct
11 Correct 4 ms 208 KB Output is correct
12 Correct 9 ms 208 KB Output is correct
13 Correct 8 ms 208 KB Output is correct
14 Correct 15 ms 332 KB Output is correct
15 Correct 11 ms 332 KB Output is correct
16 Correct 13 ms 208 KB Output is correct
17 Correct 11 ms 208 KB Output is correct
18 Correct 8 ms 208 KB Output is correct
19 Correct 7 ms 208 KB Output is correct
20 Correct 11 ms 208 KB Output is correct
21 Correct 10 ms 208 KB Output is correct
22 Correct 7 ms 304 KB Output is correct
23 Correct 9 ms 208 KB Output is correct
24 Correct 8 ms 208 KB Output is correct
25 Correct 13 ms 208 KB Output is correct
26 Correct 14 ms 208 KB Output is correct
27 Correct 9 ms 364 KB Output is correct
28 Correct 10 ms 208 KB Output is correct
29 Correct 11 ms 208 KB Output is correct
30 Correct 9 ms 208 KB Output is correct
31 Correct 9 ms 208 KB Output is correct
32 Correct 9 ms 208 KB Output is correct
33 Correct 8 ms 208 KB Output is correct
34 Correct 8 ms 208 KB Output is correct
35 Correct 9 ms 208 KB Output is correct
36 Correct 8 ms 304 KB Output is correct
37 Correct 8 ms 208 KB Output is correct
38 Correct 7 ms 208 KB Output is correct
39 Correct 9 ms 208 KB Output is correct
40 Correct 9 ms 208 KB Output is correct
41 Correct 9 ms 208 KB Output is correct
42 Correct 11 ms 208 KB Output is correct
43 Correct 11 ms 208 KB Output is correct
44 Correct 13 ms 208 KB Output is correct
45 Correct 15 ms 208 KB Output is correct
46 Correct 17 ms 208 KB Output is correct
47 Correct 14 ms 248 KB Output is correct
48 Correct 13 ms 208 KB Output is correct
49 Correct 9 ms 208 KB Output is correct
50 Correct 7 ms 208 KB Output is correct
51 Correct 12 ms 208 KB Output is correct
52 Correct 10 ms 208 KB Output is correct
53 Correct 8 ms 208 KB Output is correct
54 Correct 7 ms 208 KB Output is correct
55 Correct 13 ms 304 KB Output is correct
56 Correct 8 ms 208 KB Output is correct
57 Correct 10 ms 320 KB Output is correct
58 Correct 9 ms 320 KB Output is correct
59 Correct 14 ms 316 KB Output is correct
60 Correct 12 ms 420 KB Output is correct
61 Correct 11 ms 208 KB Output is correct
62 Correct 11 ms 312 KB Output is correct
63 Correct 12 ms 328 KB Output is correct
64 Correct 13 ms 240 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 6 ms 336 KB Output is correct
4 Correct 7 ms 208 KB Output is correct
5 Correct 5 ms 208 KB Output is correct
6 Correct 9 ms 208 KB Output is correct
7 Correct 10 ms 208 KB Output is correct
8 Correct 9 ms 248 KB Output is correct
9 Correct 8 ms 208 KB Output is correct
10 Correct 7 ms 208 KB Output is correct
11 Correct 7 ms 208 KB Output is correct
12 Correct 9 ms 208 KB Output is correct
13 Correct 6 ms 208 KB Output is correct
14 Correct 11 ms 208 KB Output is correct
15 Correct 9 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 8 ms 304 KB Output is correct
19 Correct 8 ms 208 KB Output is correct
20 Correct 8 ms 208 KB Output is correct
21 Correct 11 ms 256 KB Output is correct
22 Correct 14 ms 208 KB Output is correct
23 Correct 10 ms 208 KB Output is correct
24 Correct 6 ms 208 KB Output is correct
25 Correct 13 ms 208 KB Output is correct
26 Correct 8 ms 240 KB Output is correct
27 Correct 9 ms 208 KB Output is correct
28 Correct 11 ms 248 KB Output is correct
29 Correct 10 ms 208 KB Output is correct
30 Correct 8 ms 332 KB Output is correct
31 Correct 8 ms 208 KB Output is correct
32 Correct 12 ms 208 KB Output is correct
33 Correct 14 ms 208 KB Output is correct
34 Correct 11 ms 208 KB Output is correct
35 Correct 11 ms 208 KB Output is correct
36 Correct 9 ms 208 KB Output is correct
37 Correct 8 ms 300 KB Output is correct
38 Correct 12 ms 288 KB Output is correct
39 Correct 15 ms 300 KB Output is correct
40 Correct 7 ms 308 KB Output is correct
41 Correct 9 ms 208 KB Output is correct
42 Correct 9 ms 208 KB Output is correct
43 Correct 8 ms 212 KB Output is correct
44 Correct 10 ms 208 KB Output is correct
45 Correct 9 ms 208 KB Output is correct
46 Correct 10 ms 208 KB Output is correct
47 Correct 7 ms 208 KB Output is correct
48 Correct 7 ms 208 KB Output is correct
49 Correct 9 ms 208 KB Output is correct
50 Partially correct 6 ms 208 KB Output is partially correct
51 Partially correct 10 ms 208 KB Output is partially correct
52 Correct 12 ms 208 KB Output is correct
53 Correct 10 ms 304 KB Output is correct
54 Correct 11 ms 208 KB Output is correct
55 Correct 13 ms 208 KB Output is correct
56 Correct 10 ms 208 KB Output is correct
57 Partially correct 11 ms 208 KB Output is partially correct
58 Partially correct 10 ms 260 KB Output is partially correct
59 Partially correct 11 ms 300 KB Output is partially correct
60 Correct 11 ms 208 KB Output is correct
61 Correct 14 ms 208 KB Output is correct
62 Correct 8 ms 208 KB Output is correct
63 Correct 6 ms 212 KB Output is correct
64 Correct 12 ms 208 KB Output is correct
65 Partially correct 14 ms 208 KB Output is partially correct
66 Partially correct 11 ms 312 KB Output is partially correct
67 Partially correct 10 ms 304 KB Output is partially correct
68 Partially correct 12 ms 312 KB Output is partially correct
69 Partially correct 12 ms 208 KB Output is partially correct
70 Partially correct 12 ms 316 KB Output is partially correct
71 Partially correct 11 ms 312 KB Output is partially correct
72 Correct 9 ms 208 KB Output is correct
73 Partially correct 16 ms 208 KB Output is partially correct
74 Partially correct 11 ms 304 KB Output is partially correct
75 Partially correct 12 ms 208 KB Output is partially correct
76 Partially correct 14 ms 208 KB Output is partially correct
77 Correct 11 ms 324 KB Output is correct
78 Correct 9 ms 208 KB Output is correct
79 Partially correct 15 ms 208 KB Output is partially correct
80 Partially correct 15 ms 336 KB Output is partially correct
81 Partially correct 14 ms 308 KB Output is partially correct
82 Partially correct 15 ms 304 KB Output is partially correct