답안 #840544

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
840544 2023-08-31T13:37:09 Z arbuzick 가장 긴 여행 (IOI23_longesttrip) C++17
70 / 100
83 ms 464 KB
#include <bits/stdc++.h>

#include "longesttrip.h"

using namespace std;

vector<int> longest_trip(int n, int d) {
    if (d == 3) {
        vector<int> ans(n);
        iota(ans.begin(), ans.end(), 0);
        return ans;
    }
    if (d == 2) {
        vector<int> ans = {0};
        vector<int> used(n);
        used[0] = 1;
        while (ans.size() < n) {
            int nxt = -1;
            for (int i = 0; i < n; ++i) {
                if (!used[i] && are_connected({ans.back()}, {i})) {
                    nxt = i;
                    used[i] = 1;
                    break;
                }
            }
            if (nxt == -1) {
                reverse(ans.begin(), ans.end());
                for (int i = 0; i < n; ++i) {
                    if (!used[i] && are_connected({ans.back()}, {i})) {
                        nxt = i;
                        used[i] = 1;
                        break;
                    }
                }
            }
            ans.push_back(nxt);
        }
        return ans;
    }
    if (d == 1) {
        vector<int> ans = {0};
        vector<int> used(n);
        used[0] = 1;
        auto try_to_add = [&]() {
            vector<int> vals;
            for (int i = 0; i < n; ++i) {
                if (!used[i]) {
                    vals.push_back(i);
                }
            }
            if (vals.empty()) {
                return false;
            }
            if (!are_connected({ans.back()}, vals)) {
                return false;
            }
            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({ans.back()}, vals1)) {
                    vals = vals1;
                } else {
                    vals = vals2;
                }
            }
            used[vals[0]] = 1;
            ans.push_back(vals[0]);
            return true;
        };
        while (true) {
            if (!try_to_add()) {
                break;
            }
        }
        if (ans.size() < n) {
            reverse(ans.begin(), ans.end());
            if (try_to_add()) {
                while (true) {
                    if (!try_to_add()) {
                        break;
                    }
                }
            } else {
                for (int _ = 0; _ < (int)ans.size(); ++_) {
                    reverse(ans.begin(), ans.end());
                    int vl = ans.back();
                    ans.pop_back();
                    reverse(ans.begin(), ans.end());
                    ans.push_back(vl);
                    if (try_to_add()) {
                        while (true) {
                            if (!try_to_add()) {
                                break;
                            }
                        }
                        break;
                    }
                }
            }
        }
        if (ans.size() * 2 < n) {
            ans.clear();
            for (int i = 0; i < n; ++i) {
                if (!used[i]) {
                    ans.push_back(i);
                }
            }
        }
        return ans;
    }
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:17:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   17 |         while (ans.size() < n) {
      |                ~~~~~~~~~~~^~~
longesttrip.cpp:82:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   82 |         if (ans.size() < n) {
      |             ~~~~~~~~~~~^~~
longesttrip.cpp:108:28: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  108 |         if (ans.size() * 2 < n) {
      |             ~~~~~~~~~~~~~~~^~~
longesttrip.cpp:118:1: warning: control reaches end of non-void function [-Wreturn-type]
  118 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 260 KB Output is correct
2 Correct 19 ms 316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 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 7 ms 208 KB Output is correct
5 Correct 7 ms 208 KB Output is correct
6 Correct 8 ms 208 KB Output is correct
7 Correct 5 ms 208 KB Output is correct
8 Correct 6 ms 208 KB Output is correct
9 Correct 7 ms 208 KB Output is correct
10 Correct 8 ms 208 KB Output is correct
11 Correct 7 ms 208 KB Output is correct
12 Correct 7 ms 208 KB Output is correct
13 Correct 9 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 208 KB Output is correct
2 Correct 18 ms 208 KB Output is correct
3 Correct 35 ms 208 KB Output is correct
4 Correct 51 ms 208 KB Output is correct
5 Correct 55 ms 208 KB Output is correct
6 Correct 12 ms 208 KB Output is correct
7 Correct 19 ms 208 KB Output is correct
8 Correct 44 ms 208 KB Output is correct
9 Correct 44 ms 208 KB Output is correct
10 Correct 69 ms 316 KB Output is correct
11 Correct 66 ms 208 KB Output is correct
12 Correct 83 ms 208 KB Output is correct
13 Correct 73 ms 208 KB Output is correct
14 Correct 13 ms 208 KB Output is correct
15 Correct 16 ms 208 KB Output is correct
16 Correct 24 ms 208 KB Output is correct
17 Correct 33 ms 208 KB Output is correct
18 Correct 35 ms 208 KB Output is correct
19 Correct 46 ms 208 KB Output is correct
20 Correct 48 ms 208 KB Output is correct
21 Correct 64 ms 324 KB Output is correct
22 Correct 81 ms 208 KB Output is correct
23 Correct 70 ms 340 KB Output is correct
24 Correct 66 ms 324 KB Output is correct
25 Correct 11 ms 332 KB Output is correct
26 Correct 15 ms 208 KB Output is correct
27 Correct 23 ms 208 KB Output is correct
28 Correct 23 ms 208 KB Output is correct
29 Correct 30 ms 208 KB Output is correct
30 Correct 48 ms 208 KB Output is correct
31 Correct 38 ms 208 KB Output is correct
32 Correct 38 ms 208 KB Output is correct
33 Correct 51 ms 208 KB Output is correct
34 Correct 60 ms 208 KB Output is correct
35 Correct 56 ms 208 KB Output is correct
36 Correct 72 ms 208 KB Output is correct
37 Correct 60 ms 312 KB Output is correct
38 Correct 78 ms 320 KB Output is correct
39 Correct 72 ms 208 KB Output is correct
40 Correct 57 ms 208 KB Output is correct
41 Correct 62 ms 208 KB Output is correct
42 Correct 56 ms 324 KB Output is correct
43 Correct 51 ms 208 KB Output is correct
44 Correct 54 ms 208 KB Output is correct
45 Correct 13 ms 208 KB Output is correct
46 Correct 12 ms 208 KB Output is correct
47 Correct 25 ms 208 KB Output is correct
48 Correct 19 ms 208 KB Output is correct
49 Correct 25 ms 208 KB Output is correct
50 Correct 48 ms 208 KB Output is correct
51 Correct 45 ms 208 KB Output is correct
52 Correct 45 ms 208 KB Output is correct
53 Correct 58 ms 208 KB Output is correct
54 Correct 43 ms 332 KB Output is correct
55 Correct 48 ms 208 KB Output is correct
56 Correct 73 ms 320 KB Output is correct
57 Correct 73 ms 328 KB Output is correct
58 Correct 74 ms 208 KB Output is correct
59 Correct 63 ms 336 KB Output is correct
60 Correct 62 ms 332 KB Output is correct
61 Correct 71 ms 344 KB Output is correct
62 Correct 71 ms 208 KB Output is correct
63 Correct 74 ms 208 KB Output is correct
64 Correct 68 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 208 KB Output is correct
2 Correct 17 ms 208 KB Output is correct
3 Correct 43 ms 208 KB Output is correct
4 Partially correct 48 ms 260 KB Output is partially correct
5 Partially correct 76 ms 332 KB Output is partially correct
6 Correct 10 ms 208 KB Output is correct
7 Correct 19 ms 208 KB Output is correct
8 Correct 37 ms 208 KB Output is correct
9 Partially correct 44 ms 208 KB Output is partially correct
10 Partially correct 55 ms 336 KB Output is partially correct
11 Partially correct 69 ms 328 KB Output is partially correct
12 Partially correct 75 ms 336 KB Output is partially correct
13 Partially correct 73 ms 208 KB Output is partially correct
14 Correct 12 ms 208 KB Output is correct
15 Correct 19 ms 208 KB Output is correct
16 Correct 20 ms 208 KB Output is correct
17 Correct 28 ms 208 KB Output is correct
18 Correct 28 ms 208 KB Output is correct
19 Partially correct 50 ms 208 KB Output is partially correct
20 Partially correct 52 ms 208 KB Output is partially correct
21 Correct 14 ms 208 KB Output is correct
22 Correct 17 ms 208 KB Output is correct
23 Correct 27 ms 208 KB Output is correct
24 Correct 31 ms 208 KB Output is correct
25 Correct 20 ms 208 KB Output is correct
26 Partially correct 49 ms 208 KB Output is partially correct
27 Correct 37 ms 208 KB Output is correct
28 Correct 31 ms 208 KB Output is correct
29 Partially correct 50 ms 208 KB Output is partially correct
30 Partially correct 53 ms 208 KB Output is partially correct
31 Partially correct 54 ms 208 KB Output is partially correct
32 Correct 10 ms 208 KB Output is correct
33 Correct 9 ms 208 KB Output is correct
34 Correct 23 ms 208 KB Output is correct
35 Correct 20 ms 208 KB Output is correct
36 Correct 20 ms 332 KB Output is correct
37 Correct 40 ms 208 KB Output is correct
38 Correct 44 ms 208 KB Output is correct
39 Correct 42 ms 208 KB Output is correct
40 Partially correct 58 ms 208 KB Output is partially correct
41 Partially correct 43 ms 208 KB Output is partially correct
42 Partially correct 58 ms 208 KB Output is partially correct
43 Partially correct 61 ms 328 KB Output is partially correct
44 Partially correct 66 ms 336 KB Output is partially correct
45 Partially correct 68 ms 336 KB Output is partially correct
46 Partially correct 62 ms 328 KB Output is partially correct
47 Partially correct 81 ms 336 KB Output is partially correct
48 Partially correct 81 ms 208 KB Output is partially correct
49 Partially correct 75 ms 336 KB Output is partially correct
50 Partially correct 67 ms 208 KB Output is partially correct
51 Partially correct 63 ms 464 KB Output is partially correct
52 Partially correct 50 ms 208 KB Output is partially correct
53 Partially correct 55 ms 208 KB Output is partially correct
54 Partially correct 60 ms 324 KB Output is partially correct
55 Partially correct 52 ms 208 KB Output is partially correct
56 Partially correct 78 ms 208 KB Output is partially correct
57 Partially correct 65 ms 208 KB Output is partially correct
58 Partially correct 50 ms 208 KB Output is partially correct
59 Partially correct 51 ms 328 KB Output is partially correct
60 Partially correct 56 ms 208 KB Output is partially correct
61 Partially correct 60 ms 336 KB Output is partially correct
62 Partially correct 62 ms 332 KB Output is partially correct
63 Partially correct 63 ms 328 KB Output is partially correct
64 Partially correct 69 ms 208 KB Output is partially correct
65 Partially correct 73 ms 336 KB Output is partially correct
66 Partially correct 76 ms 208 KB Output is partially correct
67 Partially correct 68 ms 336 KB Output is partially correct
68 Partially correct 67 ms 208 KB Output is partially correct
69 Partially correct 81 ms 324 KB Output is partially correct
70 Partially correct 63 ms 312 KB Output is partially correct
71 Partially correct 70 ms 332 KB Output is partially correct
72 Partially correct 77 ms 208 KB Output is partially correct
73 Partially correct 64 ms 208 KB Output is partially correct
74 Partially correct 74 ms 208 KB Output is partially correct
75 Partially correct 73 ms 324 KB Output is partially correct
76 Partially correct 74 ms 208 KB Output is partially correct
77 Partially correct 67 ms 336 KB Output is partially correct
78 Partially correct 76 ms 208 KB Output is partially correct
79 Partially correct 66 ms 316 KB Output is partially correct
80 Partially correct 79 ms 312 KB Output is partially correct
81 Partially correct 80 ms 208 KB Output is partially correct
82 Partially correct 50 ms 328 KB Output is partially correct