Submission #958078

# Submission time Handle Problem Language Result Execution time Memory
958078 2024-04-04T20:37:25 Z Nelt Longest Trip (IOI23_longesttrip) C++17
85 / 100
779 ms 2080 KB
#include "longesttrip.h"
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma,popcnt")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

/* DEFINES */
#define S second
#define F first
#define ll long long
#define ull unsigned long long
#define ld long double
#define npos ULLONG_MAX
#define INF LLONG_MAX
#define vv(a) vector<a>
#define ss(a) set<a>
#define pp(a, b) pair<a, b>
#define mm(a, b) map<a, b>
#define qq(a) queue<a>
#define pq(a) priority_queue<a>
#define ump(a, b) unordered_map<a, b>
#define ANDROID                   \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define allc(a) begin(a), end(a)
#define all(a) a, a + sizeof(a) / sizeof(a[0])
#define elif else if
#define endl "\n"
#define pb push_back
#define ins insert
#define logi __lg
#define sqrt sqrtl
#define mpr make_pair
using namespace std;
using namespace __cxx11;
using namespace __gnu_pbds;
typedef char chr;
typedef basic_string<chr> str;
template <typename T, typename key = less<T>>
using ordered_set = tree<T, null_type, key, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 256;
vv(ll) g[N];
bool mat[N][N];
std::vector<int> longest_trip(int n, int d)
{
    for (ll i = 0; i < n; i++)
        for (ll j = 0; j < n; j++)
            mat[i][j] = 0;
    for (ll i = 0; i < n; i++)
        g[i].clear();
    vv(int) ans;
    if (d == 3)
    {
        for (ll i = 0; i < n; i++)
            ans.pb(i);
        return ans;
    }
    if (d == 2)
    {
        for (int i = 0; i < n; i++)
            for (int j = i + 1; j < n; j++)
                if (are_connected({i}, {j}))
                    g[i].pb(j), g[j].pb(i), mat[i][j] = mat[j][i] = true;
        vector<int> ans;
        ll found = 0;
        for (ll i = 0; i + 1 < n; i++)
            if (mat[i][i + 1])
            {
                ans.push_back(i), ans.push_back(i + 1), found = i;
                break;
            }
        for (ll i = 0; i < n; i++)
            if (i != found and i != found + 1)
            {
                if (mat[ans.front()][i])
                    ans.insert(ans.begin(), i);
                else
                    ans.push_back(i);
            }
        return ans;
    }
    vector<int> comp, comp1;
    ll p[n];
    iota(p, p + n, 0);
    shuffle(p, p + n, rng);
    // p[0] = 4;
    // p[1] = 0;
    // p[2] = 1;
    // p[3] = 2;
    // p[4] = 3;
    // for (ll i : p)
    //     cout << i << " ";
    // cout << endl;
    for (int i : p)
        if (comp1.empty())
        {
            if (comp.empty())
            {
                comp = {i};
                continue;
            }
            if (!are_connected({comp.back()}, {i}))
                comp1 = {i};
            else
                comp.push_back(i);
        }
        else if (comp.empty())
        {
            if (!are_connected({comp1.back()}, {i}))
                comp = {i};
            else
                comp1.push_back(i);
        }
        else
        {
            bool ok = are_connected({comp.back()}, {i});
            if (ok)
            {
                bool ok1 = are_connected({comp1.back()}, {i});
                if (ok1)
                {
                    comp.push_back(i);
                    reverse(comp1.begin(), comp1.end());
                    for (ll j : comp1)
                        comp.push_back(j);
                    comp1.clear();
                }
                else
                    comp.push_back(i);
            }
            else
                comp1.push_back(i);
        }
    // cout << endl;
    // for (ll i : comp)
    //     cout << i << " ";
    // cout << endl;
    // for (ll i : comp1)
    //     cout << i << " ";
    // cout << endl;
    // cout << "OK";
    if (comp1.empty())
        return comp;
    if (comp.size() > comp1.size())
        swap(comp, comp1);
    if (!are_connected(comp, comp1))
        return comp1;
    if (comp1.size() >= 3)
    {
        bool ok;
        if (comp.size() >= 3)
        {
            ok = are_connected({comp.front()}, {comp.back()});
            if (!ok)
            {
                ans = comp;
                if (!are_connected({comp.back()}, {comp1.front()}))
                    reverse(ans.begin(), ans.end());
                for (ll i : comp1)
                    ans.push_back(i);
                return ans;
            }
        }
        swap(comp, comp1);
        if (comp.size() >= 3)
        {
            ok = are_connected({comp.front()}, {comp.back()});
            if (!ok)
            {
                ans = comp;
                if (!are_connected({comp.back()}, {comp1.front()}))
                    reverse(ans.begin(), ans.end());
                for (ll i : comp1)
                    ans.push_back(i);
                return ans;
            }
        }
        swap(comp, comp1);
    }
    ll l = 0, r = comp.size() - 1;
    int first = comp.size() - 1;
    vector<int> tmp;
    while (l <= r)
    {
        tmp.clear();
        ll mid = (l + r) >> 1;
        for (ll i = 0; i <= mid; i++)
            tmp.push_back(comp[i]);
        if (are_connected(tmp, comp1))
            r = mid - 1, first = mid;
        else
            l = mid + 1;
    }
    l = 0, r = comp1.size() - 1;
    int second = comp1.size() - 1;
    while (l <= r)
    {
        tmp.clear();
        ll mid = (l + r) >> 1;
        for (ll i = 0; i <= mid; i++)
            tmp.push_back(comp1[i]);
        if (are_connected({comp[first]}, tmp))
            r = mid - 1, second = mid;
        else
            l = mid + 1;
    }
    // cout << comp[first] << " " << comp1[second] << endl;
    for (ll i = first + comp.size() - 1; i >= first; i--)
        ans.push_back(comp[i % comp.size()]);
    for (ll i = second; i < second + comp1.size(); i++)
        ans.push_back(comp1[i % comp1.size()]);
    return ans;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:214:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  214 |     for (ll i = second; i < second + comp1.size(); i++)
      |                         ~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 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 23 ms 456 KB Output is correct
3 Correct 118 ms 856 KB Output is correct
4 Correct 355 ms 1112 KB Output is correct
5 Correct 779 ms 1968 KB Output is correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 20 ms 596 KB Output is correct
8 Correct 125 ms 856 KB Output is correct
9 Correct 265 ms 884 KB Output is correct
10 Correct 754 ms 1840 KB Output is correct
11 Correct 705 ms 1576 KB Output is correct
12 Correct 722 ms 2044 KB Output is correct
13 Correct 748 ms 2080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 4 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 7 ms 344 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 5 ms 344 KB Output is correct
9 Correct 4 ms 344 KB Output is correct
10 Correct 4 ms 344 KB Output is correct
11 Correct 4 ms 344 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 5 ms 600 KB Output is correct
14 Correct 7 ms 344 KB Output is correct
15 Correct 7 ms 344 KB Output is correct
16 Correct 6 ms 344 KB Output is correct
17 Correct 6 ms 344 KB Output is correct
18 Correct 6 ms 344 KB Output is correct
19 Correct 5 ms 632 KB Output is correct
20 Correct 5 ms 600 KB Output is correct
21 Correct 5 ms 344 KB Output is correct
22 Correct 5 ms 344 KB Output is correct
23 Correct 5 ms 668 KB Output is correct
24 Correct 5 ms 344 KB Output is correct
25 Correct 8 ms 344 KB Output is correct
26 Correct 7 ms 344 KB Output is correct
27 Correct 7 ms 596 KB Output is correct
28 Correct 7 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 6 ms 600 KB Output is correct
34 Correct 8 ms 344 KB Output is correct
35 Correct 8 ms 344 KB Output is correct
36 Correct 9 ms 344 KB Output is correct
37 Correct 6 ms 344 KB Output is correct
38 Correct 7 ms 600 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 6 ms 344 KB Output is correct
43 Correct 6 ms 344 KB Output is correct
44 Correct 7 ms 600 KB Output is correct
45 Correct 7 ms 344 KB Output is correct
46 Correct 8 ms 344 KB Output is correct
47 Correct 7 ms 344 KB Output is correct
48 Correct 11 ms 344 KB Output is correct
49 Correct 10 ms 344 KB Output is correct
50 Correct 6 ms 476 KB Output is correct
51 Correct 7 ms 472 KB Output is correct
52 Correct 7 ms 464 KB Output is correct
53 Correct 7 ms 600 KB Output is correct
54 Correct 8 ms 736 KB Output is correct
55 Correct 7 ms 600 KB Output is correct
56 Correct 8 ms 600 KB Output is correct
57 Correct 8 ms 524 KB Output is correct
58 Correct 9 ms 764 KB Output is correct
59 Correct 7 ms 600 KB Output is correct
60 Correct 7 ms 604 KB Output is correct
61 Correct 7 ms 600 KB Output is correct
62 Correct 7 ms 512 KB Output is correct
63 Correct 7 ms 600 KB Output is correct
64 Correct 8 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 8 ms 344 KB Output is correct
3 Correct 5 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 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 5 ms 344 KB Output is correct
11 Correct 4 ms 344 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 5 ms 340 KB Output is correct
14 Correct 7 ms 344 KB Output is correct
15 Correct 11 ms 344 KB Output is correct
16 Correct 9 ms 344 KB Output is correct
17 Correct 6 ms 344 KB Output is correct
18 Correct 6 ms 344 KB Output is correct
19 Correct 5 ms 472 KB Output is correct
20 Correct 5 ms 344 KB Output is correct
21 Correct 9 ms 344 KB Output is correct
22 Correct 6 ms 344 KB Output is correct
23 Correct 9 ms 344 KB Output is correct
24 Correct 7 ms 344 KB Output is correct
25 Correct 9 ms 344 KB Output is correct
26 Correct 6 ms 344 KB Output is correct
27 Correct 7 ms 344 KB Output is correct
28 Correct 6 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 8 ms 344 KB Output is correct
32 Correct 7 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 11 ms 344 KB Output is correct
36 Correct 7 ms 344 KB Output is correct
37 Correct 6 ms 468 KB Output is correct
38 Correct 8 ms 468 KB Output is correct
39 Correct 9 ms 344 KB Output is correct
40 Correct 6 ms 344 KB Output is correct
41 Correct 8 ms 600 KB Output is correct
42 Correct 7 ms 600 KB Output is correct
43 Correct 5 ms 508 KB Output is correct
44 Correct 5 ms 344 KB Output is correct
45 Correct 5 ms 344 KB Output is correct
46 Correct 5 ms 600 KB Output is correct
47 Partially correct 7 ms 344 KB Output is partially correct
48 Partially correct 9 ms 600 KB Output is partially correct
49 Partially correct 8 ms 600 KB Output is partially correct
50 Partially correct 7 ms 344 KB Output is partially correct
51 Partially correct 7 ms 600 KB Output is partially correct
52 Correct 8 ms 344 KB Output is correct
53 Correct 8 ms 600 KB Output is correct
54 Correct 7 ms 344 KB Output is correct
55 Correct 6 ms 596 KB Output is correct
56 Partially correct 8 ms 600 KB Output is partially correct
57 Partially correct 9 ms 600 KB Output is partially correct
58 Partially correct 6 ms 600 KB Output is partially correct
59 Partially correct 7 ms 600 KB Output is partially correct
60 Correct 7 ms 344 KB Output is correct
61 Correct 6 ms 344 KB Output is correct
62 Partially correct 7 ms 860 KB Output is partially correct
63 Partially correct 10 ms 604 KB Output is partially correct
64 Partially correct 10 ms 768 KB Output is partially correct
65 Partially correct 7 ms 772 KB Output is partially correct
66 Partially correct 8 ms 604 KB Output is partially correct
67 Correct 7 ms 600 KB Output is correct
68 Correct 7 ms 604 KB Output is correct
69 Partially correct 7 ms 604 KB Output is partially correct
70 Partially correct 8 ms 604 KB Output is partially correct
71 Partially correct 9 ms 600 KB Output is partially correct
72 Partially correct 8 ms 600 KB Output is partially correct
73 Partially correct 7 ms 772 KB Output is partially correct
74 Partially correct 9 ms 924 KB Output is partially correct
75 Correct 7 ms 600 KB Output is correct
76 Correct 8 ms 600 KB Output is correct
77 Partially correct 9 ms 600 KB Output is partially correct
78 Partially correct 8 ms 772 KB Output is partially correct
79 Partially correct 7 ms 604 KB Output is partially correct
80 Partially correct 8 ms 604 KB Output is partially correct
81 Correct 7 ms 600 KB Output is correct
82 Correct 7 ms 600 KB Output is correct