답안 #847601

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
847601 2023-09-10T04:05:30 Z dlalswp25 가장 긴 여행 (IOI23_longesttrip) C++17
5 / 100
7 ms 596 KB
#include "longesttrip.h"
#include <bits/stdc++.h>

using namespace std;

bool ask(vector<int> a, vector<int> b) {
    if (!a.size() || !b.size()) return false;
    return are_connected(a, b);
}

bool adj(int a, int b) {
    return ask({a}, {b});
}

std::vector<int> longest_trip(int N, int D)
{
    vector<int> A = {0}, B;
    for (int i = 1; i < N; i++) {
        if (!B.size()) {
            (adj(A.back(), i) ? A : B).push_back(i);
        } else if (!adj(A.back(), i)) {
            B.push_back(i);
        } else {
            int j = i++;
            if (adj(B.back(), i)) {
                A.push_back(j);
                B.push_back(i);
                if (adj(j, i)) {
                    while (B.size()) {
                        A.push_back(B.back());
                        B.pop_back();
                    }
                }
            } else {
                if (adj(j, i)) {
                    A.push_back(j);
                    A.push_back(i);
                } else {
                    A.push_back(i);
                    B.push_back(j);
                }
            }
        }
    }
    if (!B.size()) return A;
    if (!ask(A, B)) return (A.size() > B.size() ? A : B);
    if (adj(A.back(), B[0])) {
        A.insert(A.end(), B.begin(), B.end());
        return A;
    }
    if (adj(A[0], B[0])) {
        reverse(A.begin(), A.end());
        A.insert(A.end(), B.begin(), B.end());
        return A;
    }
    vector<int> X = A;
    while (X.size() > 1) {
        vector<int> L, R;
        for (int i = 0; i < X.size(); i++) {
            if (i < X.size() / 2) L.push_back(X[i]);
            else R.push_back(X[i]);
        }
        if (ask(L, B)) X = L;
        else X = R;
    }
    int a = X[0];
    X = B;
    while (X.size() > 1) {
        vector<int> L, R;
        for (int i = 0; i < X.size(); i++) {
            if (i < X.size() / 2) L.push_back(X[i]);
            else R.push_back(X[i]);
        }
        if (ask({a}, L)) X = L;
        else X = R;
    }
    int b = X[0];
    vector<int> ans;
    int i;
    for (i = 0; i < A.size(); i++) {
        if (A[i] == a) break;
    }
    for (int j = (i + 1) % A.size(); j != i; j = (j + 1) % A.size()) {
        ans.push_back(A[j]);
    }
    ans.push_back(a);
    ans.push_back(b);
    for (i = 0; i < B.size(); i++) {
        if (B[i] == b) break;
    }
    for (int j = (i + 1) % B.size(); j != i; j = (j + 1) % B.size()) {
        ans.push_back(B[j]);
    }
    return ans;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:59:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |         for (int i = 0; i < X.size(); i++) {
      |                         ~~^~~~~~~~~~
longesttrip.cpp:60:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |             if (i < X.size() / 2) L.push_back(X[i]);
      |                 ~~^~~~~~~~~~~~~~
longesttrip.cpp:70:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |         for (int i = 0; i < X.size(); i++) {
      |                         ~~^~~~~~~~~~
longesttrip.cpp:71:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |             if (i < X.size() / 2) L.push_back(X[i]);
      |                 ~~^~~~~~~~~~~~~~
longesttrip.cpp:80:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |     for (i = 0; i < A.size(); i++) {
      |                 ~~^~~~~~~~~~
longesttrip.cpp:88:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |     for (i = 0; i < B.size(); i++) {
      |                 ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 4 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 5 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 5 ms 344 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
6 Incorrect 1 ms 344 KB invalid array
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 4 ms 344 KB Output is correct
6 Incorrect 1 ms 344 KB invalid array
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 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 596 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
6 Incorrect 0 ms 344 KB invalid array
7 Halted 0 ms 0 KB -