Submission #1050365

# Submission time Handle Problem Language Result Execution time Memory
1050365 2024-08-09T08:57:00 Z phoenix Longest Trip (IOI23_longesttrip) C++17
60 / 100
775 ms 1320 KB
#include "longesttrip.h"
#include <bits/stdc++.h>

using namespace std;

#ifndef LOCAL
#define cerr if (false) cout
#endif

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

vector<int> operator + (vector<int> a, vector<int> b) {
    for (int c : b) a.push_back(c);
    return a;
} 

void print(vector<int> a) {
    cerr << '{';
    for (int c : a) 
        cerr << c << ' ';
    cerr << '}';
    cerr << '\n';
}

vector<int> fin(vector<int> V, int x) {
    for (int i = 0; i < (int)V.size() - 1; i++) {
        if (V[i] == x) swap(V[i], V[i + 1]); 
    }
    assert(V.back() == x);
    return V;
}

vector<int> beg(vector<int> V, int x) {
    for (int i = (int)V.size() - 1; i > 0; i--) {
        if (V[i] == x) swap(V[i], V[i - 1]); 
    }
    assert(V[0] == x);
    return V;
}

struct info {
    vector<int> A;
    vector<int> B;
    vector<int> ord;
};

int N;

bool g[256][256];

bool are_con(vector<int> a, vector<int> b) {
    cerr << "are connected: \n";
    print(a);
    print(b);
    cerr << '\n';
    return are_connected(a, b);
} 


vector<int> connect(vector<int> A, vector<int> B) {
    for (int i = 0; i < (int)B.size() - 1; i++) {
        if (are_con({A.back()}, {B[i]})) {
            return A + beg(B, B[i]);
        }
    }
    return A + beg(B, B.back());
};

info recursion(vector<int> V) {
    if ((int)V.size() == 1) 
        return {V, {}, V};
    vector<int> A, B;
    vector<bool> us(N);
    int p = V[rnd() % (int)V.size()];
    cerr << "chosen vertex: " << p << '\n';
    for (int c : V) if (p != c) {
        if (!are_con({p}, {c})) {
            B.push_back(c);
            us[c] = true;
        }
    }
    if (B.empty()) {
        info res;
        cerr << p << " is a star\n";
        V.erase(find(V.begin(), V.end(), p));
        info m = recursion(V);
        if (m.ord.empty()) {
            res.ord = m.A + vector<int>{p} + m.B;
        } else {
            res.ord = m.ord;
            res.ord.push_back(p);
        }
        return res;
    }
    A.push_back(p);
    us[p] = true;
    for (int c : V) {
        if (us[c]) continue;
        if (!are_con({c}, B)) {
            A.push_back(c);
            us[c] = true;
        }
    }
    cerr << "Division: \n";
    print(A);
    print(B);
    cerr << '\n';

    vector<int> v1;
    for (int c : V) 
        if (!us[c]) 
            v1.push_back(c);
    if (v1.empty()) 
        return {A, B, {}};

    info M = recursion(v1);
    info res;
    
    if (!M.ord.empty()) {
        res.ord = fin(A, p) + connect(M.ord, B);
        return res;
    }
    for (int it = 0; it < 2; it++) {
        if ((int)A.size() == 1) {
            res.ord = M.A + A + connect(M.B, B);
            return res;       
        }
        swap(A, B);
    }
    int c = A[(A[0] == p)], t = M.A[0];
    if (are_con({c}, {t})) {
        res.ord = fin(M.A, t) + beg(fin(A, p), c) + connect(M.B, B);
    } else {
        res.ord = fin(A, p) + connect(M.B, B) + beg(M.A, t);  
    }
    return res;
}


vector<int> longest_trip(int N_ins, int D) {
    N = N_ins;
    // for (int i = 0; i < N; i++) for (int j = 0; j < N; j++)
    //     g[i][j] = 0;
    // for (int i = 0; i < N; i++)
    //     for (int j = i + 1; j < N; j++) {
    //         g[i][j] = g[j][i] = are_connected({i}, {j});
    //     }
    
    vector<int> v(N);
    for (int i = 0; i < N; i++) v[i] = i;
    info res = recursion(v);
    if (res.ord.empty()) {
        if ((int)res.A.size() < (int)res.B.size())
            swap(res.A, res.B);
        return res.A;
    }
    return res.ord;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 23 ms 444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 23 ms 344 KB Output is correct
3 Correct 127 ms 600 KB Output is correct
4 Correct 284 ms 708 KB Output is correct
5 Correct 733 ms 1148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 22 ms 344 KB Output is correct
3 Correct 105 ms 464 KB Output is correct
4 Correct 365 ms 460 KB Output is correct
5 Correct 712 ms 876 KB Output is correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 28 ms 344 KB Output is correct
8 Correct 116 ms 344 KB Output is correct
9 Correct 289 ms 592 KB Output is correct
10 Correct 715 ms 1320 KB Output is correct
11 Correct 698 ms 784 KB Output is correct
12 Correct 669 ms 756 KB Output is correct
13 Correct 695 ms 824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 344 KB Output is correct
2 Correct 15 ms 344 KB Output is correct
3 Correct 100 ms 344 KB Output is correct
4 Correct 345 ms 588 KB Output is correct
5 Correct 718 ms 824 KB Output is correct
6 Correct 4 ms 344 KB Output is correct
7 Correct 19 ms 344 KB Output is correct
8 Correct 127 ms 344 KB Output is correct
9 Correct 265 ms 956 KB Output is correct
10 Correct 719 ms 1016 KB Output is correct
11 Correct 696 ms 988 KB Output is correct
12 Correct 722 ms 1024 KB Output is correct
13 Correct 775 ms 868 KB Output is correct
14 Correct 5 ms 344 KB Output is correct
15 Correct 6 ms 344 KB Output is correct
16 Correct 15 ms 448 KB Output is correct
17 Correct 26 ms 344 KB Output is correct
18 Correct 34 ms 600 KB Output is correct
19 Correct 47 ms 344 KB Output is correct
20 Correct 39 ms 452 KB Output is correct
21 Correct 83 ms 704 KB Output is correct
22 Correct 73 ms 696 KB Output is correct
23 Correct 78 ms 856 KB Output is correct
24 Correct 78 ms 680 KB Output is correct
25 Correct 6 ms 344 KB Output is correct
26 Correct 4 ms 344 KB Output is correct
27 Correct 7 ms 344 KB Output is correct
28 Correct 5 ms 344 KB Output is correct
29 Correct 7 ms 344 KB Output is correct
30 Correct 7 ms 344 KB Output is correct
31 Correct 7 ms 344 KB Output is correct
32 Correct 6 ms 344 KB Output is correct
33 Correct 11 ms 344 KB Output is correct
34 Correct 8 ms 344 KB Output is correct
35 Correct 8 ms 344 KB Output is correct
36 Correct 8 ms 344 KB Output is correct
37 Correct 8 ms 344 KB Output is correct
38 Correct 10 ms 344 KB Output is correct
39 Correct 9 ms 344 KB Output is correct
40 Correct 12 ms 344 KB Output is correct
41 Correct 12 ms 344 KB Output is correct
42 Correct 9 ms 344 KB Output is correct
43 Correct 10 ms 432 KB Output is correct
44 Correct 9 ms 344 KB Output is correct
45 Correct 6 ms 344 KB Output is correct
46 Correct 6 ms 344 KB Output is correct
47 Correct 10 ms 344 KB Output is correct
48 Correct 7 ms 344 KB Output is correct
49 Correct 10 ms 344 KB Output is correct
50 Correct 8 ms 344 KB Output is correct
51 Correct 8 ms 344 KB Output is correct
52 Correct 9 ms 344 KB Output is correct
53 Correct 7 ms 344 KB Output is correct
54 Correct 7 ms 344 KB Output is correct
55 Correct 12 ms 344 KB Output is correct
56 Correct 10 ms 344 KB Output is correct
57 Correct 12 ms 600 KB Output is correct
58 Correct 11 ms 452 KB Output is correct
59 Correct 11 ms 600 KB Output is correct
60 Correct 10 ms 344 KB Output is correct
61 Correct 14 ms 344 KB Output is correct
62 Correct 9 ms 344 KB Output is correct
63 Correct 9 ms 344 KB Output is correct
64 Correct 11 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 21 ms 344 KB Output is correct
3 Partially correct 113 ms 344 KB Output is partially correct
4 Partially correct 339 ms 728 KB Output is partially correct
5 Partially correct 705 ms 1104 KB Output is partially correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 21 ms 344 KB Output is correct
8 Partially correct 120 ms 344 KB Output is partially correct
9 Partially correct 253 ms 856 KB Output is partially correct
10 Partially correct 708 ms 1236 KB Output is partially correct
11 Partially correct 726 ms 748 KB Output is partially correct
12 Partially correct 673 ms 1160 KB Output is partially correct
13 Partially correct 704 ms 1032 KB Output is partially correct
14 Correct 6 ms 344 KB Output is correct
15 Correct 7 ms 344 KB Output is correct
16 Correct 12 ms 344 KB Output is correct
17 Correct 17 ms 344 KB Output is correct
18 Correct 28 ms 344 KB Output is correct
19 Partially correct 45 ms 464 KB Output is partially correct
20 Partially correct 41 ms 592 KB Output is partially correct
21 Correct 7 ms 344 KB Output is correct
22 Correct 4 ms 344 KB Output is correct
23 Correct 7 ms 408 KB Output is correct
24 Correct 7 ms 344 KB Output is correct
25 Correct 7 ms 344 KB Output is correct
26 Correct 6 ms 340 KB Output is correct
27 Correct 7 ms 344 KB Output is correct
28 Correct 9 ms 344 KB Output is correct
29 Correct 10 ms 344 KB Output is correct
30 Correct 8 ms 344 KB Output is correct
31 Correct 8 ms 440 KB Output is correct
32 Correct 9 ms 344 KB Output is correct
33 Correct 8 ms 344 KB Output is correct
34 Correct 7 ms 344 KB Output is correct
35 Correct 7 ms 344 KB Output is correct
36 Correct 7 ms 344 KB Output is correct
37 Correct 8 ms 448 KB Output is correct
38 Correct 7 ms 344 KB Output is correct
39 Correct 9 ms 344 KB Output is correct
40 Correct 8 ms 344 KB Output is correct
41 Correct 9 ms 344 KB Output is correct
42 Correct 8 ms 344 KB Output is correct
43 Partially correct 77 ms 724 KB Output is partially correct
44 Partially correct 67 ms 736 KB Output is partially correct
45 Partially correct 83 ms 712 KB Output is partially correct
46 Partially correct 69 ms 500 KB Output is partially correct
47 Partially correct 9 ms 344 KB Output is partially correct
48 Partially correct 9 ms 344 KB Output is partially correct
49 Partially correct 12 ms 444 KB Output is partially correct
50 Partially correct 9 ms 344 KB Output is partially correct
51 Partially correct 8 ms 692 KB Output is partially correct
52 Correct 9 ms 344 KB Output is correct
53 Correct 12 ms 344 KB Output is correct
54 Correct 11 ms 436 KB Output is correct
55 Correct 11 ms 600 KB Output is correct
56 Partially correct 8 ms 344 KB Output is partially correct
57 Partially correct 13 ms 444 KB Output is partially correct
58 Partially correct 13 ms 344 KB Output is partially correct
59 Partially correct 13 ms 344 KB Output is partially correct
60 Correct 8 ms 344 KB Output is correct
61 Correct 10 ms 344 KB Output is correct
62 Partially correct 8 ms 444 KB Output is partially correct
63 Partially correct 12 ms 440 KB Output is partially correct
64 Partially correct 12 ms 448 KB Output is partially correct
65 Partially correct 11 ms 600 KB Output is partially correct
66 Partially correct 12 ms 600 KB Output is partially correct
67 Partially correct 10 ms 344 KB Output is partially correct
68 Partially correct 10 ms 600 KB Output is partially correct
69 Partially correct 14 ms 344 KB Output is partially correct
70 Partially correct 12 ms 344 KB Output is partially correct
71 Partially correct 12 ms 600 KB Output is partially correct
72 Partially correct 9 ms 344 KB Output is partially correct
73 Partially correct 10 ms 344 KB Output is partially correct
74 Partially correct 9 ms 448 KB Output is partially correct
75 Partially correct 9 ms 344 KB Output is partially correct
76 Partially correct 11 ms 444 KB Output is partially correct
77 Partially correct 7 ms 344 KB Output is partially correct
78 Partially correct 14 ms 452 KB Output is partially correct
79 Partially correct 14 ms 448 KB Output is partially correct
80 Partially correct 9 ms 344 KB Output is partially correct
81 Partially correct 13 ms 344 KB Output is partially correct
82 Partially correct 13 ms 344 KB Output is partially correct