답안 #1048792

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1048792 2024-08-08T09:33:24 Z ZanP 가장 긴 여행 (IOI23_longesttrip) C++17
15 / 100
10 ms 436 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 connectTo1 = true, connectTo2 = true, 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
            {
                longPath.push_back(shortPath[0]);
                ans = longPath;
                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
                {
                    for (int k = longPath.size() - 1; k >= 0; k--)
                    {
                        ans.push_back(longPath[k]);
                    }
                    for (int k = shortPath.size() - 1; k >= 0; k--)
                    {
                        ans.push_back(shortPath[k]);
                    }
                    return;
                }
            }
            else if (are_connected({ longPath.back(), longPath.back() }, { shortPath[0] }))
            {
                for (int k = 0; k < longPath.size(); k++)
                {
                    ans.push_back(longPath[k]);
                }
                for (int k = 0; k < shortPath.size(); k++)
                {
                    ans.push_back(shortPath[k]);
                }
                return;
            }
            else
            {
                for (int k = 0; k < longPath.size(); k++)
                {
                    ans.push_back(longPath[k]);
                }
                for (int k = shortPath.size() - 1; k >= 0; k--)
                {
                    ans.push_back(shortPath[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);
        if(ans.size() > n){ while(true){} }
        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:155:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  155 |                 for (int k = 0; k < longPath.size(); 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 < shortPath.size(); k++)
      |                                 ~~^~~~~~~~~~~~~~~~~~
longesttrip.cpp:167:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  167 |                 for (int k = 0; k < longPath.size(); k++)
      |                                 ~~^~~~~~~~~~~~~~~~~
longesttrip.cpp:41:10: warning: unused variable 'connectTo1' [-Wunused-variable]
   41 |     bool connectTo1 = true, connectTo2 = true, connectBetween = true;
      |          ^~~~~~~~~~
longesttrip.cpp:41:29: warning: unused variable 'connectTo2' [-Wunused-variable]
   41 |     bool connectTo1 = true, connectTo2 = true, connectBetween = true;
      |                             ^~~~~~~~~~
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:250:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  250 |         if(ans.size() > n){ while(true){} }
      |            ~~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 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 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 344 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 7 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 7 ms 344 KB Output is correct
7 Correct 5 ms 344 KB Output is correct
8 Correct 6 ms 344 KB Output is correct
9 Correct 7 ms 344 KB Output is correct
10 Correct 5 ms 344 KB Output is correct
11 Correct 6 ms 344 KB Output is correct
12 Correct 5 ms 344 KB Output is correct
13 Correct 7 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 7 ms 344 KB Output is correct
3 Correct 4 ms 344 KB Output is correct
4 Correct 7 ms 344 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 5 ms 344 KB Output is correct
9 Correct 6 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 6 ms 344 KB Output is correct
13 Correct 4 ms 344 KB Output is correct
14 Correct 6 ms 344 KB Output is correct
15 Correct 10 ms 344 KB Output is correct
16 Incorrect 1 ms 344 KB Incorrect
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 9 ms 344 KB Output is correct
3 Correct 9 ms 344 KB Output is correct
4 Correct 8 ms 344 KB Output is correct
5 Correct 7 ms 344 KB Output is correct
6 Correct 6 ms 344 KB Output is correct
7 Correct 8 ms 344 KB Output is correct
8 Correct 4 ms 344 KB Output is correct
9 Correct 6 ms 436 KB Output is correct
10 Correct 5 ms 344 KB Output is correct
11 Correct 4 ms 344 KB Output is correct
12 Correct 6 ms 344 KB Output is correct
13 Correct 4 ms 344 KB Output is correct
14 Correct 8 ms 344 KB Output is correct
15 Correct 6 ms 344 KB Output is correct
16 Incorrect 1 ms 344 KB Incorrect
17 Halted 0 ms 0 KB -