#include "longesttrip.h"
#include<bits/stdc++.h>
using namespace std;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
vector<int>longest_trip(int n, int d) {
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
vector<int>V, A, B;
rep(i, n) {
V.pb(i);
swap(V[i], V[rng()%(i+1)]);
}
A.pb(V[0]);
for(int i=1; i<n; ++i) {
if(A.size()<B.size()) swap(A, B);
if(B.size()==0) {
B.pb(V[i]);
continue;
}
vector<int>X, Y;
X.pb(V[i]);
if(rng()%2) {
Y.pb(A.back());
if(are_connected(X, Y)) A.pb(V[i]);
else {
Y[0]=B.back();
if(are_connected(X, Y)) B.pb(V[i]);
else {
while(B.size()>0) {
A.pb(B.back());
B.pop_back();
}
}
}
} else {
Y.pb(B.back());
if(are_connected(X, Y)) B.pb(V[i]);
else {
Y[0]=A.back();
if(are_connected(X, Y)) A.pb(V[i]);
else {
while(B.size()>0) {
A.pb(B.back());
B.pop_back();
}
}
}
}
if(rng()%2) reverse(all(A));
if(rng()%2) reverse(all(B));
}
if(B.size()>A.size()) swap(A, B);
if(B.size()==0) return A;
vector<int>X, Y;
X.pb(A[0]);
Y.pb(B[0]);
if(are_connected(X, Y)) {
reverse(all(A));
for(auto i : B) A.pb(i);
return A;
}
Y.clear();
Y.pb(B.back());
if(are_connected(X, Y)) {
for(auto i : A) B.pb(i);
return B;
}
X.clear();
Y.clear();
X.pb(A.back());
Y.pb(B[0]);
if(are_connected(X, Y)) {
for(auto i : B) A.pb(i);
return A;
}
if(!are_connected(A, B)) return A;
int po=0, ko=A.size()-1;
while(po<ko) {
int sr=(po+ko)/2;
vector<int>tmp;
rep(i, sr+1) tmp.pb(A[i]);
if(are_connected(tmp, B)) ko=sr; else po=sr+1;
}
X.clear();
X.pb(A[po]);
int po2=0, ko2=B.size()-1;
while(po2<ko2) {
int sr=(po2+ko2)/2;
vector<int>tmp;
rep(i, sr+1) tmp.pb(B[i]);
if(are_connected(X, tmp)) ko2=sr; else po2=sr+1;
}
vector<int>ans;
rep(i, A.size()) ans.pb(A[(i+po+1)%A.size()]);
rep(i, B.size()) ans.pb(B[(i+po2)%B.size()]);
return ans;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:4:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
longesttrip.cpp:97:3: note: in expansion of macro 'rep'
97 | rep(i, A.size()) ans.pb(A[(i+po+1)%A.size()]);
| ^~~
longesttrip.cpp:4:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
longesttrip.cpp:98:3: note: in expansion of macro 'rep'
98 | rep(i, B.size()) ans.pb(B[(i+po2)%B.size()]);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Incorrect |
3 ms |
208 KB |
Incorrect |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
208 KB |
Output is correct |
2 |
Correct |
10 ms |
208 KB |
Output is correct |
3 |
Correct |
6 ms |
208 KB |
Output is correct |
4 |
Correct |
7 ms |
208 KB |
Output is correct |
5 |
Correct |
7 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
208 KB |
Output is correct |
2 |
Correct |
10 ms |
208 KB |
Output is correct |
3 |
Correct |
7 ms |
208 KB |
Output is correct |
4 |
Correct |
8 ms |
208 KB |
Output is correct |
5 |
Correct |
9 ms |
208 KB |
Output is correct |
6 |
Correct |
8 ms |
208 KB |
Output is correct |
7 |
Correct |
8 ms |
208 KB |
Output is correct |
8 |
Correct |
7 ms |
208 KB |
Output is correct |
9 |
Correct |
10 ms |
208 KB |
Output is correct |
10 |
Correct |
10 ms |
256 KB |
Output is correct |
11 |
Correct |
8 ms |
208 KB |
Output is correct |
12 |
Correct |
9 ms |
208 KB |
Output is correct |
13 |
Correct |
8 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
208 KB |
Output is correct |
2 |
Correct |
9 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 |
9 ms |
208 KB |
Output is correct |
6 |
Correct |
11 ms |
208 KB |
Output is correct |
7 |
Correct |
11 ms |
208 KB |
Output is correct |
8 |
Correct |
7 ms |
208 KB |
Output is correct |
9 |
Correct |
4 ms |
208 KB |
Output is correct |
10 |
Correct |
9 ms |
332 KB |
Output is correct |
11 |
Correct |
9 ms |
208 KB |
Output is correct |
12 |
Correct |
9 ms |
208 KB |
Output is correct |
13 |
Correct |
8 ms |
208 KB |
Output is correct |
14 |
Correct |
11 ms |
332 KB |
Output is correct |
15 |
Correct |
17 ms |
208 KB |
Output is correct |
16 |
Correct |
9 ms |
208 KB |
Output is correct |
17 |
Correct |
8 ms |
208 KB |
Output is correct |
18 |
Correct |
10 ms |
208 KB |
Output is correct |
19 |
Correct |
9 ms |
208 KB |
Output is correct |
20 |
Correct |
11 ms |
208 KB |
Output is correct |
21 |
Correct |
9 ms |
208 KB |
Output is correct |
22 |
Correct |
7 ms |
208 KB |
Output is correct |
23 |
Correct |
6 ms |
208 KB |
Output is correct |
24 |
Correct |
7 ms |
208 KB |
Output is correct |
25 |
Correct |
12 ms |
208 KB |
Output is correct |
26 |
Correct |
18 ms |
208 KB |
Output is correct |
27 |
Correct |
9 ms |
208 KB |
Output is correct |
28 |
Correct |
11 ms |
208 KB |
Output is correct |
29 |
Correct |
9 ms |
208 KB |
Output is correct |
30 |
Correct |
7 ms |
208 KB |
Output is correct |
31 |
Correct |
14 ms |
208 KB |
Output is correct |
32 |
Correct |
9 ms |
208 KB |
Output is correct |
33 |
Correct |
7 ms |
208 KB |
Output is correct |
34 |
Correct |
8 ms |
208 KB |
Output is correct |
35 |
Correct |
7 ms |
208 KB |
Output is correct |
36 |
Correct |
9 ms |
208 KB |
Output is correct |
37 |
Correct |
8 ms |
208 KB |
Output is correct |
38 |
Correct |
10 ms |
208 KB |
Output is correct |
39 |
Correct |
14 ms |
208 KB |
Output is correct |
40 |
Correct |
12 ms |
208 KB |
Output is correct |
41 |
Correct |
10 ms |
208 KB |
Output is correct |
42 |
Correct |
9 ms |
208 KB |
Output is correct |
43 |
Correct |
13 ms |
208 KB |
Output is correct |
44 |
Correct |
13 ms |
208 KB |
Output is correct |
45 |
Correct |
10 ms |
208 KB |
Output is correct |
46 |
Correct |
13 ms |
208 KB |
Output is correct |
47 |
Correct |
11 ms |
208 KB |
Output is correct |
48 |
Correct |
12 ms |
208 KB |
Output is correct |
49 |
Correct |
11 ms |
256 KB |
Output is correct |
50 |
Correct |
9 ms |
208 KB |
Output is correct |
51 |
Correct |
9 ms |
208 KB |
Output is correct |
52 |
Correct |
11 ms |
208 KB |
Output is correct |
53 |
Correct |
6 ms |
208 KB |
Output is correct |
54 |
Correct |
10 ms |
208 KB |
Output is correct |
55 |
Correct |
7 ms |
208 KB |
Output is correct |
56 |
Correct |
7 ms |
208 KB |
Output is correct |
57 |
Correct |
8 ms |
208 KB |
Output is correct |
58 |
Correct |
12 ms |
208 KB |
Output is correct |
59 |
Correct |
10 ms |
208 KB |
Output is correct |
60 |
Correct |
11 ms |
208 KB |
Output is correct |
61 |
Correct |
11 ms |
208 KB |
Output is correct |
62 |
Correct |
12 ms |
384 KB |
Output is correct |
63 |
Correct |
10 ms |
208 KB |
Output is correct |
64 |
Correct |
10 ms |
288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
208 KB |
Output is correct |
2 |
Correct |
7 ms |
208 KB |
Output is correct |
3 |
Correct |
7 ms |
208 KB |
Output is correct |
4 |
Correct |
8 ms |
336 KB |
Output is correct |
5 |
Correct |
6 ms |
208 KB |
Output is correct |
6 |
Correct |
10 ms |
208 KB |
Output is correct |
7 |
Correct |
7 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 |
6 ms |
208 KB |
Output is correct |
11 |
Correct |
9 ms |
208 KB |
Output is correct |
12 |
Correct |
6 ms |
208 KB |
Output is correct |
13 |
Correct |
6 ms |
208 KB |
Output is correct |
14 |
Correct |
13 ms |
208 KB |
Output is correct |
15 |
Incorrect |
0 ms |
208 KB |
Incorrect |
16 |
Halted |
0 ms |
0 KB |
- |