답안 #846744

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
846744 2023-09-08T11:01:59 Z IvanJ 가장 긴 여행 (IOI23_longesttrip) C++17
100 / 100
10 ms 856 KB
#include "longesttrip.h"
#include<bits/stdc++.h>

#define pb push_back
#define all(a) (a).begin(), (a).end()

using namespace std;

vector<int> longest_trip(int n, int D) {
    if(D == 3) {
        vector<int> p;
        for(int i = 0;i < n;i++) p.pb(i);
        return p;
    }
    if(D == 2) {
        deque<int> dq;
        if(!are_connected({0}, {1}))
            dq.pb(0), dq.pb(2), dq.pb(1);
        else if(!are_connected({1}, {2}))
            dq.pb(1), dq.pb(0), dq.pb(2);
        else dq.pb(0), dq.pb(1), dq.pb(2);

        for(int i = 3;i < n;i++) {
            if(are_connected({i}, {dq.front()}))
                dq.push_front(i);
            else dq.pb(i);
        }

        return vector<int>(all(dq));
    }
    if(D == 1) {
        vector<int> p1, p2;
        if(n & 1) p1.pb(n - 1);
        for(int i = 1;i < n;i += 2) {
            int a = i - 1, b = i;
            if(p2.size()) {
                int c = p1.back(), d = p2.back();
                if(are_connected({a}, {b})) {
                    if(are_connected({a}, {c})) {
                        p1.pb(a), p1.pb(b);
                        if(are_connected({b}, {d})) {
                            while(p2.size()) 
                                p1.pb(p2.back()), p2.pop_back();
                        }
                    }  else {
                        p2.pb(a), p2.pb(b);
                        if(are_connected({b}, {c})) {
                            while(p1.size()) 
                                p2.pb(p1.back()), p1.pop_back();
                        }
                    }
                } else {
                    if(are_connected({a}, {c})) {
                        if(are_connected({b}, {d})) 
                            p1.pb(a), p2.pb(b);
                        else p1.pb(b), p2.pb(a);
                    } else p1.pb(b), p2.pb(a);
                }
            } else {
                if(are_connected({a}, {b})) {
                    if(p1.size() == 0)
                        p1.pb(a), p1.pb(b);
                    else if(are_connected({a}, {p1.back()})) {
                        p1.pb(a), p1.pb(b);
                    } else {
                        if(are_connected({b}, {p1.back()})) 
                            p1.pb(b), p1.pb(a);
                        else p2.pb(a), p2.pb(b);
                    }
                } else {
                    if(p1.size() == 0)
                        p1.pb(a), p2.pb(b);
                    else if(are_connected({a}, {p1.back()}))
                        p1.pb(a), p2.pb(b);
                    else p1.pb(b), p2.pb(a);
                }

            }

            if(p1.size() < p2.size()) swap(p1, p2);
        }

        if((int)p1.size() == n) return p1;
        if(!are_connected(p1, p2)) return p1;

        if(are_connected({p1.back()}, {p2[0]})) {
            vector<int> p = p1;
            for(int i : p2) p.pb(i);
            return p;
        }
        if(are_connected({p2.back()}, {p1[0]})) {
            vector<int> p = p2;
            for(int i : p1) p.pb(i);
            return p;
        }

        vector<int> v = p1;
        while(v.size() > 1) {
            int mid = (int)v.size() / 2;
            vector<int> v1, v2;
            for(int i = 0;i < mid;i++) v1.pb(v[i]);
            for(int i = mid;i < (int)v.size();i++) v2.pb(v[i]);
            if(are_connected(v1, p2)) v = v1;
            else v = v2;
        }

        int X = v[0];
        v = p2;
        while(v.size() > 1) {
            int mid = (int)v.size() / 2;
            vector<int> v1, v2;
            for(int i = 0;i < mid;i++) v1.pb(v[i]);
            for(int i = mid;i < (int)v.size();i++) v2.pb(v[i]);
            if(are_connected({X}, v1)) v = v1;
            else v = v2;
        }
        int Y = v[0];

        vector<int> p;
        int pos1 = -1, pos2 = -1;
        for(int i = 0;i < (int)p1.size();i++)
            if(p1[i] == X) pos1 = i;
        for(int i = 0;i < (int)p2.size();i++)
            if(p2[i] == Y) pos2 = i;

        for(int i = 0;i < (int)p1.size();i++)
            p.pb(p1[(i + pos1) % (int)p1.size()]);
        reverse(all(p));
        for(int i = 0;i < (int)p2.size();i++)
            p.pb(p2[(i + pos2) % (int)p2.size()]);
        return p;
    }
    return {};
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 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 5 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 4 ms 344 KB Output is correct
5 Correct 5 ms 432 KB Output is correct
6 Correct 6 ms 412 KB Output is correct
7 Correct 4 ms 344 KB Output is correct
8 Correct 4 ms 344 KB Output is correct
9 Correct 4 ms 344 KB Output is correct
10 Correct 6 ms 600 KB Output is correct
11 Correct 4 ms 344 KB Output is correct
12 Correct 5 ms 852 KB Output is correct
13 Correct 4 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 540 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 6 ms 344 KB Output is correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 7 ms 344 KB Output is correct
8 Correct 5 ms 344 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 4 ms 344 KB Output is correct
11 Correct 5 ms 600 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 5 ms 600 KB Output is correct
14 Correct 9 ms 596 KB Output is correct
15 Correct 8 ms 344 KB Output is correct
16 Correct 8 ms 344 KB Output is correct
17 Correct 6 ms 600 KB Output is correct
18 Correct 6 ms 600 KB Output is correct
19 Correct 5 ms 600 KB Output is correct
20 Correct 5 ms 344 KB Output is correct
21 Correct 6 ms 344 KB Output is correct
22 Correct 5 ms 344 KB Output is correct
23 Correct 5 ms 344 KB Output is correct
24 Correct 5 ms 596 KB Output is correct
25 Correct 7 ms 344 KB Output is correct
26 Correct 6 ms 344 KB Output is correct
27 Correct 9 ms 344 KB Output is correct
28 Correct 6 ms 344 KB Output is correct
29 Correct 7 ms 596 KB Output is correct
30 Correct 6 ms 344 KB Output is correct
31 Correct 6 ms 344 KB Output is correct
32 Correct 6 ms 344 KB Output is correct
33 Correct 6 ms 344 KB Output is correct
34 Correct 6 ms 344 KB Output is correct
35 Correct 8 ms 344 KB Output is correct
36 Correct 7 ms 344 KB Output is correct
37 Correct 6 ms 600 KB Output is correct
38 Correct 6 ms 600 KB Output is correct
39 Correct 7 ms 600 KB Output is correct
40 Correct 6 ms 344 KB Output is correct
41 Correct 8 ms 344 KB Output is correct
42 Correct 7 ms 344 KB Output is correct
43 Correct 8 ms 600 KB Output is correct
44 Correct 6 ms 596 KB Output is correct
45 Correct 7 ms 344 KB Output is correct
46 Correct 7 ms 344 KB Output is correct
47 Correct 8 ms 344 KB Output is correct
48 Correct 7 ms 344 KB Output is correct
49 Correct 8 ms 596 KB Output is correct
50 Correct 6 ms 436 KB Output is correct
51 Correct 7 ms 436 KB Output is correct
52 Correct 8 ms 344 KB Output is correct
53 Correct 6 ms 344 KB Output is correct
54 Correct 7 ms 600 KB Output is correct
55 Correct 7 ms 344 KB Output is correct
56 Correct 5 ms 440 KB Output is correct
57 Correct 7 ms 600 KB Output is correct
58 Correct 7 ms 600 KB Output is correct
59 Correct 7 ms 600 KB Output is correct
60 Correct 7 ms 436 KB Output is correct
61 Correct 8 ms 344 KB Output is correct
62 Correct 7 ms 600 KB Output is correct
63 Correct 6 ms 440 KB Output is correct
64 Correct 7 ms 604 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 4 ms 344 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
6 Correct 9 ms 344 KB Output is correct
7 Correct 5 ms 344 KB Output is correct
8 Correct 4 ms 344 KB Output is correct
9 Correct 4 ms 344 KB Output is correct
10 Correct 7 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 6 ms 344 KB Output is correct
15 Correct 8 ms 344 KB Output is correct
16 Correct 7 ms 344 KB Output is correct
17 Correct 8 ms 344 KB Output is correct
18 Correct 5 ms 344 KB Output is correct
19 Correct 5 ms 600 KB Output is correct
20 Correct 5 ms 600 KB Output is correct
21 Correct 7 ms 344 KB Output is correct
22 Correct 7 ms 344 KB Output is correct
23 Correct 8 ms 344 KB Output is correct
24 Correct 6 ms 344 KB Output is correct
25 Correct 8 ms 344 KB Output is correct
26 Correct 6 ms 344 KB Output is correct
27 Correct 6 ms 344 KB Output is correct
28 Correct 7 ms 344 KB Output is correct
29 Correct 6 ms 344 KB Output is correct
30 Correct 7 ms 344 KB Output is correct
31 Correct 6 ms 344 KB Output is correct
32 Correct 8 ms 344 KB Output is correct
33 Correct 10 ms 344 KB Output is correct
34 Correct 8 ms 344 KB Output is correct
35 Correct 9 ms 344 KB Output is correct
36 Correct 8 ms 596 KB Output is correct
37 Correct 6 ms 436 KB Output is correct
38 Correct 9 ms 436 KB Output is correct
39 Correct 8 ms 344 KB Output is correct
40 Correct 7 ms 600 KB Output is correct
41 Correct 6 ms 344 KB Output is correct
42 Correct 7 ms 344 KB Output is correct
43 Correct 5 ms 344 KB Output is correct
44 Correct 5 ms 340 KB Output is correct
45 Correct 5 ms 344 KB Output is correct
46 Correct 6 ms 344 KB Output is correct
47 Correct 6 ms 344 KB Output is correct
48 Correct 7 ms 600 KB Output is correct
49 Correct 7 ms 344 KB Output is correct
50 Correct 9 ms 600 KB Output is correct
51 Correct 6 ms 344 KB Output is correct
52 Correct 6 ms 344 KB Output is correct
53 Correct 7 ms 344 KB Output is correct
54 Correct 7 ms 600 KB Output is correct
55 Correct 6 ms 344 KB Output is correct
56 Correct 7 ms 344 KB Output is correct
57 Correct 7 ms 344 KB Output is correct
58 Correct 7 ms 344 KB Output is correct
59 Correct 6 ms 344 KB Output is correct
60 Correct 9 ms 344 KB Output is correct
61 Correct 7 ms 760 KB Output is correct
62 Correct 5 ms 436 KB Output is correct
63 Correct 7 ms 444 KB Output is correct
64 Correct 8 ms 856 KB Output is correct
65 Correct 6 ms 600 KB Output is correct
66 Correct 7 ms 600 KB Output is correct
67 Correct 6 ms 344 KB Output is correct
68 Correct 6 ms 600 KB Output is correct
69 Correct 7 ms 600 KB Output is correct
70 Correct 7 ms 600 KB Output is correct
71 Correct 8 ms 856 KB Output is correct
72 Correct 6 ms 852 KB Output is correct
73 Correct 6 ms 440 KB Output is correct
74 Correct 6 ms 604 KB Output is correct
75 Correct 7 ms 344 KB Output is correct
76 Correct 7 ms 344 KB Output is correct
77 Correct 8 ms 600 KB Output is correct
78 Correct 6 ms 440 KB Output is correct
79 Correct 7 ms 440 KB Output is correct
80 Correct 8 ms 856 KB Output is correct
81 Correct 6 ms 600 KB Output is correct
82 Correct 6 ms 344 KB Output is correct