Submission #1031985

# Submission time Handle Problem Language Result Execution time Memory
1031985 2024-07-23T09:26:15 Z abczz Longest Trip (IOI23_longesttrip) C++17
100 / 100
13 ms 600 KB
#include "longesttrip.h"
#include <iostream>
#include <vector>
#include <random>
#include <queue>
#include <algorithm>
#define ll int

using namespace std;

std::vector<int> longest_trip(int N, int D)
{
    random_device rd;
    mt19937_64 mt(rd());
    vector <int> V, U, F, X[257], perm;
    bool res;
    bool B[257];
    ll x, y;
    for (int i=0; i<N; ++i) {
        X[i].clear();
        B[i] = 0;
        perm.push_back(i);
    }
    if (D == 3) {
        for (int i=0; i<N; ++i) V.push_back(i);
        return V;
    }
    else if (D == 2) {
        for (int i=1; i<N; ++i) {
            res = are_connected({0}, {i});
            if (res) {
                B[0] = B[i] = 1;
                U.push_back(0);
                V.push_back(i);
                break;
            }
        }
        for (int i=1; i<N; ++i) {
            if (B[i]) continue;
            res = are_connected({U.back()}, {i});
            if (res) U.push_back(i);
            else V.push_back(i);
        }
        for (int i=U.size()-1; i>=0; --i) F.push_back(U[i]);
        for (auto u : V) F.push_back(u);
        return F;
    }
    else {
        F.push_back(perm[0]);
        for (int i=1; i<N; ++i) {
            U.push_back(perm[i]);
            X[perm[i]].push_back(perm[i]);
        }
        while (F.size() < N) {
            V.clear();
            while (!U.empty()) {
                auto u = U.back();
                U.pop_back();
                res = are_connected({F.back()}, {X[u].back()});
                if (res) {
                    while (!X[u].empty()) {
                        auto v = X[u].back();
                        X[u].pop_back();
                        F.push_back(v);
                    }
                    break;
                }
                else {
                    V.push_back(u);
                }
            }
            if (res) {
                if (V.size() > 1 && X[V[1]].size() > X[V[0]].size()) swap(V[0], V[1]);
                for (int i=1; i<V.size(); ++i) {
                    X[V[0]].push_back(V[i]);
                }
                if (!V.empty()) {
                    if (!U.empty()) {
                        auto v = U.back();
                        U.pop_back();
                        U.push_back(V[0]);
                        U.push_back(v);
                    }
                    else U.push_back(V[0]);
                }
            }
            else break;
        }
        if (F.size() == N) return F;
        if (V.size() > 1 && X[V[1]].size() > X[V[0]].size()) swap(V[0], V[1]);
        for (int i=1; i<V.size(); ++i) {
            X[V[0]].push_back(V[i]);
        }
        if (X[V[0]].size() > 1) {
            res = are_connected({F.back()}, {X[V[0]][0]});
            if (res) {
                for (auto v : X[V[0]]) F.push_back(v);
                return F;
            }
        }
        if (F.size() > 1) {
            res = are_connected({F[0]}, {X[V[0]].back()});
            if (res) {
                reverse(F.begin(), F.end());
                while (!X[V[0]].empty()) {
                    auto v = X[V[0]].back();
                    X[V[0]].pop_back();
                    F.push_back(v);
                }
                return F;
            }
            res = are_connected({F[0]}, {X[V[0]][0]});
            if (res) {
                reverse(F.begin(), F.end());
                for (auto v : X[V[0]]) F.push_back(v);
                return F;
            }
        }
        res = are_connected(F, X[V[0]]);
        if (!res) {
            if (F.size() > X[V[0]].size()) return F;
            else return X[V[0]];
        }
        ll l = 0, r = X[V[0]].size()-1, mid;
        vector <ll> tmp;
        while (l < r) {
            tmp.clear();
            mid = (l+r)/2;
            for (int i=l; i<=mid; ++i) tmp.push_back(X[V[0]][i]);
            res = are_connected(F, tmp);
            if (res) r = mid;
            else l = mid + 1;
        }
        y = l;
        l = 0, r = F.size()-1;
        while (l < r) {
            tmp.clear();
            mid = (l+r)/2;
            for (int i=l; i<=mid; ++i) tmp.push_back(F[i]);
            res = are_connected(tmp, {X[V[0]][y]});
            if (res) r = mid;
            else l = mid + 1;
        }
        x = l;
        U.clear();
        for (int i=x+1; i<F.size(); ++i) U.push_back(F[i]);
        for (int i=0; i<=x; ++i) U.push_back(F[i]);
        for (int i=y; i<X[V[0]].size(); ++i) U.push_back(X[V[0]][i]);
        for (int i=0; i<y; ++i) U.push_back(X[V[0]][i]);
        return U;
    }
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:54:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   54 |         while (F.size() < N) {
      |                ~~~~~~~~~^~~
longesttrip.cpp:74:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |                 for (int i=1; i<V.size(); ++i) {
      |                               ~^~~~~~~~~
longesttrip.cpp:89:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   89 |         if (F.size() == N) return F;
      |             ~~~~~~~~~^~~~
longesttrip.cpp:91:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |         for (int i=1; i<V.size(); ++i) {
      |                       ~^~~~~~~~~
longesttrip.cpp:146:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  146 |         for (int i=x+1; i<F.size(); ++i) U.push_back(F[i]);
      |                         ~^~~~~~~~~
longesttrip.cpp:148:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  148 |         for (int i=y; i<X[V[0]].size(); ++i) U.push_back(X[V[0]][i]);
      |                       ~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
# Verdict Execution time Memory 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 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 6 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 Correct 8 ms 344 KB Output is correct
7 Correct 7 ms 344 KB Output is correct
8 Correct 6 ms 344 KB Output is correct
9 Correct 8 ms 344 KB Output is correct
10 Correct 4 ms 344 KB Output is correct
11 Correct 5 ms 344 KB Output is correct
12 Correct 5 ms 344 KB Output is correct
13 Correct 7 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 7 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 6 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 5 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 5 ms 340 KB Output is correct
13 Correct 5 ms 344 KB Output is correct
14 Correct 11 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 6 ms 344 KB Output is correct
18 Correct 9 ms 344 KB Output is correct
19 Correct 6 ms 344 KB Output is correct
20 Correct 9 ms 344 KB Output is correct
21 Correct 6 ms 344 KB Output is correct
22 Correct 6 ms 344 KB Output is correct
23 Correct 7 ms 344 KB Output is correct
24 Correct 6 ms 344 KB Output is correct
25 Correct 13 ms 344 KB Output is correct
26 Correct 13 ms 344 KB Output is correct
27 Correct 8 ms 344 KB Output is correct
28 Correct 8 ms 344 KB Output is correct
29 Correct 8 ms 344 KB Output is correct
30 Correct 10 ms 344 KB Output is correct
31 Correct 7 ms 344 KB Output is correct
32 Correct 8 ms 344 KB Output is correct
33 Correct 5 ms 600 KB Output is correct
34 Correct 8 ms 344 KB Output is correct
35 Correct 6 ms 456 KB Output is correct
36 Correct 5 ms 344 KB Output is correct
37 Correct 7 ms 344 KB Output is correct
38 Correct 7 ms 344 KB Output is correct
39 Correct 6 ms 344 KB Output is correct
40 Correct 6 ms 344 KB Output is correct
41 Correct 8 ms 344 KB Output is correct
42 Correct 5 ms 344 KB Output is correct
43 Correct 7 ms 600 KB Output is correct
44 Correct 7 ms 344 KB Output is correct
45 Correct 6 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 10 ms 344 KB Output is correct
49 Correct 8 ms 344 KB Output is correct
50 Correct 7 ms 340 KB Output is correct
51 Correct 7 ms 344 KB Output is correct
52 Correct 8 ms 344 KB Output is correct
53 Correct 7 ms 344 KB Output is correct
54 Correct 6 ms 344 KB Output is correct
55 Correct 9 ms 344 KB Output is correct
56 Correct 6 ms 600 KB Output is correct
57 Correct 6 ms 456 KB Output is correct
58 Correct 6 ms 344 KB Output is correct
59 Correct 7 ms 456 KB Output is correct
60 Correct 6 ms 344 KB Output is correct
61 Correct 5 ms 344 KB Output is correct
62 Correct 10 ms 344 KB Output is correct
63 Correct 9 ms 344 KB Output is correct
64 Correct 5 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 6 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 8 ms 344 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 4 ms 344 KB Output is correct
9 Correct 5 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 5 ms 344 KB Output is correct
13 Correct 4 ms 344 KB Output is correct
14 Correct 11 ms 344 KB Output is correct
15 Correct 9 ms 344 KB Output is correct
16 Correct 12 ms 344 KB Output is correct
17 Correct 6 ms 344 KB Output is correct
18 Correct 8 ms 344 KB Output is correct
19 Correct 6 ms 344 KB Output is correct
20 Correct 6 ms 452 KB Output is correct
21 Correct 10 ms 344 KB Output is correct
22 Correct 11 ms 344 KB Output is correct
23 Correct 10 ms 344 KB Output is correct
24 Correct 6 ms 344 KB Output is correct
25 Correct 7 ms 344 KB Output is correct
26 Correct 5 ms 344 KB Output is correct
27 Correct 6 ms 344 KB Output is correct
28 Correct 4 ms 344 KB Output is correct
29 Correct 5 ms 344 KB Output is correct
30 Correct 6 ms 344 KB Output is correct
31 Correct 8 ms 592 KB Output is correct
32 Correct 12 ms 344 KB Output is correct
33 Correct 11 ms 344 KB Output is correct
34 Correct 9 ms 344 KB Output is correct
35 Correct 9 ms 344 KB Output is correct
36 Correct 8 ms 344 KB Output is correct
37 Correct 6 ms 344 KB Output is correct
38 Correct 8 ms 344 KB Output is correct
39 Correct 8 ms 344 KB Output is correct
40 Correct 7 ms 344 KB Output is correct
41 Correct 7 ms 344 KB Output is correct
42 Correct 10 ms 344 KB Output is correct
43 Correct 7 ms 344 KB Output is correct
44 Correct 6 ms 344 KB Output is correct
45 Correct 5 ms 344 KB Output is correct
46 Correct 6 ms 344 KB Output is correct
47 Correct 7 ms 344 KB Output is correct
48 Correct 8 ms 344 KB Output is correct
49 Correct 7 ms 344 KB Output is correct
50 Correct 7 ms 344 KB Output is correct
51 Correct 7 ms 344 KB Output is correct
52 Correct 7 ms 344 KB Output is correct
53 Correct 5 ms 344 KB Output is correct
54 Correct 7 ms 360 KB Output is correct
55 Correct 8 ms 340 KB Output is correct
56 Correct 8 ms 344 KB Output is correct
57 Correct 8 ms 344 KB Output is correct
58 Correct 9 ms 344 KB Output is correct
59 Correct 4 ms 600 KB Output is correct
60 Correct 6 ms 344 KB Output is correct
61 Correct 7 ms 344 KB Output is correct
62 Correct 8 ms 344 KB Output is correct
63 Correct 8 ms 344 KB Output is correct
64 Correct 8 ms 344 KB Output is correct
65 Correct 8 ms 344 KB Output is correct
66 Correct 7 ms 344 KB Output is correct
67 Correct 6 ms 344 KB Output is correct
68 Correct 7 ms 344 KB Output is correct
69 Correct 7 ms 344 KB Output is correct
70 Correct 5 ms 344 KB Output is correct
71 Correct 10 ms 344 KB Output is correct
72 Correct 5 ms 344 KB Output is correct
73 Correct 8 ms 344 KB Output is correct
74 Correct 7 ms 344 KB Output is correct
75 Correct 8 ms 344 KB Output is correct
76 Correct 7 ms 344 KB Output is correct
77 Correct 6 ms 344 KB Output is correct
78 Correct 7 ms 344 KB Output is correct
79 Correct 11 ms 344 KB Output is correct
80 Correct 6 ms 600 KB Output is correct
81 Correct 8 ms 344 KB Output is correct
82 Correct 7 ms 456 KB Output is correct