답안 #839728

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
839728 2023-08-30T15:49:08 Z model_code 가장 긴 여행 (IOI23_longesttrip) C++17
100 / 100
25 ms 336 KB
// correct/sol_gyp_D1.cpp

#include "longesttrip.h"
#include <bits/stdc++.h>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>

using namespace std;

bool sajat = 0;
/*
const int maxNumberOfCalls = 32640;
const int maxTotalNumberOfCalls = 150000;
const int maxTotalNumberOfLandmarksInCalls = 1500000;
int call_counter = 0;
int total_call_counter = 0;
int landmark_counter = 0;

int C, N, D;
std::vector<std::vector<int>> U;

static inline void protocol_violation(std::string message) {
    printf("Protocol Violation: %s\n", message.c_str());
    exit(0);
}

bool are_connected(std::vector<int> A, std::vector<int> B) {
    if (sajat) {
        cout << "nagy kerdes:\n";
        for (auto x:A) {
            cout << x << " ";
        }
        cout << "\n";
        for (auto y:B) {
            cout << y << " ";
        }
        cout << "\n";
        int x;
        cin >> x;
        return x;
    }
    ++call_counter; ++ total_call_counter;
    if (call_counter > maxNumberOfCalls || total_call_counter > maxTotalNumberOfCalls) {
        protocol_violation("too many calls");
    }

    int nA = A.size(), nB = B.size();
    landmark_counter += nA + nB;
    if (landmark_counter > maxTotalNumberOfLandmarksInCalls) {
        protocol_violation("too many elements");
    }

    if (nA == 0 || nB == 0) {
        protocol_violation("invalid array");
    }
    for (int i = 0; i < nA; ++i) {
        if (A[i] < 0 || N <= A[i]) {
            protocol_violation("invalid array");
        }
        for (int j = 0; j < i; ++j) {
            if (A[i] == A[j]) {
                protocol_violation("invalid array");
            }
        }
    }
    for (int i = 0; i < nB; ++i) {
        if (B[i] < 0 || N <= B[i]) {
            protocol_violation("invalid array");
        }
        for (int j = 0; j < i; ++j) {
            if (B[i] == B[j]) {
                protocol_violation("invalid array");
            }
        }
    }

    for (int i = 0; i < nA; ++i) {
        for (int j = 0; j < nB; ++j) {
            if (A[i] == B[j]){
                protocol_violation("non-disjoint arrays");
            }
        }
    }

    for (int i = 0; i < nA; ++i) {
        for (int j = 0; j < nB; ++j) {
            if (U[std::max(A[i],B[j])][std::min(A[i],B[j])] == 1) {
                return true;
            }
        }
    }

    return false;
}

*/
bool el(int x, int y)
{
    if (sajat)
    {
        cout << "kerdes: " << x << " " << y << "\n";
        int res;
        cin >> res;
        return res;
    }
    return are_connected({x}, {y});
}

std::vector<int> longest_trip(int N, int D)
{
    int n = N;
    vector<int> sor;
    for (int i = 1; i < N; i++)
    {
        sor.push_back(i);
    }
    random_shuffle(sor.begin(), sor.end());
    vector<int> a, b;
    a.push_back(0);
    bool spec = 0;
    for (int j = 1; j < N; j++)
    {
        int i = sor[j - 1];
        if (el(i, a.back()))
        {
            a.push_back(i);
            spec = 0;
        }
        else
        {
            if (b.size() == 0 || spec || el(i, b.back()))
            {
                b.push_back(i);
                spec = 1;
            }
            else
            {
                if (b.size() == 1)
                {
                    spec = 1;
                }
                else
                {
                    spec = 0;
                }
                reverse(b.begin(), b.end());
                for (auto x : b)
                {
                    a.push_back(x);
                }
                b.clear();
                b.push_back(i);
            }
        }
        if (sajat)
        {
            cout << "vege:\n";
            for (auto x : a)
            {
                cout << x << " ";
            }
            cout << "\n";
            for (auto x : b)
            {
                cout << x << " ";
            }
            cout << "\n";
            cout << "spec: " << spec << "\n";
        }
    }
    int sa = a.size(), sb = b.size();
    /*for (int i=1; i<sa; i++) {
        assert(el(a[i-1], a[i]));
    }
    for (int i=1; i<sb; i++) {
        assert(el(b[i-1], b[i]));
    }*/
    if (b.size() > a.size())
    {
        swap(a, b);
    }

    if (a.size() > 0 && b.size() > 0)
    {
        if (el(a.back(), b.back()))
        {
            reverse(b.begin(), b.end());
            for (auto x : b)
            {
                a.push_back(x);
            }
            return a;
        }
        else if (el(a[0], b.back()))
        {
            vector<int> c;
            for (auto x : b)
            {
                c.push_back(x);
            }
            for (auto x : a)
            {
                c.push_back(x);
            }
            /*assert(c.size()==n);
            for (int i=1; i<n; i++) {
                assert(el(c[i-1], c[i]));
            }*/
            return c;
        }
        else if (el(a[0], b[0]))
        {
            vector<int> c;
            reverse(b.begin(), b.end());
            for (auto x : b)
            {
                c.push_back(x);
            }
            for (auto x : a)
            {
                c.push_back(x);
            }
            return c;
        }
        else if (el(a.back(), b[0]))
        {
            vector<int> c;
            for (auto x : a)
            {
                c.push_back(x);
            }
            for (auto x : b)
            {
                c.push_back(x);
            }
            return c;
        }
        else
        {
            if (!are_connected(a, b))
            {
                return a;
            }
            int lo = 0, hi = a.size(), mid;
            while (hi - lo > 1)
            {
                mid = (hi + lo) / 2;
                vector<int> aa;
                for (int i = 0; i < mid; i++)
                {
                    aa.push_back(a[i]);
                }
                if (are_connected(aa, b))
                {
                    hi = mid;
                }
                else
                {
                    lo = mid;
                }
            }
            int pa = lo;
            vector<int> ha = {a[lo]};
            lo = 0, hi = b.size(), mid = 0;
            while (hi - lo > 1)
            {
                mid = (hi + lo) / 2;
                vector<int> bb;
                for (int i = 0; i < mid; i++)
                {
                    bb.push_back(b[i]);
                }
                if (are_connected(ha, bb))
                {
                    hi = mid;
                }
                else
                {
                    lo = mid;
                }
            }
            int pb = lo;

            int sa = a.size(), sb = b.size();
            vector<int> c;
            for (int i = pa + 1; i <= pa + sa; i++)
            {
                c.push_back(a[i % sa]);
            }
            for (int i = pb; i < pb + sb; i++)
            {
                c.push_back(b[i % sb]);
            }
            return c;
        }
    }
    else
    {
        return a;
    }
}

/*
int main() {
    if (sajat) {
        int n;
        cin >> n;
        vector<int> res=longest_trip(n, 1);
        for (auto x:res) {
            cout << x << " ";
        }
        return 0;
    }
    assert(1 == scanf("%d", &C));
    int maximumCalls = 0;
    for (int c = 0; c < C; ++c) {
        call_counter = 0;
        assert(2 == scanf("%d %d", &N, &D));

        U.resize(N);
        for (int i = 1; i < N; ++i) {
            U[i].resize(i);
            for (int j = 0; j < i; ++j) {
                assert(1 == scanf("%d", &U[i][j]));
            }
        }

        for (int i = 2; i < N; ++i) {
            for (int j = 1; j < i; ++j) {
                for (int k = 0; k < j; ++k) {
                    if (U[i][j] + U[i][k] + U[j][k] < D) {
                        printf("Insufficient Density\n");
                        exit(0);
                    }
                }
            }
        }

        std::vector<int> T = longest_trip(N, D);
        int l = T.size();
        cout << "valasz:           ";
        for (int i = 0; i < l; ++i) {
            printf(i == 0 ? "%d" : " %d", T[i]);
        }
        printf("\n");
        printf("%d\n", call_counter);

        maximumCalls = std::max(maximumCalls, call_counter);
        call_counter = 0;
    }
    printf("%d\n", maximumCalls);

    return 0;
}
*/
/*
2
5 1
1
1 1
0 0 1
0 0 0 1
4 1
1
0 0
0 0 1

*/

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:113:9: warning: unused variable 'n' [-Wunused-variable]
  113 |     int n = N;
      |         ^
longesttrip.cpp:173:9: warning: unused variable 'sa' [-Wunused-variable]
  173 |     int sa = a.size(), sb = b.size();
      |         ^~
longesttrip.cpp:173:24: warning: unused variable 'sb' [-Wunused-variable]
  173 |     int sa = a.size(), sb = b.size();
      |                        ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 2 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 208 KB Output is correct
2 Correct 9 ms 260 KB Output is correct
3 Correct 9 ms 208 KB Output is correct
4 Correct 9 ms 208 KB Output is correct
5 Correct 8 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 208 KB Output is correct
2 Correct 9 ms 208 KB Output is correct
3 Correct 10 ms 208 KB Output is correct
4 Correct 6 ms 228 KB Output is correct
5 Correct 7 ms 252 KB Output is correct
6 Correct 11 ms 208 KB Output is correct
7 Correct 6 ms 208 KB Output is correct
8 Correct 9 ms 252 KB Output is correct
9 Correct 5 ms 208 KB Output is correct
10 Correct 7 ms 208 KB Output is correct
11 Correct 7 ms 208 KB Output is correct
12 Correct 8 ms 208 KB Output is correct
13 Correct 8 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 208 KB Output is correct
2 Correct 9 ms 208 KB Output is correct
3 Correct 6 ms 208 KB Output is correct
4 Correct 8 ms 208 KB Output is correct
5 Correct 9 ms 208 KB Output is correct
6 Correct 11 ms 208 KB Output is correct
7 Correct 11 ms 208 KB Output is correct
8 Correct 8 ms 208 KB Output is correct
9 Correct 5 ms 208 KB Output is correct
10 Correct 9 ms 256 KB Output is correct
11 Correct 10 ms 208 KB Output is correct
12 Correct 13 ms 208 KB Output is correct
13 Correct 6 ms 208 KB Output is correct
14 Correct 24 ms 256 KB Output is correct
15 Correct 12 ms 208 KB Output is correct
16 Correct 12 ms 208 KB Output is correct
17 Correct 11 ms 208 KB Output is correct
18 Correct 11 ms 208 KB Output is correct
19 Correct 9 ms 208 KB Output is correct
20 Correct 9 ms 300 KB Output is correct
21 Correct 9 ms 208 KB Output is correct
22 Correct 10 ms 208 KB Output is correct
23 Correct 8 ms 208 KB Output is correct
24 Correct 11 ms 208 KB Output is correct
25 Correct 20 ms 208 KB Output is correct
26 Correct 21 ms 208 KB Output is correct
27 Correct 14 ms 260 KB Output is correct
28 Correct 9 ms 208 KB Output is correct
29 Correct 17 ms 208 KB Output is correct
30 Correct 9 ms 208 KB Output is correct
31 Correct 8 ms 208 KB Output is correct
32 Correct 9 ms 208 KB Output is correct
33 Correct 10 ms 336 KB Output is correct
34 Correct 9 ms 208 KB Output is correct
35 Correct 8 ms 208 KB Output is correct
36 Correct 8 ms 336 KB Output is correct
37 Correct 8 ms 208 KB Output is correct
38 Correct 8 ms 208 KB Output is correct
39 Correct 8 ms 208 KB Output is correct
40 Correct 9 ms 208 KB Output is correct
41 Correct 6 ms 208 KB Output is correct
42 Correct 11 ms 208 KB Output is correct
43 Correct 8 ms 208 KB Output is correct
44 Correct 12 ms 208 KB Output is correct
45 Correct 10 ms 208 KB Output is correct
46 Correct 9 ms 208 KB Output is correct
47 Correct 10 ms 208 KB Output is correct
48 Correct 11 ms 208 KB Output is correct
49 Correct 10 ms 208 KB Output is correct
50 Correct 7 ms 300 KB Output is correct
51 Correct 16 ms 300 KB Output is correct
52 Correct 11 ms 228 KB Output is correct
53 Correct 10 ms 256 KB Output is correct
54 Correct 10 ms 304 KB Output is correct
55 Correct 11 ms 208 KB Output is correct
56 Correct 5 ms 308 KB Output is correct
57 Correct 8 ms 208 KB Output is correct
58 Correct 11 ms 304 KB Output is correct
59 Correct 10 ms 304 KB Output is correct
60 Correct 9 ms 312 KB Output is correct
61 Correct 12 ms 328 KB Output is correct
62 Correct 10 ms 208 KB Output is correct
63 Correct 9 ms 308 KB Output is correct
64 Correct 12 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 208 KB Output is correct
2 Correct 9 ms 208 KB Output is correct
3 Correct 10 ms 208 KB Output is correct
4 Correct 9 ms 208 KB Output is correct
5 Correct 10 ms 208 KB Output is correct
6 Correct 10 ms 224 KB Output is correct
7 Correct 11 ms 236 KB Output is correct
8 Correct 7 ms 208 KB Output is correct
9 Correct 4 ms 208 KB Output is correct
10 Correct 11 ms 208 KB Output is correct
11 Correct 6 ms 208 KB Output is correct
12 Correct 10 ms 256 KB Output is correct
13 Correct 9 ms 208 KB Output is correct
14 Correct 23 ms 260 KB Output is correct
15 Correct 16 ms 208 KB Output is correct
16 Correct 12 ms 208 KB Output is correct
17 Correct 10 ms 208 KB Output is correct
18 Correct 10 ms 208 KB Output is correct
19 Correct 11 ms 208 KB Output is correct
20 Correct 7 ms 304 KB Output is correct
21 Correct 18 ms 208 KB Output is correct
22 Correct 25 ms 208 KB Output is correct
23 Correct 12 ms 208 KB Output is correct
24 Correct 12 ms 208 KB Output is correct
25 Correct 15 ms 208 KB Output is correct
26 Correct 10 ms 332 KB Output is correct
27 Correct 11 ms 208 KB Output is correct
28 Correct 9 ms 208 KB Output is correct
29 Correct 9 ms 208 KB Output is correct
30 Correct 7 ms 208 KB Output is correct
31 Correct 6 ms 208 KB Output is correct
32 Correct 14 ms 208 KB Output is correct
33 Correct 10 ms 208 KB Output is correct
34 Correct 13 ms 212 KB Output is correct
35 Correct 11 ms 208 KB Output is correct
36 Correct 14 ms 208 KB Output is correct
37 Correct 9 ms 208 KB Output is correct
38 Correct 9 ms 212 KB Output is correct
39 Correct 18 ms 208 KB Output is correct
40 Correct 12 ms 296 KB Output is correct
41 Correct 9 ms 332 KB Output is correct
42 Correct 9 ms 208 KB Output is correct
43 Correct 11 ms 208 KB Output is correct
44 Correct 10 ms 208 KB Output is correct
45 Correct 7 ms 208 KB Output is correct
46 Correct 10 ms 208 KB Output is correct
47 Correct 7 ms 208 KB Output is correct
48 Correct 8 ms 208 KB Output is correct
49 Correct 10 ms 208 KB Output is correct
50 Correct 10 ms 208 KB Output is correct
51 Correct 12 ms 208 KB Output is correct
52 Correct 9 ms 208 KB Output is correct
53 Correct 12 ms 208 KB Output is correct
54 Correct 7 ms 208 KB Output is correct
55 Correct 12 ms 208 KB Output is correct
56 Correct 9 ms 208 KB Output is correct
57 Correct 5 ms 288 KB Output is correct
58 Correct 10 ms 208 KB Output is correct
59 Correct 11 ms 208 KB Output is correct
60 Correct 11 ms 208 KB Output is correct
61 Correct 8 ms 208 KB Output is correct
62 Correct 7 ms 208 KB Output is correct
63 Correct 8 ms 308 KB Output is correct
64 Correct 11 ms 300 KB Output is correct
65 Correct 10 ms 304 KB Output is correct
66 Correct 9 ms 208 KB Output is correct
67 Correct 13 ms 208 KB Output is correct
68 Correct 12 ms 292 KB Output is correct
69 Correct 11 ms 308 KB Output is correct
70 Correct 13 ms 208 KB Output is correct
71 Correct 9 ms 292 KB Output is correct
72 Correct 11 ms 208 KB Output is correct
73 Correct 9 ms 288 KB Output is correct
74 Correct 13 ms 208 KB Output is correct
75 Correct 9 ms 304 KB Output is correct
76 Correct 12 ms 208 KB Output is correct
77 Correct 7 ms 312 KB Output is correct
78 Correct 14 ms 300 KB Output is correct
79 Correct 13 ms 308 KB Output is correct
80 Correct 7 ms 284 KB Output is correct
81 Correct 9 ms 208 KB Output is correct
82 Correct 10 ms 208 KB Output is correct