#include "longesttrip.h"
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
namespace Query {
map<pii, bool> prec;
bool query(int a, int b) {
if(a > b) swap(a, b);
if(prec.count(pii{a, b})) return prec[pii{a, b}];
vector<int> t[2];
t[0].emplace_back(a);
t[1].emplace_back(b);
return prec[pii{a, b}] = are_connected(t[0], t[1]);
}
bool query(int a, vector<int> b) {
vector<int> t[2];
t[0].emplace_back(a);
t[1] = b;
return are_connected(t[0], t[1]);
}
bool query(vector<int> b, int a) {
return query(a, b);
}
bool query(vector<int> a, vector<int> b) {
return are_connected(a, b);
}
};
std::vector<int> longest_trip(int N, int D) {
Query::prec.clear();
using namespace Query;
vector<int> perm(N);
iota(all(perm), 0);
shuffle(all(perm), rng);
vector<int> st[2];
st[0].emplace_back(perm.back());
perm.pop_back();
bool certain = 0;
for(auto x : perm) {
if(sz(st[0]) < sz(st[1])) swap(st[0], st[1]);
if(sz(st[0]) && sz(st[1]) && rng() % 2 == 0) swap(st[0], st[1]);
//cerr << certain << '\t' << st[0].back() << ' ' << (sz(st[1])? st[1].back() : -1) << ' ' << x << '\n';
if(sz(st[1]) == 0) st[1].emplace_back(x);
else if(query(st[0].back(), x)) st[0].emplace_back(x), certain = 0;
else {
if(certain) st[1].emplace_back(x), certain = 0;
else {
if(query(st[1].back(), x))
st[1].emplace_back(x), certain = 1;
else {
copy(rbegin(st[1]), rend(st[1]), back_inserter(st[0]));
st[1].clear();
if(query(st[0].back(), x)) st[0].emplace_back(x);
else certain = 1, st[1].emplace_back(x);
}
}
}
}
if(sz(st[1]) == 0) return st[0];
if(sz(st[0]) == 0) return st[1]; // ???
if(rng() % 2 == 0) swap(st[0], st[1]); // nu o sa aibe niciun efect lol
//for(int i = 1; i < sz(st[0]); i++) assert(query(st[0][i], st[0][i - 1]));
//for(int i = 1; i < sz(st[1]); i++) assert(query(st[1][i], st[1][i - 1]));
//for(auto a : st[0]) cerr << a << ' '; cerr << '\n';
//for(auto a : st[1]) cerr << a << ' '; cerr << '\n';
if(query(st[0][0], st[1])) {
if(query(st[0][0], st[1].back()));
else if(query(st[0][0], st[1][0])) { reverse(all(st[1])); }
else {
int lim = 1;
for(int i = 10; i >= 0; i--) {
int cand = lim + (1 << i);
if(cand >= sz(st[1]) - 1) continue; // scoate -1ul ffs
if(!query(st[0][0], vector<int>(begin(st[1]), begin(st[1]) + cand))) lim = cand;
}
for(int i = 0; i < lim; i++) st[1].emplace_back(st[1][i]);
st[1].erase(begin(st[1]), begin(st[1]) + lim);
reverse(all(st[1]));
}
copy(all(st[0]), back_inserter(st[1]));
return st[1];
}
else {
if(!query(st[0], st[1])) return sz(st[0]) > sz(st[1])? st[0] : st[1];
int lim = 0;
for(int i = 10; i >= 0; i--) {
int cand = lim + (1 << i);
if(cand >= sz(st[1])) continue;
if(query(st[0], vector<int>(begin(st[1]) + cand, end(st[1])))) lim = cand;
}
swap(st[1][0], st[1][lim]);
if(query(st[0].back(), st[1][0]));
else {
lim = 1;
for(int i = 10; i >= 0; i--) {
int cand = lim + (1 << i);
if(cand >= sz(st[0]) - 1) continue; // scoate -1ul ffs
if(!query(st[1][0], vector<int>(begin(st[0]), begin(st[0]) + cand))) lim = cand;
}
for(int i = 0; i < lim; i++) st[0].emplace_back(st[0][i]);
st[0].erase(begin(st[0]), begin(st[0]) + lim);
reverse(all(st[0]));
}
copy(all(st[1]), back_inserter(st[0]));
return st[0];
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
8 ms |
600 KB |
Output is correct |
4 |
Correct |
7 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
8 ms |
344 KB |
Output is correct |
4 |
Correct |
7 ms |
452 KB |
Output is correct |
5 |
Correct |
6 ms |
600 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
8 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
600 KB |
Output is correct |
11 |
Correct |
6 ms |
344 KB |
Output is correct |
12 |
Correct |
7 ms |
464 KB |
Output is correct |
13 |
Correct |
6 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
9 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
624 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
444 KB |
Output is correct |
9 |
Correct |
6 ms |
600 KB |
Output is correct |
10 |
Correct |
8 ms |
344 KB |
Output is correct |
11 |
Correct |
6 ms |
600 KB |
Output is correct |
12 |
Correct |
6 ms |
464 KB |
Output is correct |
13 |
Correct |
6 ms |
464 KB |
Output is correct |
14 |
Correct |
11 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
344 KB |
Output is correct |
17 |
Correct |
9 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
344 KB |
Output is correct |
19 |
Correct |
9 ms |
344 KB |
Output is correct |
20 |
Correct |
8 ms |
344 KB |
Output is correct |
21 |
Correct |
6 ms |
344 KB |
Output is correct |
22 |
Correct |
6 ms |
472 KB |
Output is correct |
23 |
Correct |
6 ms |
344 KB |
Output is correct |
24 |
Correct |
6 ms |
344 KB |
Output is correct |
25 |
Correct |
6 ms |
344 KB |
Output is correct |
26 |
Correct |
7 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
344 KB |
Output is correct |
28 |
Correct |
9 ms |
344 KB |
Output is correct |
29 |
Correct |
7 ms |
344 KB |
Output is correct |
30 |
Correct |
9 ms |
344 KB |
Output is correct |
31 |
Correct |
7 ms |
344 KB |
Output is correct |
32 |
Correct |
7 ms |
344 KB |
Output is correct |
33 |
Correct |
7 ms |
344 KB |
Output is correct |
34 |
Correct |
7 ms |
344 KB |
Output is correct |
35 |
Correct |
7 ms |
344 KB |
Output is correct |
36 |
Correct |
7 ms |
344 KB |
Output is correct |
37 |
Correct |
7 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 |
444 KB |
Output is correct |
41 |
Correct |
8 ms |
344 KB |
Output is correct |
42 |
Correct |
10 ms |
344 KB |
Output is correct |
43 |
Correct |
10 ms |
344 KB |
Output is correct |
44 |
Correct |
8 ms |
344 KB |
Output is correct |
45 |
Correct |
8 ms |
344 KB |
Output is correct |
46 |
Correct |
12 ms |
344 KB |
Output is correct |
47 |
Correct |
8 ms |
344 KB |
Output is correct |
48 |
Correct |
8 ms |
344 KB |
Output is correct |
49 |
Correct |
8 ms |
344 KB |
Output is correct |
50 |
Correct |
7 ms |
344 KB |
Output is correct |
51 |
Correct |
8 ms |
344 KB |
Output is correct |
52 |
Correct |
12 ms |
344 KB |
Output is correct |
53 |
Correct |
7 ms |
344 KB |
Output is correct |
54 |
Correct |
8 ms |
448 KB |
Output is correct |
55 |
Correct |
11 ms |
344 KB |
Output is correct |
56 |
Correct |
7 ms |
344 KB |
Output is correct |
57 |
Correct |
8 ms |
348 KB |
Output is correct |
58 |
Correct |
8 ms |
472 KB |
Output is correct |
59 |
Correct |
7 ms |
344 KB |
Output is correct |
60 |
Correct |
8 ms |
600 KB |
Output is correct |
61 |
Correct |
7 ms |
344 KB |
Output is correct |
62 |
Correct |
8 ms |
344 KB |
Output is correct |
63 |
Correct |
8 ms |
344 KB |
Output is correct |
64 |
Correct |
8 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
344 KB |
Output is correct |
2 |
Correct |
9 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
9 ms |
344 KB |
Output is correct |
8 |
Correct |
8 ms |
344 KB |
Output is correct |
9 |
Correct |
6 ms |
344 KB |
Output is correct |
10 |
Correct |
8 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
6 ms |
596 KB |
Output is correct |
13 |
Correct |
7 ms |
600 KB |
Output is correct |
14 |
Correct |
10 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
344 KB |
Output is correct |
17 |
Correct |
8 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
344 KB |
Output is correct |
19 |
Correct |
7 ms |
452 KB |
Output is correct |
20 |
Correct |
6 ms |
344 KB |
Output is correct |
21 |
Correct |
8 ms |
344 KB |
Output is correct |
22 |
Correct |
8 ms |
344 KB |
Output is correct |
23 |
Correct |
11 ms |
344 KB |
Output is correct |
24 |
Correct |
7 ms |
344 KB |
Output is correct |
25 |
Correct |
11 ms |
344 KB |
Output is correct |
26 |
Correct |
9 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
344 KB |
Output is correct |
28 |
Correct |
7 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 |
7 ms |
344 KB |
Output is correct |
32 |
Correct |
8 ms |
344 KB |
Output is correct |
33 |
Correct |
8 ms |
344 KB |
Output is correct |
34 |
Correct |
10 ms |
344 KB |
Output is correct |
35 |
Correct |
8 ms |
344 KB |
Output is correct |
36 |
Correct |
10 ms |
344 KB |
Output is correct |
37 |
Correct |
7 ms |
344 KB |
Output is correct |
38 |
Correct |
13 ms |
344 KB |
Output is correct |
39 |
Correct |
8 ms |
344 KB |
Output is correct |
40 |
Correct |
9 ms |
344 KB |
Output is correct |
41 |
Correct |
7 ms |
344 KB |
Output is correct |
42 |
Correct |
10 ms |
344 KB |
Output is correct |
43 |
Correct |
9 ms |
460 KB |
Output is correct |
44 |
Correct |
6 ms |
344 KB |
Output is correct |
45 |
Correct |
6 ms |
344 KB |
Output is correct |
46 |
Correct |
6 ms |
344 KB |
Output is correct |
47 |
Correct |
7 ms |
620 KB |
Output is correct |
48 |
Correct |
7 ms |
344 KB |
Output is correct |
49 |
Correct |
9 ms |
600 KB |
Output is correct |
50 |
Correct |
7 ms |
344 KB |
Output is correct |
51 |
Correct |
8 ms |
344 KB |
Output is correct |
52 |
Correct |
7 ms |
344 KB |
Output is correct |
53 |
Correct |
11 ms |
340 KB |
Output is correct |
54 |
Correct |
7 ms |
456 KB |
Output is correct |
55 |
Correct |
7 ms |
344 KB |
Output is correct |
56 |
Correct |
7 ms |
344 KB |
Output is correct |
57 |
Correct |
7 ms |
344 KB |
Output is correct |
58 |
Correct |
10 ms |
344 KB |
Output is correct |
59 |
Correct |
10 ms |
440 KB |
Output is correct |
60 |
Correct |
10 ms |
344 KB |
Output is correct |
61 |
Correct |
7 ms |
344 KB |
Output is correct |
62 |
Correct |
6 ms |
344 KB |
Output is correct |
63 |
Correct |
8 ms |
484 KB |
Output is correct |
64 |
Correct |
8 ms |
344 KB |
Output is correct |
65 |
Correct |
8 ms |
344 KB |
Output is correct |
66 |
Correct |
8 ms |
480 KB |
Output is correct |
67 |
Correct |
8 ms |
344 KB |
Output is correct |
68 |
Correct |
11 ms |
480 KB |
Output is correct |
69 |
Correct |
9 ms |
468 KB |
Output is correct |
70 |
Correct |
8 ms |
344 KB |
Output is correct |
71 |
Correct |
7 ms |
344 KB |
Output is correct |
72 |
Correct |
11 ms |
728 KB |
Output is correct |
73 |
Correct |
8 ms |
344 KB |
Output is correct |
74 |
Correct |
6 ms |
344 KB |
Output is correct |
75 |
Correct |
8 ms |
344 KB |
Output is correct |
76 |
Correct |
10 ms |
344 KB |
Output is correct |
77 |
Correct |
9 ms |
484 KB |
Output is correct |
78 |
Correct |
8 ms |
344 KB |
Output is correct |
79 |
Correct |
9 ms |
480 KB |
Output is correct |
80 |
Correct |
11 ms |
476 KB |
Output is correct |
81 |
Correct |
9 ms |
704 KB |
Output is correct |
82 |
Correct |
11 ms |
344 KB |
Output is correct |