#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll n, m, q, k, x, y, a, b, c, d;
vector<int> solve2() {
deque<int> dq;
x = -1;
y = -1;
if (are_connected({0}, {1})) {
x = 0;
y = 1;
}
else if (are_connected({0}, {2})) {
x = 0;
y = 2;
}
else {
x = 1;
y = 2;
}
dq.push_back(x);
dq.push_back(y);
for (int i = 0; i < n; i++) {
if (i == x || i == y) {
continue;
}
if (are_connected({dq[0]}, {i})) {
dq.push_front(i);
}
else if (d >= 2 || are_connected({dq[dq.size() - 1]}, {i})) {
dq.push_back(i);
}
}
vector<int> ans;
for (auto i : dq) {
ans.push_back(i);
}
return ans;
}
vector<int> longest_trip(int N, int D)
{
mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
n = N;
d = D;
if (D >= 2) {
return solve2();
}
vector<int> vec(n);
for (int i = 0; i < n; i++) {
vec[i] = i;
}
for (int i = 0; i < n; i++) {
swap(vec[i], vec[gen() % (i + 1)]);
}
deque<int> dq1, dq2;
dq1.push_back(vec[0]);
for (int i = 1; i < n; i++) {
if (are_connected({dq1[dq1.size() - 1]}, {vec[i]})) {
dq1.push_back(vec[i]);
}
else if (dq2.size() == 0 || are_connected({dq2[dq2.size() - 1]}, {vec[i]})) {
dq2.push_back(vec[i]);
}
else {
while (!dq2.empty()) {
dq1.push_back(dq2[dq2.size() - 1]);
dq2.pop_back();
}
dq2.push_back(vec[i]);
}
}
vector<int> ans;
vector<int> ans1, ans2;
for (auto i : dq1) {
ans1.push_back(i);
}
for (auto i : dq2) {
ans2.push_back(i);
}
if (ans2.size() == 0 || !are_connected(ans1, ans2)) {
if (dq1.size() > dq2.size()) {
for (auto i : dq1) {
ans.push_back(i);
}
}
else {
for (auto i : dq2) {
ans.push_back(i);
}
}
return ans;
}
if (are_connected({ans1[0]}, {ans2[0]})) {
reverse(ans1.begin(), ans1.end());
for (int i : ans2) {
ans1.push_back(i);
}
return ans1;
}
// cout << "DEBUG" << endl;
if (are_connected({ans1[0]}, {ans2[ans2.size() - 1]})) {
reverse(ans1.begin(), ans1.end());
reverse(ans2.begin(), ans2.end());
for (int i : ans2) {
ans1.push_back(i);
}
return ans1;
}
if (are_connected({ans1[ans1.size() - 1]}, {ans2[0]})) {
for (int i : ans2) {
ans1.push_back(i);
}
return ans1;
}
if (are_connected({ans1[ans1.size() - 1]}, {ans2[ans2.size() - 1]})) {
reverse(ans2.begin(), ans2.end());
for (int i : ans2) {
ans1.push_back(i);
}
return ans1;
}
ll l = 0, r = ans1.size() - 1;
while (r > l) {
ll mid = (r + l) / 2;
vector<int> chk;
for (int i = l; i <= mid; i++) {
chk.push_back(ans1[i]);
}
if (are_connected(chk, ans2)) {
r = mid;
}
else {
l = mid + 1;
}
}
ll e = l;
l = 0;
r = ans2.size() - 1;
while (r > l) {
ll mid = (r + l) / 2;
vector<int> chk;
for (int i = l; i <= mid; i++) {
chk.push_back(ans2[i]);
}
if (are_connected({ans1[e]}, chk)) {
r = mid;
}
else {
l = mid + 1;
}
}
ll f = l;
for (int i = 0; i < ans1.size(); i++) {
ans.push_back(ans1[(e + 1 + i) % ans1.size()]);
}
for (int i = 0; i < ans2.size(); i++) {
ans.push_back(ans2[(f + i) % ans2.size()]);
}
return ans;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:158:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
158 | for (int i = 0; i < ans1.size(); i++) {
| ~~^~~~~~~~~~~~~
longesttrip.cpp:161:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
161 | for (int i = 0; i < ans2.size(); i++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
436 KB |
Output is correct |
10 |
Correct |
4 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
440 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
10 ms |
344 KB |
Output is correct |
7 |
Correct |
8 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
4 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
6 ms |
344 KB |
Output is correct |
13 |
Correct |
6 ms |
344 KB |
Output is correct |
14 |
Correct |
10 ms |
344 KB |
Output is correct |
15 |
Correct |
8 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 |
5 ms |
344 KB |
Output is correct |
19 |
Correct |
5 ms |
344 KB |
Output is correct |
20 |
Correct |
5 ms |
344 KB |
Output is correct |
21 |
Correct |
6 ms |
344 KB |
Output is correct |
22 |
Correct |
4 ms |
600 KB |
Output is correct |
23 |
Correct |
6 ms |
344 KB |
Output is correct |
24 |
Correct |
5 ms |
444 KB |
Output is correct |
25 |
Correct |
9 ms |
344 KB |
Output is correct |
26 |
Correct |
7 ms |
344 KB |
Output is correct |
27 |
Correct |
6 ms |
344 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
5 ms |
344 KB |
Output is correct |
30 |
Correct |
4 ms |
344 KB |
Output is correct |
31 |
Correct |
8 ms |
344 KB |
Output is correct |
32 |
Correct |
7 ms |
344 KB |
Output is correct |
33 |
Correct |
5 ms |
344 KB |
Output is correct |
34 |
Correct |
4 ms |
344 KB |
Output is correct |
35 |
Correct |
5 ms |
344 KB |
Output is correct |
36 |
Correct |
6 ms |
344 KB |
Output is correct |
37 |
Correct |
5 ms |
344 KB |
Output is correct |
38 |
Correct |
5 ms |
344 KB |
Output is correct |
39 |
Correct |
6 ms |
344 KB |
Output is correct |
40 |
Correct |
6 ms |
344 KB |
Output is correct |
41 |
Correct |
6 ms |
344 KB |
Output is correct |
42 |
Correct |
6 ms |
344 KB |
Output is correct |
43 |
Correct |
6 ms |
344 KB |
Output is correct |
44 |
Correct |
7 ms |
344 KB |
Output is correct |
45 |
Correct |
8 ms |
344 KB |
Output is correct |
46 |
Correct |
9 ms |
344 KB |
Output is correct |
47 |
Correct |
9 ms |
344 KB |
Output is correct |
48 |
Correct |
7 ms |
344 KB |
Output is correct |
49 |
Correct |
7 ms |
344 KB |
Output is correct |
50 |
Correct |
5 ms |
344 KB |
Output is correct |
51 |
Correct |
7 ms |
344 KB |
Output is correct |
52 |
Correct |
8 ms |
344 KB |
Output is correct |
53 |
Correct |
5 ms |
344 KB |
Output is correct |
54 |
Correct |
4 ms |
344 KB |
Output is correct |
55 |
Correct |
8 ms |
596 KB |
Output is correct |
56 |
Correct |
5 ms |
344 KB |
Output is correct |
57 |
Correct |
6 ms |
344 KB |
Output is correct |
58 |
Correct |
7 ms |
344 KB |
Output is correct |
59 |
Correct |
9 ms |
344 KB |
Output is correct |
60 |
Correct |
9 ms |
344 KB |
Output is correct |
61 |
Correct |
10 ms |
344 KB |
Output is correct |
62 |
Correct |
7 ms |
440 KB |
Output is correct |
63 |
Correct |
6 ms |
344 KB |
Output is correct |
64 |
Correct |
10 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
344 KB |
Output is correct |
6 |
Correct |
14 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
344 KB |
Output is correct |
13 |
Correct |
7 ms |
344 KB |
Output is correct |
14 |
Correct |
11 ms |
344 KB |
Output is correct |
15 |
Correct |
10 ms |
344 KB |
Output is correct |
16 |
Correct |
9 ms |
344 KB |
Output is correct |
17 |
Correct |
5 ms |
344 KB |
Output is correct |
18 |
Correct |
7 ms |
344 KB |
Output is correct |
19 |
Correct |
5 ms |
344 KB |
Output is correct |
20 |
Correct |
5 ms |
344 KB |
Output is correct |
21 |
Correct |
5 ms |
344 KB |
Output is correct |
22 |
Correct |
10 ms |
344 KB |
Output is correct |
23 |
Correct |
5 ms |
344 KB |
Output is correct |
24 |
Correct |
5 ms |
344 KB |
Output is correct |
25 |
Correct |
5 ms |
344 KB |
Output is correct |
26 |
Correct |
5 ms |
344 KB |
Output is correct |
27 |
Correct |
6 ms |
344 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
5 ms |
344 KB |
Output is correct |
30 |
Correct |
5 ms |
600 KB |
Output is correct |
31 |
Correct |
5 ms |
344 KB |
Output is correct |
32 |
Correct |
7 ms |
344 KB |
Output is correct |
33 |
Correct |
9 ms |
344 KB |
Output is correct |
34 |
Correct |
7 ms |
344 KB |
Output is correct |
35 |
Correct |
8 ms |
344 KB |
Output is correct |
36 |
Correct |
8 ms |
344 KB |
Output is correct |
37 |
Correct |
4 ms |
344 KB |
Output is correct |
38 |
Correct |
7 ms |
344 KB |
Output is correct |
39 |
Correct |
8 ms |
344 KB |
Output is correct |
40 |
Correct |
5 ms |
344 KB |
Output is correct |
41 |
Correct |
7 ms |
344 KB |
Output is correct |
42 |
Correct |
6 ms |
344 KB |
Output is correct |
43 |
Correct |
4 ms |
344 KB |
Output is correct |
44 |
Correct |
6 ms |
600 KB |
Output is correct |
45 |
Correct |
4 ms |
344 KB |
Output is correct |
46 |
Correct |
6 ms |
344 KB |
Output is correct |
47 |
Correct |
4 ms |
344 KB |
Output is correct |
48 |
Correct |
4 ms |
344 KB |
Output is correct |
49 |
Partially correct |
6 ms |
344 KB |
Output is partially correct |
50 |
Partially correct |
6 ms |
344 KB |
Output is partially correct |
51 |
Partially correct |
6 ms |
344 KB |
Output is partially correct |
52 |
Correct |
6 ms |
344 KB |
Output is correct |
53 |
Correct |
7 ms |
344 KB |
Output is correct |
54 |
Correct |
5 ms |
444 KB |
Output is correct |
55 |
Correct |
6 ms |
344 KB |
Output is correct |
56 |
Correct |
4 ms |
344 KB |
Output is correct |
57 |
Correct |
5 ms |
444 KB |
Output is correct |
58 |
Partially correct |
5 ms |
344 KB |
Output is partially correct |
59 |
Partially correct |
6 ms |
344 KB |
Output is partially correct |
60 |
Correct |
6 ms |
344 KB |
Output is correct |
61 |
Correct |
6 ms |
344 KB |
Output is correct |
62 |
Correct |
5 ms |
344 KB |
Output is correct |
63 |
Correct |
5 ms |
344 KB |
Output is correct |
64 |
Correct |
5 ms |
344 KB |
Output is correct |
65 |
Partially correct |
6 ms |
344 KB |
Output is partially correct |
66 |
Correct |
9 ms |
344 KB |
Output is correct |
67 |
Partially correct |
5 ms |
344 KB |
Output is partially correct |
68 |
Partially correct |
8 ms |
600 KB |
Output is partially correct |
69 |
Partially correct |
6 ms |
344 KB |
Output is partially correct |
70 |
Partially correct |
7 ms |
344 KB |
Output is partially correct |
71 |
Correct |
3 ms |
344 KB |
Output is correct |
72 |
Correct |
6 ms |
344 KB |
Output is correct |
73 |
Partially correct |
6 ms |
444 KB |
Output is partially correct |
74 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
75 |
Partially correct |
6 ms |
344 KB |
Output is partially correct |
76 |
Partially correct |
7 ms |
344 KB |
Output is partially correct |
77 |
Correct |
6 ms |
344 KB |
Output is correct |
78 |
Partially correct |
6 ms |
440 KB |
Output is partially correct |
79 |
Partially correct |
7 ms |
344 KB |
Output is partially correct |
80 |
Partially correct |
6 ms |
344 KB |
Output is partially correct |
81 |
Partially correct |
9 ms |
344 KB |
Output is partially correct |
82 |
Partially correct |
8 ms |
344 KB |
Output is partially correct |