Submission #840665

# Submission time Handle Problem Language Result Execution time Memory
840665 2023-08-31T15:17:10 Z Lucpp Longest Trip (IOI23_longesttrip) C++17
85 / 100
21 ms 428 KB
#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
#define sz(x) (int)(x.size())

mt19937 rng(42);

map<pair<int, int>, bool> cache;

bool qry(int a, int b){
    auto p = make_pair(a, b);
    if(cache.count(p)) return cache[p];
    else return cache[p] = are_connected({a}, {b});
}

vector<int> longest_trip(int N, int D){
    cache.clear();
    (void)D;
    vector<vector<int>> paths{{0}, {1}};
    for(int i = 2; i < N; i++){
        int j = rng() % 2;
        if(qry(paths[0].back(), paths[1].back())){
            paths[0].insert(paths[0].end(), paths[1].rbegin(), paths[1].rend());
            paths[1] = {i};
        }
        else if(qry(paths[j].back(), i)) paths[j].push_back(i);
        else paths[j^1].push_back(i);
    }
    if(!are_connected(paths[0], paths[1])){
        return sz(paths[0]) > sz(paths[1]) ? paths[0] : paths[1];
    }
    for(int i = 0; i < 2; i++){
        if(sz(paths[i]) < 3) continue;
        if(!qry(paths[i][0], paths[i].back())){
            if(are_connected({paths[i][0]}, {paths[i^1][0]})){
                reverse(paths[i].begin(), paths[i].end());
            }
            paths[i].insert(paths[i].end(), paths[i^1].begin(), paths[i^1].end());
            return paths[i];
        }
    }
    auto prefix = [&](vector<int> v, int len){
        vector<int> w;
        for(int i = 0; i <= len; i++) w.push_back(v[i]);
        return w;
    };
    int lo = 0, hi = sz(paths[0])-1;
    while(lo < hi){
        int m = (lo+hi)/2;
        if(are_connected(prefix(paths[0], m), paths[1])) hi = m;
        else lo = m+1;
    }
    int p1 = lo;
    lo = 0, hi = sz(paths[1])-1;
    while(lo < hi){
        int m = (lo+hi)/2;
        if(are_connected(prefix(paths[0], p1), prefix(paths[1], m))) hi = m;
        else lo = m+1;
    }
    vector<int> res;
    for(int i = 1; i <= sz(paths[0]); i++){
        res.push_back(paths[0][(p1+i)%sz(paths[0])]);
    }
    for(int i = 0; i < sz(paths[1]); i++){
        res.push_back(paths[1][(lo+i)%sz(paths[1])]);
    }
    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 3 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 208 KB Output is correct
2 Correct 13 ms 208 KB Output is correct
3 Correct 10 ms 208 KB Output is correct
4 Correct 10 ms 208 KB Output is correct
5 Correct 8 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 208 KB Output is correct
2 Correct 10 ms 208 KB Output is correct
3 Correct 8 ms 208 KB Output is correct
4 Correct 9 ms 208 KB Output is correct
5 Correct 10 ms 312 KB Output is correct
6 Correct 8 ms 208 KB Output is correct
7 Correct 11 ms 208 KB Output is correct
8 Correct 11 ms 208 KB Output is correct
9 Correct 8 ms 208 KB Output is correct
10 Correct 10 ms 208 KB Output is correct
11 Correct 9 ms 208 KB Output is correct
12 Correct 8 ms 208 KB Output is correct
13 Correct 10 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 208 KB Output is correct
2 Correct 14 ms 208 KB Output is correct
3 Correct 8 ms 208 KB Output is correct
4 Correct 11 ms 208 KB Output is correct
5 Correct 7 ms 208 KB Output is correct
6 Correct 8 ms 208 KB Output is correct
7 Correct 7 ms 208 KB Output is correct
8 Correct 12 ms 308 KB Output is correct
9 Correct 8 ms 300 KB Output is correct
10 Correct 9 ms 312 KB Output is correct
11 Correct 10 ms 316 KB Output is correct
12 Correct 8 ms 208 KB Output is correct
13 Correct 7 ms 316 KB Output is correct
14 Correct 11 ms 208 KB Output is correct
15 Correct 13 ms 208 KB Output is correct
16 Correct 11 ms 208 KB Output is correct
17 Correct 10 ms 208 KB Output is correct
18 Correct 13 ms 208 KB Output is correct
19 Correct 7 ms 316 KB Output is correct
20 Correct 14 ms 208 KB Output is correct
21 Correct 10 ms 208 KB Output is correct
22 Correct 11 ms 320 KB Output is correct
23 Correct 8 ms 208 KB Output is correct
24 Correct 8 ms 328 KB Output is correct
25 Correct 12 ms 208 KB Output is correct
26 Correct 13 ms 208 KB Output is correct
27 Correct 14 ms 208 KB Output is correct
28 Correct 13 ms 208 KB Output is correct
29 Correct 13 ms 208 KB Output is correct
30 Correct 14 ms 300 KB Output is correct
31 Correct 16 ms 208 KB Output is correct
32 Correct 16 ms 208 KB Output is correct
33 Correct 9 ms 208 KB Output is correct
34 Correct 10 ms 208 KB Output is correct
35 Correct 14 ms 308 KB Output is correct
36 Correct 14 ms 336 KB Output is correct
37 Correct 13 ms 208 KB Output is correct
38 Correct 16 ms 208 KB Output is correct
39 Correct 11 ms 208 KB Output is correct
40 Correct 12 ms 208 KB Output is correct
41 Correct 16 ms 332 KB Output is correct
42 Correct 17 ms 328 KB Output is correct
43 Correct 16 ms 336 KB Output is correct
44 Correct 14 ms 336 KB Output is correct
45 Correct 16 ms 208 KB Output is correct
46 Correct 15 ms 208 KB Output is correct
47 Correct 15 ms 208 KB Output is correct
48 Correct 16 ms 208 KB Output is correct
49 Correct 11 ms 208 KB Output is correct
50 Correct 14 ms 208 KB Output is correct
51 Correct 15 ms 304 KB Output is correct
52 Correct 12 ms 304 KB Output is correct
53 Correct 11 ms 312 KB Output is correct
54 Correct 13 ms 208 KB Output is correct
55 Correct 17 ms 320 KB Output is correct
56 Correct 10 ms 296 KB Output is correct
57 Correct 10 ms 336 KB Output is correct
58 Correct 18 ms 336 KB Output is correct
59 Correct 20 ms 320 KB Output is correct
60 Correct 17 ms 312 KB Output is correct
61 Correct 17 ms 328 KB Output is correct
62 Correct 19 ms 336 KB Output is correct
63 Correct 17 ms 208 KB Output is correct
64 Correct 19 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 208 KB Output is correct
2 Correct 15 ms 208 KB Output is correct
3 Correct 11 ms 208 KB Output is correct
4 Correct 10 ms 208 KB Output is correct
5 Correct 10 ms 208 KB Output is correct
6 Correct 8 ms 208 KB Output is correct
7 Correct 10 ms 208 KB Output is correct
8 Correct 9 ms 208 KB Output is correct
9 Correct 9 ms 208 KB Output is correct
10 Correct 8 ms 208 KB Output is correct
11 Correct 10 ms 212 KB Output is correct
12 Correct 10 ms 208 KB Output is correct
13 Correct 8 ms 320 KB Output is correct
14 Correct 11 ms 208 KB Output is correct
15 Correct 14 ms 208 KB Output is correct
16 Correct 13 ms 208 KB Output is correct
17 Correct 11 ms 208 KB Output is correct
18 Correct 9 ms 300 KB Output is correct
19 Correct 9 ms 316 KB Output is correct
20 Correct 10 ms 320 KB Output is correct
21 Correct 12 ms 208 KB Output is correct
22 Correct 11 ms 208 KB Output is correct
23 Correct 14 ms 208 KB Output is correct
24 Correct 13 ms 208 KB Output is correct
25 Correct 13 ms 208 KB Output is correct
26 Correct 11 ms 308 KB Output is correct
27 Correct 12 ms 208 KB Output is correct
28 Correct 10 ms 208 KB Output is correct
29 Correct 10 ms 208 KB Output is correct
30 Correct 15 ms 208 KB Output is correct
31 Correct 16 ms 208 KB Output is correct
32 Correct 11 ms 208 KB Output is correct
33 Correct 16 ms 208 KB Output is correct
34 Correct 14 ms 208 KB Output is correct
35 Correct 12 ms 208 KB Output is correct
36 Correct 9 ms 208 KB Output is correct
37 Correct 10 ms 208 KB Output is correct
38 Correct 18 ms 208 KB Output is correct
39 Correct 16 ms 208 KB Output is correct
40 Correct 12 ms 312 KB Output is correct
41 Correct 8 ms 320 KB Output is correct
42 Correct 18 ms 316 KB Output is correct
43 Correct 11 ms 320 KB Output is correct
44 Correct 11 ms 328 KB Output is correct
45 Correct 12 ms 300 KB Output is correct
46 Correct 12 ms 328 KB Output is correct
47 Partially correct 14 ms 208 KB Output is partially correct
48 Partially correct 14 ms 332 KB Output is partially correct
49 Partially correct 14 ms 296 KB Output is partially correct
50 Partially correct 15 ms 336 KB Output is partially correct
51 Partially correct 16 ms 208 KB Output is partially correct
52 Partially correct 17 ms 208 KB Output is partially correct
53 Partially correct 12 ms 336 KB Output is partially correct
54 Partially correct 15 ms 336 KB Output is partially correct
55 Partially correct 20 ms 320 KB Output is partially correct
56 Partially correct 15 ms 428 KB Output is partially correct
57 Partially correct 17 ms 300 KB Output is partially correct
58 Partially correct 14 ms 336 KB Output is partially correct
59 Partially correct 14 ms 208 KB Output is partially correct
60 Partially correct 10 ms 208 KB Output is partially correct
61 Partially correct 14 ms 304 KB Output is partially correct
62 Correct 9 ms 208 KB Output is correct
63 Partially correct 12 ms 336 KB Output is partially correct
64 Partially correct 11 ms 208 KB Output is partially correct
65 Partially correct 21 ms 324 KB Output is partially correct
66 Partially correct 11 ms 336 KB Output is partially correct
67 Partially correct 17 ms 208 KB Output is partially correct
68 Partially correct 14 ms 208 KB Output is partially correct
69 Partially correct 10 ms 336 KB Output is partially correct
70 Partially correct 14 ms 296 KB Output is partially correct
71 Partially correct 11 ms 336 KB Output is partially correct
72 Partially correct 18 ms 332 KB Output is partially correct
73 Partially correct 16 ms 336 KB Output is partially correct
74 Partially correct 17 ms 208 KB Output is partially correct
75 Partially correct 13 ms 208 KB Output is partially correct
76 Partially correct 15 ms 308 KB Output is partially correct
77 Partially correct 20 ms 208 KB Output is partially correct
78 Partially correct 18 ms 336 KB Output is partially correct
79 Partially correct 15 ms 304 KB Output is partially correct
80 Partially correct 15 ms 336 KB Output is partially correct
81 Partially correct 18 ms 208 KB Output is partially correct
82 Partially correct 15 ms 208 KB Output is partially correct