Submission #839873

# Submission time Handle Problem Language Result Execution time Memory
839873 2023-08-30T19:15:21 Z tamionv Longest Trip (IOI23_longesttrip) C++17
85 / 100
23 ms 436 KB
#include "longesttrip.h"

#include <bits/stdc++.h>

using namespace std;

mt19937 mt(123);

bool my_are_connected(vector<int> a, vector<int> b) {
    a.erase(unique(begin(a), end(a)), end(a));
    b.erase(unique(begin(b), end(b)), end(b));
    if (a.empty() || b.empty()) return false;
    return are_connected(a, b);
}

pair<int, int> cbin(vector<int> as, vector<int> bs) {
    shuffle(begin(as), end(as), mt);
    shuffle(begin(bs), end(bs), mt);

    bool swp = false;

    if (as.size() > bs.size()) {
        swap(as, bs);
        swp = true;
    }
    pair<int, int> ret;

    if (bs.size() == 1)
        ret = make_pair(as[0], bs[0]);
    else {
        vector<int> xs(begin(bs), begin(bs) + bs.size() / 2), ys(begin(bs) + bs.size() / 2, end(bs));

        if (my_are_connected(as, xs))
            ret = cbin(as, xs);
        else
            ret = cbin(as, ys);
    }

    if (swp) swap(ret.first, ret.second);
    return ret;
}

std::vector<int> longest_trip(int N, int D) {
    mt = mt19937(123 + N + D);
    vector<int> perm(N);
    iota(begin(perm), end(perm), 0);
    shuffle(begin(perm), end(perm), mt);

    vector<int> xs = {}, ys = {};

    for (auto x : perm) {
        if (xs.empty())
            xs.push_back(x);
        else if (ys.empty()) {
            if (my_are_connected({x}, {xs.back()}))
                xs.push_back(x);
            else
                ys.push_back(x);
        } else {
            if (bernoulli_distribution(0.5)(mt)) {
                if (!my_are_connected({x}, {xs.back()}))
                    ys.push_back(x);
                else if (!my_are_connected({x}, {ys.back()}))
                    xs.push_back(x);
                else {
                    xs.push_back(x);
                    reverse(begin(ys), end(ys));
                    xs.insert(end(xs), begin(ys), end(ys));
                    ys = {};
                }
            } else {
                if (!my_are_connected({x}, {ys.back()}))
                    xs.push_back(x);
                else if (!my_are_connected({x}, {xs.back()}))
                    ys.push_back(x);
                else {
                    xs.push_back(x);
                    reverse(begin(ys), end(ys));
                    xs.insert(end(xs), begin(ys), end(ys));
                    ys = {};
                }
            }
        }
    }

    vector<int> ret = {};
    if (ys.empty())
        ret = xs;
    else {
        if (!my_are_connected(xs, ys))
            ret = (xs.size() < ys.size() ? ys : xs);
        else if (my_are_connected({xs.front(), xs.back()}, {ys.front(), ys.back()})) {
            auto [x, y] = cbin({xs.front(), xs.back()}, {ys.front(), ys.back()});
            if (xs.back() != x) reverse(begin(xs), end(xs));
            if (ys.front() != y) reverse(begin(ys), end(ys));
            xs.insert(end(xs), begin(ys), end(ys));
            ret = xs;
        } else {
            auto [x, y] = cbin(xs, ys);
            rotate(begin(xs), find(begin(xs), end(xs), x), end(xs));
            rotate(begin(ys), find(begin(ys), end(ys), y), end(ys));
            reverse(begin(xs), end(xs));
            xs.insert(end(xs), begin(ys), end(ys));
            ret = xs;
        }
    }

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