Submission #1031900

# Submission time Handle Problem Language Result Execution time Memory
1031900 2024-07-23T08:33:52 Z abczz Longest Trip (IOI23_longesttrip) C++17
75 / 100
14 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=2; 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) {
                for (int i=1; i<V.size(); ++i) {
                    X[V[0]].push_back(V[i]);
                }
                if (!V.empty()) U.push_back(V[0]);
            }
            else break;
        }
        if (F.size() == N) return F;
        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:73:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |                 for (int i=1; i<V.size(); ++i) {
      |                               ~^~~~~~~~~
longesttrip.cpp:80:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   80 |         if (F.size() == N) return F;
      |             ~~~~~~~~~^~~~
longesttrip.cpp:81:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |         for (int i=1; i<V.size(); ++i) {
      |                       ~^~~~~~~~~
longesttrip.cpp:136:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  136 |         for (int i=x+1; i<F.size(); ++i) U.push_back(F[i]);
      |                         ~^~~~~~~~~
longesttrip.cpp:138:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  138 |         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 0 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB invalid array
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 7 ms 344 KB Output is correct
4 Correct 6 ms 344 KB Output is correct
5 Correct 7 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 7 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 6 ms 344 KB Output is correct
12 Correct 7 ms 344 KB Output is correct
13 Correct 6 ms 344 KB Output is correct
14 Correct 11 ms 344 KB Output is correct
15 Correct 10 ms 344 KB Output is correct
16 Correct 7 ms 344 KB Output is correct
17 Correct 7 ms 344 KB Output is correct
18 Correct 6 ms 344 KB Output is correct
19 Correct 5 ms 344 KB Output is correct
20 Correct 6 ms 452 KB Output is correct
21 Correct 6 ms 344 KB Output is correct
22 Correct 6 ms 596 KB Output is correct
23 Correct 6 ms 344 KB Output is correct
24 Correct 6 ms 344 KB Output is correct
25 Correct 14 ms 344 KB Output is correct
26 Correct 13 ms 344 KB Output is correct
27 Correct 9 ms 344 KB Output is correct
28 Correct 8 ms 344 KB Output is correct
29 Correct 11 ms 344 KB Output is correct
30 Correct 6 ms 344 KB Output is correct
31 Correct 8 ms 344 KB Output is correct
32 Correct 5 ms 344 KB Output is correct
33 Correct 7 ms 344 KB Output is correct
34 Correct 13 ms 344 KB Output is correct
35 Correct 8 ms 448 KB Output is correct
36 Correct 7 ms 432 KB Output is correct
37 Correct 11 ms 344 KB Output is correct
38 Correct 12 ms 344 KB Output is correct
39 Correct 9 ms 340 KB Output is correct
40 Correct 7 ms 344 KB Output is correct
41 Correct 6 ms 344 KB Output is correct
42 Correct 6 ms 344 KB Output is correct
43 Correct 11 ms 600 KB Output is correct
44 Correct 7 ms 344 KB Output is correct
45 Correct 8 ms 344 KB Output is correct
46 Correct 8 ms 344 KB Output is correct
47 Correct 8 ms 344 KB Output is correct
48 Correct 11 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 10 ms 344 KB Output is correct
54 Correct 9 ms 344 KB Output is correct
55 Correct 12 ms 344 KB Output is correct
56 Correct 5 ms 344 KB Output is correct
57 Correct 11 ms 344 KB Output is correct
58 Correct 6 ms 344 KB Output is correct
59 Correct 9 ms 344 KB Output is correct
60 Correct 11 ms 344 KB Output is correct
61 Correct 11 ms 460 KB Output is correct
62 Correct 9 ms 344 KB Output is correct
63 Correct 9 ms 344 KB Output is correct
64 Correct 8 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 344 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 5 ms 600 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 6 ms 596 KB Output is correct
9 Correct 3 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 5 ms 344 KB Output is correct
14 Correct 12 ms 344 KB Output is correct
15 Correct 10 ms 344 KB Output is correct
16 Correct 8 ms 344 KB Output is correct
17 Correct 10 ms 340 KB Output is correct
18 Correct 6 ms 344 KB Output is correct
19 Correct 6 ms 432 KB Output is correct
20 Correct 5 ms 600 KB Output is correct
21 Correct 11 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 7 ms 340 KB Output is correct
25 Correct 10 ms 344 KB Output is correct
26 Correct 8 ms 344 KB Output is correct
27 Correct 12 ms 344 KB Output is correct
28 Correct 7 ms 344 KB Output is correct
29 Correct 4 ms 344 KB Output is correct
30 Correct 9 ms 344 KB Output is correct
31 Correct 8 ms 344 KB Output is correct
32 Correct 11 ms 344 KB Output is correct
33 Correct 8 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 7 ms 344 KB Output is correct
38 Correct 7 ms 344 KB Output is correct
39 Correct 7 ms 344 KB Output is correct
40 Correct 7 ms 344 KB Output is correct
41 Correct 8 ms 344 KB Output is correct
42 Correct 8 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 8 ms 416 KB Output is correct
46 Correct 6 ms 344 KB Output is correct
47 Partially correct 7 ms 344 KB Output is partially correct
48 Partially correct 10 ms 344 KB Output is partially correct
49 Partially correct 7 ms 344 KB Output is partially correct
50 Partially correct 13 ms 344 KB Output is partially correct
51 Partially correct 10 ms 344 KB Output is partially correct
52 Correct 6 ms 344 KB Output is correct
53 Correct 8 ms 344 KB Output is correct
54 Correct 8 ms 344 KB Output is correct
55 Correct 8 ms 344 KB Output is correct
56 Partially correct 7 ms 344 KB Output is partially correct
57 Partially correct 8 ms 344 KB Output is partially correct
58 Partially correct 9 ms 600 KB Output is partially correct
59 Partially correct 7 ms 456 KB Output is partially correct
60 Correct 8 ms 344 KB Output is correct
61 Correct 9 ms 344 KB Output is correct
62 Partially correct 8 ms 460 KB Output is partially correct
63 Partially correct 9 ms 344 KB Output is partially correct
64 Partially correct 6 ms 344 KB Output is partially correct
65 Partially correct 8 ms 344 KB Output is partially correct
66 Partially correct 10 ms 344 KB Output is partially correct
67 Correct 8 ms 344 KB Output is correct
68 Correct 7 ms 344 KB Output is correct
69 Partially correct 8 ms 344 KB Output is partially correct
70 Partially correct 9 ms 344 KB Output is partially correct
71 Partially correct 10 ms 344 KB Output is partially correct
72 Partially correct 8 ms 456 KB Output is partially correct
73 Partially correct 8 ms 344 KB Output is partially correct
74 Partially correct 8 ms 344 KB Output is partially correct
75 Correct 8 ms 344 KB Output is correct
76 Correct 8 ms 344 KB Output is correct
77 Partially correct 10 ms 344 KB Output is partially correct
78 Partially correct 8 ms 460 KB Output is partially correct
79 Partially correct 9 ms 600 KB Output is partially correct
80 Partially correct 8 ms 344 KB Output is partially correct
81 Correct 8 ms 344 KB Output is correct
82 Correct 12 ms 456 KB Output is correct