Submission #840669

# Submission time Handle Problem Language Result Execution time Memory
840669 2023-08-31T15:20:17 Z Lucpp Longest Trip (IOI23_longesttrip) C++17
85 / 100
18 ms 436 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){
    if(a > b) swap(a, 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 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 208 KB Output is correct
2 Correct 13 ms 208 KB Output is correct
3 Correct 9 ms 208 KB Output is correct
4 Correct 9 ms 208 KB Output is correct
5 Correct 10 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 208 KB Output is correct
2 Correct 14 ms 208 KB Output is correct
3 Correct 6 ms 208 KB Output is correct
4 Correct 11 ms 208 KB Output is correct
5 Correct 10 ms 208 KB Output is correct
6 Correct 14 ms 208 KB Output is correct
7 Correct 14 ms 208 KB Output is correct
8 Correct 8 ms 208 KB Output is correct
9 Correct 7 ms 208 KB Output is correct
10 Correct 10 ms 304 KB Output is correct
11 Correct 9 ms 208 KB Output is correct
12 Correct 10 ms 208 KB Output is correct
13 Correct 8 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 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 11 ms 208 KB Output is correct
5 Correct 7 ms 208 KB Output is correct
6 Correct 16 ms 208 KB Output is correct
7 Correct 10 ms 208 KB Output is correct
8 Correct 12 ms 208 KB Output is correct
9 Correct 8 ms 208 KB Output is correct
10 Correct 7 ms 208 KB Output is correct
11 Correct 10 ms 208 KB Output is correct
12 Correct 7 ms 208 KB Output is correct
13 Correct 7 ms 208 KB Output is correct
14 Correct 11 ms 208 KB Output is correct
15 Correct 13 ms 332 KB Output is correct
16 Correct 16 ms 208 KB Output is correct
17 Correct 12 ms 208 KB Output is correct
18 Correct 10 ms 304 KB Output is correct
19 Correct 11 ms 208 KB Output is correct
20 Correct 10 ms 316 KB Output is correct
21 Correct 11 ms 208 KB Output is correct
22 Correct 10 ms 208 KB Output is correct
23 Correct 12 ms 320 KB Output is correct
24 Correct 9 ms 208 KB Output is correct
25 Correct 10 ms 208 KB Output is correct
26 Correct 12 ms 208 KB Output is correct
27 Correct 8 ms 208 KB Output is correct
28 Correct 8 ms 208 KB Output is correct
29 Correct 10 ms 208 KB Output is correct
30 Correct 12 ms 208 KB Output is correct
31 Correct 14 ms 208 KB Output is correct
32 Correct 15 ms 208 KB Output is correct
33 Correct 11 ms 208 KB Output is correct
34 Correct 13 ms 308 KB Output is correct
35 Correct 14 ms 208 KB Output is correct
36 Correct 11 ms 208 KB Output is correct
37 Correct 11 ms 316 KB Output is correct
38 Correct 14 ms 260 KB Output is correct
39 Correct 14 ms 208 KB Output is correct
40 Correct 14 ms 208 KB Output is correct
41 Correct 14 ms 208 KB Output is correct
42 Correct 18 ms 336 KB Output is correct
43 Correct 12 ms 208 KB Output is correct
44 Correct 10 ms 208 KB Output is correct
45 Correct 13 ms 208 KB Output is correct
46 Correct 8 ms 208 KB Output is correct
47 Correct 11 ms 208 KB Output is correct
48 Correct 14 ms 208 KB Output is correct
49 Correct 14 ms 208 KB Output is correct
50 Correct 12 ms 308 KB Output is correct
51 Correct 12 ms 208 KB Output is correct
52 Correct 12 ms 208 KB Output is correct
53 Correct 9 ms 208 KB Output is correct
54 Correct 13 ms 208 KB Output is correct
55 Correct 14 ms 208 KB Output is correct
56 Correct 8 ms 316 KB Output is correct
57 Correct 13 ms 336 KB Output is correct
58 Correct 13 ms 208 KB Output is correct
59 Correct 14 ms 436 KB Output is correct
60 Correct 9 ms 328 KB Output is correct
61 Correct 11 ms 332 KB Output is correct
62 Correct 10 ms 208 KB Output is correct
63 Correct 13 ms 208 KB Output is correct
64 Correct 17 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 208 KB Output is correct
2 Correct 7 ms 208 KB Output is correct
3 Correct 11 ms 208 KB Output is correct
4 Correct 9 ms 208 KB Output is correct
5 Correct 8 ms 260 KB Output is correct
6 Correct 13 ms 208 KB Output is correct
7 Correct 14 ms 208 KB Output is correct
8 Correct 11 ms 208 KB Output is correct
9 Correct 12 ms 208 KB Output is correct
10 Correct 8 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 11 ms 208 KB Output is correct
14 Correct 9 ms 208 KB Output is correct
15 Correct 8 ms 208 KB Output is correct
16 Correct 11 ms 208 KB Output is correct
17 Correct 13 ms 208 KB Output is correct
18 Correct 12 ms 208 KB Output is correct
19 Correct 13 ms 208 KB Output is correct
20 Correct 10 ms 208 KB Output is correct
21 Correct 12 ms 208 KB Output is correct
22 Correct 11 ms 208 KB Output is correct
23 Correct 15 ms 208 KB Output is correct
24 Correct 15 ms 208 KB Output is correct
25 Correct 9 ms 208 KB Output is correct
26 Correct 9 ms 208 KB Output is correct
27 Correct 14 ms 208 KB Output is correct
28 Correct 14 ms 308 KB Output is correct
29 Correct 12 ms 208 KB Output is correct
30 Correct 12 ms 260 KB Output is correct
31 Correct 13 ms 208 KB Output is correct
32 Correct 16 ms 208 KB Output is correct
33 Correct 17 ms 208 KB Output is correct
34 Correct 11 ms 208 KB Output is correct
35 Correct 14 ms 208 KB Output is correct
36 Correct 14 ms 208 KB Output is correct
37 Correct 12 ms 208 KB Output is correct
38 Correct 11 ms 208 KB Output is correct
39 Correct 10 ms 208 KB Output is correct
40 Correct 12 ms 208 KB Output is correct
41 Correct 15 ms 208 KB Output is correct
42 Correct 14 ms 208 KB Output is correct
43 Correct 11 ms 208 KB Output is correct
44 Correct 12 ms 208 KB Output is correct
45 Correct 9 ms 208 KB Output is correct
46 Correct 8 ms 208 KB Output is correct
47 Correct 9 ms 208 KB Output is correct
48 Correct 12 ms 208 KB Output is correct
49 Correct 15 ms 208 KB Output is correct
50 Correct 11 ms 208 KB Output is correct
51 Correct 13 ms 208 KB Output is correct
52 Correct 11 ms 208 KB Output is correct
53 Correct 8 ms 208 KB Output is correct
54 Correct 14 ms 208 KB Output is correct
55 Correct 10 ms 208 KB Output is correct
56 Correct 8 ms 208 KB Output is correct
57 Correct 15 ms 208 KB Output is correct
58 Correct 15 ms 208 KB Output is correct
59 Correct 15 ms 208 KB Output is correct
60 Correct 12 ms 208 KB Output is correct
61 Correct 14 ms 208 KB Output is correct
62 Correct 9 ms 320 KB Output is correct
63 Partially correct 11 ms 336 KB Output is partially correct
64 Partially correct 16 ms 320 KB Output is partially correct
65 Partially correct 8 ms 336 KB Output is partially correct
66 Correct 15 ms 208 KB Output is correct
67 Partially correct 13 ms 336 KB Output is partially correct
68 Partially correct 10 ms 312 KB Output is partially correct
69 Partially correct 15 ms 208 KB Output is partially correct
70 Partially correct 14 ms 336 KB Output is partially correct
71 Correct 11 ms 208 KB Output is correct
72 Partially correct 16 ms 208 KB Output is partially correct
73 Partially correct 15 ms 208 KB Output is partially correct
74 Partially correct 15 ms 208 KB Output is partially correct
75 Partially correct 10 ms 312 KB Output is partially correct
76 Partially correct 15 ms 208 KB Output is partially correct
77 Correct 14 ms 208 KB Output is correct
78 Partially correct 14 ms 208 KB Output is partially correct
79 Partially correct 9 ms 328 KB Output is partially correct
80 Partially correct 15 ms 208 KB Output is partially correct
81 Partially correct 15 ms 208 KB Output is partially correct
82 Partially correct 10 ms 208 KB Output is partially correct