답안 #1048835

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1048835 2024-08-08T09:44:10 Z ZanP 가장 긴 여행 (IOI23_longesttrip) C++17
100 / 100
9 ms 600 KB
#include "longesttrip.h"
#include <iostream>
#include <vector>
using namespace std;

//bool are_connected(vector<int> A, vector<int> B) { return true; }

void sub1(vector<int>& ans, int n) {
    ans.resize(n);
    for (int i = 0; i < n; i++) { ans[i] = i; }
}

void sub2(vector<int>& ans, int n) {
    ans.reserve(n);
    bool are_last_connected = are_connected({ n - 2 }, { n - 1 });
    if (!are_last_connected) { ans.push_back(n - 1); }
    ans.push_back(0);
    for (int i = 0; i < n - 1; i++) {
        if (are_connected({ i }, { i + 1 })) {
            ans.push_back(i + 1);
        }
        else {
            if (i == n - 2) { break; }
            ans.push_back(i + 2);
            ans.push_back(i + 1);
            if (!(i == n - 3 || !are_last_connected && i == n - 4))ans.push_back(i + 3);
            i += 2;
        }
    }
}

void sub3(vector<int>& ans, int n)
{
    ans.reserve(n);
    vector<int> path1;
    vector<int> path2;
    path1.reserve(n);
    path2.reserve(n);
    path1.push_back(0);
    path2.push_back(1);
    bool connectBetween = true;
    for (int i = 2; i < n; i++)
    {
        int a = i;
        bool query = are_connected({ a }, { path1.back() });
        if (query)
        {
            path1.push_back(a);
            connectBetween = true;
        }
        else if (!connectBetween)
        {
            path2.push_back(a);
            connectBetween = false;
        }
        else
        {
            query = are_connected({ a }, { path2.back() });
            if (query)
            {
                path2.push_back(a);
                connectBetween = false;
            }
            else
            {
                if (path2.size() == 1)
                {
                    connectBetween = false;
                }
                else
                {
                    connectBetween = true;
                }
                for (int j = path2.size() - 1; j >= 0; j--)
                {
                    path1.push_back(path2[j]);
                }
                path2.clear();
                path2.push_back(a);

            }
        }
    }
    vector<int> shortPath, longPath;
    if (path1.size() > path2.size())
    {
        longPath = path1;
        shortPath = path2;
    }
    else
    {
        longPath = path2;
        shortPath = path1;
    }
    bool endsConnected;
    if (shortPath.size() == 1)
    {
        endsConnected = are_connected({ longPath[0], longPath.back() }, { shortPath[0] });
    }
    else
    {
        endsConnected = are_connected({ longPath[0], longPath.back() }, { shortPath[0], shortPath.back() });
    }
    if (endsConnected)
    {
        if (shortPath.size() == 1)
        {
            if (are_connected({ longPath[0] }, { shortPath[0] }))
            {
                ans.push_back(shortPath[0]);
                for (int k = 0; k < longPath.size(); k++)
                {
                    ans.push_back(longPath[k]);
                }
                return;
            }
            else
            {
                ans = longPath;
                ans.push_back(shortPath[0]);
                return;
            }
        }
        else
        {
            if (are_connected({ longPath[0], longPath.back() }, { shortPath[0] }))
            {
                if (are_connected({ longPath[0] }, { shortPath[0] }))
                {
                    for (int k = longPath.size() - 1; k >= 0; k--)
                    {
                        ans.push_back(longPath[k]);
                    }
                    for (int k = 0; k < shortPath.size(); k++)
                    {
                        ans.push_back(shortPath[k]);
                    }
                    return;
                }
                else
                {
                    ans = longPath;
                    for(int k = 0; k<shortPath.size();k++){ans.push_back(shortPath[k]);}
                    return;
                }
            }
            else if (are_connected({ longPath.back() }, { shortPath.back() }))
            {
                ans = longPath;
                for (int k = shortPath.size()-1; k >= 0; k--)
                {
                    ans.push_back(shortPath[k]);
                }
                return;
            }
            else
            {
                ans = shortPath;
                for (int k = 0; k < longPath.size(); k++)
                {
                    ans.push_back(longPath[k]);
                }
                return;
            }
        }
    }
    else
    {
        bool areConnected = are_connected(longPath, shortPath);
        if (areConnected)
        {
            int l = 0, r = longPath.size() - 1;
            while (l != r)
            {
                int m = (l + r) >> 1;
                vector<int> test_data(m - l + 1);
                for (int k = l; k <= m; k++) {
                    test_data[k - l] = longPath[k];
                }
                if (are_connected(shortPath, test_data)) {
                    r = m;
                }
                else {
                    l = m + 1;
                }
            }
            int pos = l;
            l = 0, r = shortPath.size() - 1;
            while (l != r)
            {
                int m = (l + r) >> 1;
                vector<int> test_data(m - l + 1);
                for (int k = l; k <= m; k++) {
                    test_data[k - l] = shortPath[k];
                }
                if (are_connected({ longPath[pos] }, test_data)) {
                    r = m;
                }
                else {
                    l = m + 1;
                }
            }
            int pos2 = l;
            for (int k = (pos2 + 1) % shortPath.size(); k != pos2; k = (k + 1) % shortPath.size()) {
                ans.push_back(shortPath[k]);
            }
            ans.push_back(shortPath[pos2]);
            ans.push_back(longPath[pos]);
            for (int k = (pos + 1) % longPath.size(); k != pos; k = (k + 1) % longPath.size()) {
                ans.push_back(longPath[k]);
            }
            return;

        }
        else
        {
            ans = longPath;
            return;
        }
    }


}

vector<int> longest_trip(int n, int d) {
    vector<int> ans;
    if (d == 3) {
        sub1(ans, n);
        return ans;
    }
    if (d == 2) {
        sub2(ans, n);
        return ans;
    }
    if (d == 1)
    {
        sub3(ans, n);
        return ans;
    }
    return { 0, 4, 5 ,2 };
}

//int main() { return 0; }

Compilation message

longesttrip.cpp: In function 'void sub2(std::vector<int>&, int)':
longesttrip.cpp:26:53: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   26 |             if (!(i == n - 3 || !are_last_connected && i == n - 4))ans.push_back(i + 3);
      |                                 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
longesttrip.cpp: In function 'void sub3(std::vector<int>&, int)':
longesttrip.cpp:111:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |                 for (int k = 0; k < longPath.size(); k++)
      |                                 ~~^~~~~~~~~~~~~~~~~
longesttrip.cpp:134:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  134 |                     for (int k = 0; k < shortPath.size(); k++)
      |                                     ~~^~~~~~~~~~~~~~~~~~
longesttrip.cpp:143:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  143 |                     for(int k = 0; k<shortPath.size();k++){ans.push_back(shortPath[k]);}
      |                                    ~^~~~~~~~~~~~~~~~~
longesttrip.cpp:159:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  159 |                 for (int k = 0; k < longPath.size(); k++)
      |                                 ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 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 4 ms 344 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 5 ms 344 KB Output is correct
7 Correct 4 ms 344 KB Output is correct
8 Correct 6 ms 344 KB Output is correct
9 Correct 4 ms 344 KB Output is correct
10 Correct 5 ms 344 KB Output is correct
11 Correct 5 ms 344 KB Output is correct
12 Correct 3 ms 344 KB Output is correct
13 Correct 6 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 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 6 ms 344 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 4 ms 344 KB Output is correct
9 Correct 4 ms 600 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 5 ms 344 KB Output is correct
15 Correct 6 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 6 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 6 ms 444 KB Output is correct
24 Correct 6 ms 344 KB Output is correct
25 Correct 8 ms 344 KB Output is correct
26 Correct 7 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 344 KB Output is correct
30 Correct 4 ms 344 KB Output is correct
31 Correct 5 ms 344 KB Output is correct
32 Correct 6 ms 344 KB Output is correct
33 Correct 4 ms 344 KB Output is correct
34 Correct 5 ms 344 KB Output is correct
35 Correct 4 ms 344 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 5 ms 344 KB Output is correct
40 Correct 7 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 6 ms 344 KB Output is correct
44 Correct 5 ms 344 KB Output is correct
45 Correct 7 ms 344 KB Output is correct
46 Correct 7 ms 400 KB Output is correct
47 Correct 6 ms 344 KB Output is correct
48 Correct 7 ms 344 KB Output is correct
49 Correct 7 ms 344 KB Output is correct
50 Correct 5 ms 344 KB Output is correct
51 Correct 6 ms 344 KB Output is correct
52 Correct 8 ms 344 KB Output is correct
53 Correct 6 ms 436 KB Output is correct
54 Correct 7 ms 344 KB Output is correct
55 Correct 5 ms 344 KB Output is correct
56 Correct 4 ms 344 KB Output is correct
57 Correct 5 ms 344 KB Output is correct
58 Correct 5 ms 344 KB Output is correct
59 Correct 6 ms 424 KB Output is correct
60 Correct 6 ms 340 KB Output is correct
61 Correct 4 ms 344 KB Output is correct
62 Correct 5 ms 344 KB Output is correct
63 Correct 9 ms 600 KB Output is correct
64 Correct 5 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 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 5 ms 344 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
6 Correct 5 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 436 KB Output is correct
10 Correct 4 ms 344 KB Output is correct
11 Correct 5 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 7 ms 344 KB Output is correct
15 Correct 6 ms 344 KB Output is correct
16 Correct 6 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 6 ms 344 KB Output is correct
20 Correct 8 ms 432 KB Output is correct
21 Correct 8 ms 344 KB Output is correct
22 Correct 6 ms 344 KB Output is correct
23 Correct 5 ms 344 KB Output is correct
24 Correct 6 ms 344 KB Output is correct
25 Correct 7 ms 344 KB Output is correct
26 Correct 5 ms 344 KB Output is correct
27 Correct 5 ms 344 KB Output is correct
28 Correct 4 ms 344 KB Output is correct
29 Correct 4 ms 404 KB Output is correct
30 Correct 4 ms 344 KB Output is correct
31 Correct 4 ms 344 KB Output is correct
32 Correct 6 ms 344 KB Output is correct
33 Correct 7 ms 344 KB Output is correct
34 Correct 6 ms 344 KB Output is correct
35 Correct 6 ms 344 KB Output is correct
36 Correct 6 ms 344 KB Output is correct
37 Correct 4 ms 344 KB Output is correct
38 Correct 6 ms 344 KB Output is correct
39 Correct 6 ms 344 KB Output is correct
40 Correct 4 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 4 ms 344 KB Output is correct
44 Correct 4 ms 416 KB Output is correct
45 Correct 4 ms 344 KB Output is correct
46 Correct 6 ms 596 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 7 ms 344 KB Output is correct
52 Correct 5 ms 428 KB Output is correct
53 Correct 6 ms 344 KB Output is correct
54 Correct 5 ms 344 KB Output is correct
55 Correct 7 ms 344 KB Output is correct
56 Correct 4 ms 344 KB Output is correct
57 Correct 4 ms 596 KB Output is correct
58 Correct 5 ms 344 KB Output is correct
59 Correct 5 ms 344 KB Output is correct
60 Correct 5 ms 340 KB Output is correct
61 Correct 5 ms 344 KB Output is correct
62 Correct 4 ms 344 KB Output is correct
63 Correct 5 ms 344 KB Output is correct
64 Correct 5 ms 344 KB Output is correct
65 Correct 5 ms 344 KB Output is correct
66 Correct 8 ms 436 KB Output is correct
67 Correct 6 ms 344 KB Output is correct
68 Correct 8 ms 412 KB Output is correct
69 Correct 6 ms 344 KB Output is correct
70 Correct 6 ms 344 KB Output is correct
71 Correct 3 ms 344 KB Output is correct
72 Correct 4 ms 344 KB Output is correct
73 Correct 6 ms 344 KB Output is correct
74 Correct 7 ms 344 KB Output is correct
75 Correct 6 ms 344 KB Output is correct
76 Correct 6 ms 344 KB Output is correct
77 Correct 5 ms 424 KB Output is correct
78 Correct 5 ms 344 KB Output is correct
79 Correct 5 ms 344 KB Output is correct
80 Correct 5 ms 416 KB Output is correct
81 Correct 5 ms 344 KB Output is correct
82 Correct 5 ms 344 KB Output is correct