Submission #937453

# Submission time Handle Problem Language Result Execution time Memory
937453 2024-03-04T05:50:31 Z GrindMachine Longest Trip (IOI23_longesttrip) C++17
85 / 100
41 ms 1288 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

already knew the solution

*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

#include "longesttrip.h"

std::vector<int> longest_trip(int n, int d)
{
    vector<deque<int>> paths;
    rep(i,n){
        paths.pb({i});
    }

    while(sz(paths) >= 3){
        mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

        int siz = sz(paths);
        vector<int> ord(siz);
        rep(i,siz) ord[i] = i;
        shuffle(all(ord),rng);

        auto p1 = paths[ord[0]];
        auto p2 = paths[ord[1]];
        auto p3 = paths[ord[2]];

        vector<deque<int>> new_paths;
        rep(i,siz){
            bool ok = true;
            rep(j,3){
                if(i == ord[j]){
                    ok = false;
                }
            }

            if(ok){
                new_paths.pb(paths[i]);
            }
        }

        int u = p1[0], v = p2[0], w = p3[0];

        if(are_connected({u},{v})){
            while(!p2.empty()){
                p1.push_front(p2.front());
                p2.pop_front();
            }
        }
        else if(are_connected({u},{w})){
            while(!p3.empty()){
                p1.push_front(p3.front());
                p3.pop_front();
            }
        }
        else{
            while(!p3.empty()){
                p2.push_front(p3.front());
                p3.pop_front();
            }
        }

        if(!p1.empty()){
            new_paths.pb(p1);
        }
        if(!p2.empty()){
            new_paths.pb(p2);
        }
        if(!p3.empty()){
            new_paths.pb(p3);
        }

        paths = new_paths;
    }

    auto p1 = paths[0], p2 = paths[1];
    if(are_connected({p1[0]},{p2[0]})){
        while(!p2.empty()){
            p1.push_front(p2.front());
            p2.pop_front();
        }
    }
    else if(are_connected({p1[0]},{p2.back()})){
        while(!p2.empty()){
            p1.push_front(p2.back());
            p2.pop_back();
        }
    }
    else if(are_connected({p1.back()},{p2[0]})){
        while(!p2.empty()){
            p1.pb(p2.front());
            p2.pop_front();
        }
    }
    else if(are_connected({p1.back()},{p2.back()})){
        while(!p2.empty()){
            p1.pb(p2.back());
            p2.pop_back();
        }
    }

    if(p2.empty()){
        return vector<int>(all(p1));
    }

    int l = 0, r = sz(p1)-1;
    int pos1 = -1;

    while(l <= r){
        int mid = (l+r) >> 1;
        vector<int> v1;
        rep(i,mid+1){
            v1.pb(p1[i]);
        }
        vector<int> v2(all(p2));

        if(are_connected(v1,v2)){
            pos1 = mid;
            r = mid-1;
        }
        else{
            l = mid+1;
        }
    }

    if(pos1 == -1){
        if(sz(p1) > sz(p2)){
            return vector<int>(all(p1));
        }
        else{
            return vector<int>(all(p2));
        }
    }

    l = 0, r = sz(p2)-1;
    int pos2 = -1;

    while(l <= r){
        int mid = (l+r) >> 1;
        vector<int> v1 = {p1[pos1]};
        vector<int> v2;
        rep(i,mid+1){
            v2.pb(p2[i]);
        }

        if(are_connected(v1,v2)){
            pos2 = mid;
            r = mid-1;
        }
        else{
            l = mid+1;
        }
    }

    assert(pos2 != -1);

    rotate(p1.begin(),p1.begin()+pos1,p1.end());
    rotate(p2.begin(),p2.begin()+pos2,p2.end());

    while(!p2.empty()){
        p1.push_front(p2.front());
        p2.pop_front();
    }

    return vector<int>(all(p1));
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 11 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 344 KB Output is correct
2 Correct 11 ms 344 KB Output is correct
3 Correct 15 ms 600 KB Output is correct
4 Correct 24 ms 1112 KB Output is correct
5 Correct 31 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 344 KB Output is correct
2 Correct 11 ms 344 KB Output is correct
3 Correct 15 ms 600 KB Output is correct
4 Correct 24 ms 860 KB Output is correct
5 Correct 30 ms 784 KB Output is correct
6 Correct 10 ms 344 KB Output is correct
7 Correct 11 ms 344 KB Output is correct
8 Correct 18 ms 604 KB Output is correct
9 Correct 20 ms 856 KB Output is correct
10 Correct 30 ms 852 KB Output is correct
11 Correct 31 ms 788 KB Output is correct
12 Correct 33 ms 856 KB Output is correct
13 Correct 30 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 344 KB Output is correct
2 Correct 11 ms 344 KB Output is correct
3 Correct 17 ms 608 KB Output is correct
4 Correct 25 ms 600 KB Output is correct
5 Correct 30 ms 788 KB Output is correct
6 Correct 9 ms 344 KB Output is correct
7 Correct 13 ms 344 KB Output is correct
8 Correct 15 ms 600 KB Output is correct
9 Correct 21 ms 836 KB Output is correct
10 Correct 33 ms 856 KB Output is correct
11 Correct 31 ms 856 KB Output is correct
12 Correct 30 ms 788 KB Output is correct
13 Correct 30 ms 856 KB Output is correct
14 Correct 16 ms 344 KB Output is correct
15 Correct 14 ms 344 KB Output is correct
16 Correct 14 ms 344 KB Output is correct
17 Correct 15 ms 600 KB Output is correct
18 Correct 17 ms 600 KB Output is correct
19 Correct 21 ms 840 KB Output is correct
20 Correct 22 ms 588 KB Output is correct
21 Correct 35 ms 856 KB Output is correct
22 Correct 33 ms 788 KB Output is correct
23 Correct 30 ms 856 KB Output is correct
24 Correct 31 ms 856 KB Output is correct
25 Correct 16 ms 344 KB Output is correct
26 Correct 19 ms 344 KB Output is correct
27 Correct 14 ms 344 KB Output is correct
28 Correct 15 ms 344 KB Output is correct
29 Correct 17 ms 344 KB Output is correct
30 Correct 18 ms 780 KB Output is correct
31 Correct 19 ms 788 KB Output is correct
32 Correct 20 ms 524 KB Output is correct
33 Correct 21 ms 856 KB Output is correct
34 Correct 25 ms 608 KB Output is correct
35 Correct 22 ms 592 KB Output is correct
36 Correct 32 ms 856 KB Output is correct
37 Correct 31 ms 856 KB Output is correct
38 Correct 34 ms 856 KB Output is correct
39 Correct 34 ms 856 KB Output is correct
40 Correct 32 ms 856 KB Output is correct
41 Correct 34 ms 856 KB Output is correct
42 Correct 33 ms 796 KB Output is correct
43 Correct 33 ms 856 KB Output is correct
44 Correct 35 ms 764 KB Output is correct
45 Correct 11 ms 344 KB Output is correct
46 Correct 11 ms 344 KB Output is correct
47 Correct 14 ms 344 KB Output is correct
48 Correct 15 ms 344 KB Output is correct
49 Correct 13 ms 344 KB Output is correct
50 Correct 17 ms 1288 KB Output is correct
51 Correct 20 ms 532 KB Output is correct
52 Correct 20 ms 780 KB Output is correct
53 Correct 21 ms 344 KB Output is correct
54 Correct 21 ms 608 KB Output is correct
55 Correct 21 ms 344 KB Output is correct
56 Correct 39 ms 856 KB Output is correct
57 Correct 34 ms 856 KB Output is correct
58 Correct 34 ms 856 KB Output is correct
59 Correct 33 ms 1112 KB Output is correct
60 Correct 34 ms 788 KB Output is correct
61 Correct 33 ms 856 KB Output is correct
62 Correct 34 ms 824 KB Output is correct
63 Correct 33 ms 856 KB Output is correct
64 Correct 36 ms 1272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 12 ms 344 KB Output is correct
3 Correct 16 ms 600 KB Output is correct
4 Correct 21 ms 856 KB Output is correct
5 Correct 32 ms 788 KB Output is correct
6 Correct 9 ms 344 KB Output is correct
7 Correct 12 ms 596 KB Output is correct
8 Correct 15 ms 512 KB Output is correct
9 Correct 19 ms 600 KB Output is correct
10 Correct 30 ms 856 KB Output is correct
11 Correct 31 ms 856 KB Output is correct
12 Correct 32 ms 856 KB Output is correct
13 Correct 37 ms 856 KB Output is correct
14 Correct 18 ms 344 KB Output is correct
15 Correct 15 ms 344 KB Output is correct
16 Correct 13 ms 600 KB Output is correct
17 Correct 15 ms 600 KB Output is correct
18 Correct 17 ms 600 KB Output is correct
19 Correct 22 ms 600 KB Output is correct
20 Correct 22 ms 564 KB Output is correct
21 Correct 15 ms 344 KB Output is correct
22 Correct 15 ms 344 KB Output is correct
23 Correct 15 ms 344 KB Output is correct
24 Correct 14 ms 344 KB Output is correct
25 Correct 15 ms 344 KB Output is correct
26 Correct 18 ms 784 KB Output is correct
27 Correct 20 ms 1032 KB Output is correct
28 Correct 21 ms 528 KB Output is correct
29 Correct 20 ms 344 KB Output is correct
30 Correct 21 ms 608 KB Output is correct
31 Correct 21 ms 584 KB Output is correct
32 Correct 11 ms 344 KB Output is correct
33 Correct 11 ms 344 KB Output is correct
34 Correct 14 ms 344 KB Output is correct
35 Correct 15 ms 344 KB Output is correct
36 Correct 15 ms 600 KB Output is correct
37 Correct 18 ms 1036 KB Output is correct
38 Correct 19 ms 784 KB Output is correct
39 Correct 22 ms 776 KB Output is correct
40 Correct 21 ms 592 KB Output is correct
41 Correct 20 ms 600 KB Output is correct
42 Correct 21 ms 600 KB Output is correct
43 Correct 35 ms 1104 KB Output is correct
44 Correct 31 ms 856 KB Output is correct
45 Correct 31 ms 856 KB Output is correct
46 Correct 32 ms 856 KB Output is correct
47 Correct 35 ms 1016 KB Output is correct
48 Correct 32 ms 856 KB Output is correct
49 Correct 35 ms 1024 KB Output is correct
50 Correct 36 ms 856 KB Output is correct
51 Partially correct 35 ms 856 KB Output is partially correct
52 Partially correct 33 ms 856 KB Output is partially correct
53 Partially correct 36 ms 856 KB Output is partially correct
54 Partially correct 34 ms 784 KB Output is partially correct
55 Correct 33 ms 856 KB Output is correct
56 Correct 33 ms 788 KB Output is correct
57 Correct 34 ms 856 KB Output is correct
58 Partially correct 34 ms 1040 KB Output is partially correct
59 Correct 35 ms 848 KB Output is correct
60 Partially correct 33 ms 852 KB Output is partially correct
61 Correct 33 ms 788 KB Output is correct
62 Correct 31 ms 788 KB Output is correct
63 Correct 33 ms 856 KB Output is correct
64 Correct 33 ms 784 KB Output is correct
65 Partially correct 33 ms 1112 KB Output is partially correct
66 Partially correct 38 ms 1036 KB Output is partially correct
67 Partially correct 33 ms 892 KB Output is partially correct
68 Partially correct 34 ms 784 KB Output is partially correct
69 Correct 34 ms 856 KB Output is correct
70 Partially correct 36 ms 796 KB Output is partially correct
71 Correct 32 ms 856 KB Output is correct
72 Correct 34 ms 784 KB Output is correct
73 Correct 34 ms 856 KB Output is correct
74 Partially correct 34 ms 888 KB Output is partially correct
75 Partially correct 41 ms 856 KB Output is partially correct
76 Partially correct 33 ms 1104 KB Output is partially correct
77 Correct 35 ms 788 KB Output is correct
78 Correct 33 ms 784 KB Output is correct
79 Correct 33 ms 788 KB Output is correct
80 Partially correct 35 ms 1112 KB Output is partially correct
81 Partially correct 33 ms 788 KB Output is partially correct
82 Partially correct 33 ms 856 KB Output is partially correct