답안 #846937

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
846937 2023-09-08T17:24:05 Z arbuzick 가장 긴 여행 (IOI23_longesttrip) C++17
100 / 100
16 ms 856 KB
#include <bits/stdc++.h>

#include "longesttrip.h"

using namespace std;

vector<int> longest_trip(int n, int d) {
    mt19937 rnd(57);
    vector<int> ord(n);
    iota(ord.begin(), ord.end(), 0);
    shuffle(ord.begin(), ord.end(), rnd);
    vector<int> ans1, ans2;
    bool disconnected = false;
    for (int i = 0; i < n; ++i) {
        if (ans1.empty() || are_connected({ans1.back()}, {ord[i]})) {
            ans1.push_back(ord[i]);
            disconnected = false;
        } else if (disconnected) {
            ans2.push_back(ord[i]);
        } else if (ans2.empty() || are_connected({ans2.back()}, {ord[i]})) {
            ans2.push_back(ord[i]);
            disconnected = true;
        } else {
            if (i == n - 1) {
                reverse(ans2.begin(), ans2.end());
                for (auto vl : ans2) {
                    ans1.push_back(vl);
                }
                ans2 = {ord[i]};
            } else {
                if (are_connected({ord[i]}, {ord[i + 1]})) {
                    reverse(ans2.begin(), ans2.end());
                    for (auto vl : ans2) {
                        ans1.push_back(vl);
                    }
                    ans2 = {ord[i], ord[i + 1]};
                } else {
                    ans1.push_back(ord[i + 1]);
                    reverse(ans2.begin(), ans2.end());
                    for (auto vl : ans2) {
                        ans1.push_back(vl);
                    }
                    ans2 = {ord[i]};
                }
                disconnected = false;
                ++i;
            }
        }
    }
    if (ans1.size() < ans2.size()) {
        swap(ans1, ans2);
    }
    auto try_to_add = [&](int last, vector<int> vals) {
        if (vals.empty()) {
            return -1;
        }
        if (!are_connected({last}, vals)) {
            return -1;
        }
        while (vals.size() > 1) {
            int m = vals.size() / 2;
            vector<int> vals1, vals2;
            for (int i = 0; i < (int)vals.size(); ++i) {
                if (i < m) {
                    vals1.push_back(vals[i]);
                } else {
                    vals2.push_back(vals[i]);
                }
            }
            if (are_connected({last}, vals1)) {
                vals = vals1;
            } else {
                vals = vals2;
            }
        }
        return vals[0];
    };
    if (!ans2.empty()) {
        if (are_connected({ans1.back()}, {ans2.back()})) {
            reverse(ans2.begin(), ans2.end());
            for (auto vl : ans2) {
                ans1.push_back(vl);
            }
            ans2.clear();
        } else if (are_connected({ans1.back()}, {ans2[0]})) {
            for (auto vl : ans2) {
                ans1.push_back(vl);
            }
            ans2.clear();
        } else if (are_connected({ans1[0]}, {ans2.back()})) {
            reverse(ans1.begin(), ans1.end());
            reverse(ans2.begin(), ans2.end());
            for (auto vl : ans2) {
                ans1.push_back(vl);
            }
            ans2.clear();
        } else if (are_connected({ans1[0]}, {ans2[0]})) {
            reverse(ans1.begin(), ans1.end());
            for (auto vl : ans2) {
                ans1.push_back(vl);
            }
            ans2.clear();
        }
    }
    if (!ans2.empty()) {
        if (are_connected(ans1, ans2)) {
            int l = 0, r = ans1.size();
            while (l < r - 1) {
                int m = (l + r) / 2;
                vector<int> nw;
                for (int i = l; i < m; ++i) {
                    nw.push_back(ans1[i]);
                }
                if (are_connected(nw, ans2)) {
                    r = m;
                } else {
                    l = m;
                }
            }
            vector<int> ll, rr;
            for (auto vl : ans1) {
                if (ll.empty() || ll.back() != ans1[l]) {
                    ll.push_back(vl);
                } else {
                    rr.push_back(vl);
                }
            }
            ans1.clear();
            for (auto vl : rr) {
                ans1.push_back(vl);
            }
            for (auto vl : ll) {
                ans1.push_back(vl);
            }
            int val = try_to_add(ans1.back(), ans2);
            if (val != -1) {
                ll.clear(), rr.clear();
                for (auto vl : ans2) {
                    if (rr.empty() && vl != val) {
                        ll.push_back(vl);
                    } else {
                        rr.push_back(vl);
                    }
                }
                for (auto vl : rr) {
                    ans1.push_back(vl);
                }
                for (auto vl : ll) {
                    ans1.push_back(vl);
                }
                ans2.clear();
            }
        }
    }
    return ans1;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 4 ms 344 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 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 4 ms 344 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 9 ms 344 KB Output is correct
7 Correct 7 ms 344 KB Output is correct
8 Correct 4 ms 344 KB Output is correct
9 Correct 4 ms 344 KB Output is correct
10 Correct 6 ms 756 KB Output is correct
11 Correct 4 ms 344 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 5 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 4 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 10 ms 344 KB Output is correct
7 Correct 5 ms 344 KB Output is correct
8 Correct 4 ms 344 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 4 ms 344 KB Output is correct
11 Correct 4 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 15 ms 496 KB Output is correct
15 Correct 11 ms 344 KB Output is correct
16 Correct 6 ms 344 KB Output is correct
17 Correct 6 ms 344 KB Output is correct
18 Correct 5 ms 344 KB Output is correct
19 Correct 5 ms 344 KB Output is correct
20 Correct 5 ms 344 KB Output is correct
21 Correct 5 ms 344 KB Output is correct
22 Correct 5 ms 344 KB Output is correct
23 Correct 4 ms 344 KB Output is correct
24 Correct 6 ms 344 KB Output is correct
25 Correct 13 ms 344 KB Output is correct
26 Correct 13 ms 344 KB Output is correct
27 Correct 10 ms 344 KB Output is correct
28 Correct 7 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 6 ms 344 KB Output is correct
32 Correct 6 ms 344 KB Output is correct
33 Correct 5 ms 600 KB Output is correct
34 Correct 5 ms 600 KB Output is correct
35 Correct 5 ms 600 KB Output is correct
36 Correct 5 ms 344 KB Output is correct
37 Correct 5 ms 344 KB Output is correct
38 Correct 6 ms 344 KB Output is correct
39 Correct 7 ms 340 KB Output is correct
40 Correct 5 ms 344 KB Output is correct
41 Correct 6 ms 344 KB Output is correct
42 Correct 6 ms 344 KB Output is correct
43 Correct 5 ms 344 KB Output is correct
44 Correct 5 ms 344 KB Output is correct
45 Correct 9 ms 344 KB Output is correct
46 Correct 9 ms 344 KB Output is correct
47 Correct 10 ms 344 KB Output is correct
48 Correct 10 ms 344 KB Output is correct
49 Correct 13 ms 344 KB Output is correct
50 Correct 6 ms 344 KB Output is correct
51 Correct 8 ms 440 KB Output is correct
52 Correct 7 ms 344 KB Output is correct
53 Correct 5 ms 344 KB Output is correct
54 Correct 5 ms 600 KB Output is correct
55 Correct 6 ms 344 KB Output is correct
56 Correct 5 ms 600 KB Output is correct
57 Correct 5 ms 448 KB Output is correct
58 Correct 8 ms 444 KB Output is correct
59 Correct 6 ms 444 KB Output is correct
60 Correct 7 ms 856 KB Output is correct
61 Correct 7 ms 600 KB Output is correct
62 Correct 7 ms 600 KB Output is correct
63 Correct 6 ms 444 KB Output is correct
64 Correct 6 ms 444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 5 ms 352 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 6 ms 344 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 4 ms 344 KB Output is correct
11 Correct 4 ms 344 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 4 ms 344 KB Output is correct
14 Correct 15 ms 344 KB Output is correct
15 Correct 12 ms 344 KB Output is correct
16 Correct 8 ms 344 KB Output is correct
17 Correct 5 ms 344 KB Output is correct
18 Correct 5 ms 344 KB Output is correct
19 Correct 5 ms 344 KB Output is correct
20 Correct 5 ms 344 KB Output is correct
21 Correct 16 ms 344 KB Output is correct
22 Correct 12 ms 344 KB Output is correct
23 Correct 8 ms 344 KB Output is correct
24 Correct 7 ms 344 KB Output is correct
25 Correct 9 ms 344 KB Output is correct
26 Correct 5 ms 344 KB Output is correct
27 Correct 6 ms 344 KB Output is correct
28 Correct 5 ms 344 KB Output is correct
29 Correct 5 ms 600 KB Output is correct
30 Correct 5 ms 440 KB Output is correct
31 Correct 6 ms 600 KB Output is correct
32 Correct 10 ms 344 KB Output is correct
33 Correct 8 ms 344 KB Output is correct
34 Correct 10 ms 344 KB Output is correct
35 Correct 8 ms 344 KB Output is correct
36 Correct 11 ms 344 KB Output is correct
37 Correct 5 ms 344 KB Output is correct
38 Correct 7 ms 440 KB Output is correct
39 Correct 6 ms 344 KB Output is correct
40 Correct 6 ms 344 KB Output is correct
41 Correct 5 ms 344 KB Output is correct
42 Correct 5 ms 344 KB Output is correct
43 Correct 5 ms 344 KB Output is correct
44 Correct 5 ms 600 KB Output is correct
45 Correct 5 ms 344 KB Output is correct
46 Correct 5 ms 344 KB Output is correct
47 Correct 4 ms 344 KB Output is correct
48 Correct 5 ms 344 KB Output is correct
49 Correct 5 ms 344 KB Output is correct
50 Correct 5 ms 344 KB Output is correct
51 Correct 6 ms 600 KB Output is correct
52 Correct 5 ms 344 KB Output is correct
53 Correct 5 ms 344 KB Output is correct
54 Correct 5 ms 600 KB Output is correct
55 Correct 6 ms 344 KB Output is correct
56 Correct 6 ms 600 KB Output is correct
57 Correct 6 ms 600 KB Output is correct
58 Correct 5 ms 344 KB Output is correct
59 Correct 6 ms 344 KB Output is correct
60 Correct 5 ms 344 KB Output is correct
61 Correct 5 ms 344 KB Output is correct
62 Correct 5 ms 600 KB Output is correct
63 Correct 6 ms 448 KB Output is correct
64 Correct 5 ms 440 KB Output is correct
65 Correct 6 ms 440 KB Output is correct
66 Correct 7 ms 440 KB Output is correct
67 Correct 7 ms 856 KB Output is correct
68 Correct 7 ms 600 KB Output is correct
69 Correct 6 ms 600 KB Output is correct
70 Correct 8 ms 600 KB Output is correct
71 Correct 6 ms 440 KB Output is correct
72 Correct 7 ms 440 KB Output is correct
73 Correct 6 ms 444 KB Output is correct
74 Correct 6 ms 604 KB Output is correct
75 Correct 6 ms 440 KB Output is correct
76 Correct 7 ms 444 KB Output is correct
77 Correct 5 ms 604 KB Output is correct
78 Correct 7 ms 856 KB Output is correct
79 Correct 8 ms 600 KB Output is correct
80 Correct 7 ms 600 KB Output is correct
81 Correct 9 ms 440 KB Output is correct
82 Correct 6 ms 692 KB Output is correct