Submission #937451

# Submission time Handle Problem Language Result Execution time Memory
937451 2024-03-04T05:44:56 Z GrindMachine Longest Trip (IOI23_longesttrip) C++17
85 / 100
33 ms 1296 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});
    }

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

    while(sz(paths) >= 3){
        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 0 ms 344 KB Output is correct
2 Correct 7 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 344 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 10 ms 600 KB Output is correct
4 Correct 16 ms 856 KB Output is correct
5 Correct 26 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 344 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 9 ms 544 KB Output is correct
4 Correct 16 ms 600 KB Output is correct
5 Correct 28 ms 784 KB Output is correct
6 Correct 9 ms 344 KB Output is correct
7 Correct 7 ms 344 KB Output is correct
8 Correct 10 ms 600 KB Output is correct
9 Correct 15 ms 600 KB Output is correct
10 Correct 27 ms 856 KB Output is correct
11 Correct 32 ms 856 KB Output is correct
12 Correct 29 ms 856 KB Output is correct
13 Correct 26 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 344 KB Output is correct
2 Correct 7 ms 344 KB Output is correct
3 Correct 10 ms 600 KB Output is correct
4 Correct 16 ms 1112 KB Output is correct
5 Correct 26 ms 856 KB Output is correct
6 Correct 10 ms 344 KB Output is correct
7 Correct 9 ms 340 KB Output is correct
8 Correct 10 ms 600 KB Output is correct
9 Correct 14 ms 600 KB Output is correct
10 Correct 25 ms 788 KB Output is correct
11 Correct 26 ms 856 KB Output is correct
12 Correct 26 ms 856 KB Output is correct
13 Correct 26 ms 792 KB Output is correct
14 Correct 20 ms 344 KB Output is correct
15 Correct 14 ms 344 KB Output is correct
16 Correct 9 ms 600 KB Output is correct
17 Correct 10 ms 344 KB Output is correct
18 Correct 11 ms 856 KB Output is correct
19 Correct 15 ms 600 KB Output is correct
20 Correct 15 ms 600 KB Output is correct
21 Correct 31 ms 856 KB Output is correct
22 Correct 26 ms 792 KB Output is correct
23 Correct 26 ms 856 KB Output is correct
24 Correct 26 ms 856 KB Output is correct
25 Correct 16 ms 344 KB Output is correct
26 Correct 16 ms 344 KB Output is correct
27 Correct 12 ms 344 KB Output is correct
28 Correct 12 ms 344 KB Output is correct
29 Correct 10 ms 344 KB Output is correct
30 Correct 12 ms 784 KB Output is correct
31 Correct 13 ms 528 KB Output is correct
32 Correct 15 ms 528 KB Output is correct
33 Correct 14 ms 856 KB Output is correct
34 Correct 15 ms 600 KB Output is correct
35 Correct 15 ms 600 KB Output is correct
36 Correct 26 ms 896 KB Output is correct
37 Correct 29 ms 856 KB Output is correct
38 Correct 28 ms 856 KB Output is correct
39 Correct 32 ms 788 KB Output is correct
40 Correct 30 ms 788 KB Output is correct
41 Correct 29 ms 856 KB Output is correct
42 Correct 28 ms 1040 KB Output is correct
43 Correct 33 ms 788 KB Output is correct
44 Correct 27 ms 788 KB Output is correct
45 Correct 9 ms 344 KB Output is correct
46 Correct 9 ms 344 KB Output is correct
47 Correct 10 ms 344 KB Output is correct
48 Correct 11 ms 344 KB Output is correct
49 Correct 9 ms 344 KB Output is correct
50 Correct 12 ms 780 KB Output is correct
51 Correct 14 ms 780 KB Output is correct
52 Correct 15 ms 532 KB Output is correct
53 Correct 16 ms 600 KB Output is correct
54 Correct 15 ms 600 KB Output is correct
55 Correct 16 ms 840 KB Output is correct
56 Correct 30 ms 856 KB Output is correct
57 Correct 32 ms 784 KB Output is correct
58 Correct 28 ms 852 KB Output is correct
59 Correct 29 ms 1044 KB Output is correct
60 Correct 29 ms 788 KB Output is correct
61 Correct 31 ms 852 KB Output is correct
62 Correct 28 ms 856 KB Output is correct
63 Correct 28 ms 784 KB Output is correct
64 Correct 31 ms 1296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 7 ms 344 KB Output is correct
3 Correct 10 ms 600 KB Output is correct
4 Correct 16 ms 1112 KB Output is correct
5 Correct 26 ms 856 KB Output is correct
6 Correct 9 ms 344 KB Output is correct
7 Correct 7 ms 344 KB Output is correct
8 Correct 10 ms 600 KB Output is correct
9 Correct 17 ms 852 KB Output is correct
10 Correct 28 ms 784 KB Output is correct
11 Correct 25 ms 856 KB Output is correct
12 Correct 25 ms 856 KB Output is correct
13 Correct 26 ms 856 KB Output is correct
14 Correct 21 ms 344 KB Output is correct
15 Correct 12 ms 344 KB Output is correct
16 Correct 9 ms 600 KB Output is correct
17 Correct 10 ms 600 KB Output is correct
18 Correct 11 ms 600 KB Output is correct
19 Correct 15 ms 600 KB Output is correct
20 Correct 15 ms 856 KB Output is correct
21 Correct 14 ms 344 KB Output is correct
22 Correct 14 ms 344 KB Output is correct
23 Correct 12 ms 596 KB Output is correct
24 Correct 12 ms 600 KB Output is correct
25 Correct 11 ms 344 KB Output is correct
26 Correct 13 ms 524 KB Output is correct
27 Correct 13 ms 1044 KB Output is correct
28 Correct 14 ms 524 KB Output is correct
29 Correct 15 ms 600 KB Output is correct
30 Correct 14 ms 600 KB Output is correct
31 Correct 17 ms 600 KB Output is correct
32 Correct 10 ms 344 KB Output is correct
33 Correct 12 ms 344 KB Output is correct
34 Correct 11 ms 344 KB Output is correct
35 Correct 10 ms 344 KB Output is correct
36 Correct 9 ms 344 KB Output is correct
37 Correct 12 ms 780 KB Output is correct
38 Correct 14 ms 784 KB Output is correct
39 Correct 14 ms 784 KB Output is correct
40 Correct 16 ms 592 KB Output is correct
41 Correct 15 ms 600 KB Output is correct
42 Correct 16 ms 600 KB Output is correct
43 Correct 26 ms 856 KB Output is correct
44 Correct 28 ms 856 KB Output is correct
45 Correct 28 ms 856 KB Output is correct
46 Correct 29 ms 788 KB Output is correct
47 Correct 28 ms 828 KB Output is correct
48 Correct 27 ms 1044 KB Output is correct
49 Correct 28 ms 856 KB Output is correct
50 Correct 28 ms 856 KB Output is correct
51 Partially correct 29 ms 1040 KB Output is partially correct
52 Partially correct 28 ms 780 KB Output is partially correct
53 Partially correct 30 ms 1052 KB Output is partially correct
54 Partially correct 28 ms 784 KB Output is partially correct
55 Partially correct 29 ms 784 KB Output is partially correct
56 Correct 28 ms 788 KB Output is correct
57 Correct 28 ms 784 KB Output is correct
58 Correct 32 ms 856 KB Output is correct
59 Correct 28 ms 792 KB Output is correct
60 Partially correct 30 ms 600 KB Output is partially correct
61 Partially correct 29 ms 1112 KB Output is partially correct
62 Correct 26 ms 784 KB Output is correct
63 Correct 27 ms 856 KB Output is correct
64 Correct 29 ms 856 KB Output is correct
65 Partially correct 30 ms 856 KB Output is partially correct
66 Correct 28 ms 856 KB Output is correct
67 Partially correct 31 ms 856 KB Output is partially correct
68 Partially correct 28 ms 856 KB Output is partially correct
69 Partially correct 32 ms 860 KB Output is partially correct
70 Partially correct 31 ms 792 KB Output is partially correct
71 Correct 27 ms 856 KB Output is correct
72 Correct 27 ms 788 KB Output is correct
73 Correct 28 ms 856 KB Output is correct
74 Partially correct 28 ms 1104 KB Output is partially correct
75 Partially correct 29 ms 856 KB Output is partially correct
76 Correct 32 ms 856 KB Output is correct
77 Correct 30 ms 796 KB Output is correct
78 Correct 32 ms 856 KB Output is correct
79 Correct 28 ms 856 KB Output is correct
80 Partially correct 32 ms 820 KB Output is partially correct
81 Partially correct 28 ms 856 KB Output is partially correct
82 Partially correct 30 ms 788 KB Output is partially correct