#include "longesttrip.h"
#include<bits/stdc++.h>
using namespace std;
//bool are_connected(std::vector<int> A, std::vector<int> B);
vector<int>A;
vector<int>B;
int al;
int fA(int l, int r, int s=0){
if(l==r){
if(s) return l;
if(are_connected({A[l]},B)) return l;
return -1;
}
int md=(l+r)>>1;
vector<int>check;
for(int i=l;i<=md;i++) check.push_back(A[i]);
if(are_connected(check,B)) return fA(l,md,1);
return fA(md+1,r,s);
}
int fB(int l, int r){
if(l==r) return l;
int md=(l+r)>>1;
vector<int>check;
for(int i=l;i<=md;i++) check.push_back(B[i]);
if(are_connected(check,{A[al]})) return fB(l,md);
return fB(md+1,r);
}
std::vector<int> longest_trip(int N, int D)
{
deque<int>a;
deque<int>b;
a.push_back(0);
b.push_back(1);
bool snc=0;
for(int i=2;i<N;i++){
if(snc==0){
bool ac = are_connected({a.front()},{i});
if(ac){
a.push_front(i);
snc=0;
continue;
}
bool bc = are_connected({b.front()},{i});
if(bc){
b.push_front(i);
snc=1;
continue;
}
while(b.size()){
a.push_front(b.front());
b.pop_front();
}
b.push_front(i);
snc=0;
continue;
}
bool ac = are_connected({a.front()},{i});
if(ac){
a.push_front(i);
snc=0;
continue;
}
b.push_front(i);
snc=1;
continue;
}
vector<int>afb;
vector<int>bfb;
afb.push_back(a.front());
if(a.front()!=a.back()) afb.push_back(a.back());
bfb.push_back(b.front());
if(b.front()!=b.back()) bfb.push_back(b.back());
if(are_connected(afb,bfb)){
if(are_connected({a.front()},{b.front()})){
while(b.size()){
a.push_front(b.front());
b.pop_front();
}
}
else if(are_connected({a.front()},{b.back()})){
while(b.size()){
a.push_front(b.back());
b.pop_back();
}
}else if(are_connected({a.back()},{b.front()})){
while(b.size()){
a.push_back(b.front());
b.pop_front();
}
}
else{
while(b.size()){
a.push_back(b.back());
b.pop_back();
}
}
vector<int>ret;
while(a.size()){
ret.push_back(a.front());
a.pop_front();
}
return ret;
}
A.clear();
B.clear();
while(a.size()){
A.push_back(a.front());
a.pop_front();
}
while(b.size()){
B.push_back(b.front());
b.pop_front();
}
al = fA(0,A.size()-1,0);
if(al==-1) return A.size()>B.size()?A:B;
int bl = fB(0,B.size()-1);
int as = A.size(),bs=B.size();
vector<int>ret;
for(int i=(al+1)%as;i!=al;i=(i+1)%as){
ret.push_back(A[i]);
}
ret.push_back(A[al]);
ret.push_back(B[bl]);
for(int i=(bl+1)%bs;i!=bl;i=(i+1)%bs){
ret.push_back(B[i]);
}
return ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
428 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 |
5 ms |
600 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 |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
764 KB |
Output is correct |
6 |
Correct |
7 ms |
600 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
7 ms |
344 KB |
Output is correct |
9 |
Correct |
5 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 |
6 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
6 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 |
4 ms |
344 KB |
Output is correct |
14 |
Correct |
10 ms |
344 KB |
Output is correct |
15 |
Correct |
9 ms |
344 KB |
Output is correct |
16 |
Correct |
6 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
344 KB |
Output is correct |
19 |
Correct |
7 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 |
6 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 |
6 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 |
6 ms |
344 KB |
Output is correct |
30 |
Correct |
4 ms |
344 KB |
Output is correct |
31 |
Correct |
5 ms |
344 KB |
Output is correct |
32 |
Correct |
5 ms |
344 KB |
Output is correct |
33 |
Correct |
5 ms |
344 KB |
Output is correct |
34 |
Correct |
5 ms |
344 KB |
Output is correct |
35 |
Correct |
5 ms |
344 KB |
Output is correct |
36 |
Correct |
4 ms |
344 KB |
Output is correct |
37 |
Correct |
6 ms |
344 KB |
Output is correct |
38 |
Correct |
5 ms |
600 KB |
Output is correct |
39 |
Correct |
5 ms |
344 KB |
Output is correct |
40 |
Correct |
6 ms |
344 KB |
Output is correct |
41 |
Correct |
8 ms |
344 KB |
Output is correct |
42 |
Correct |
5 ms |
344 KB |
Output is correct |
43 |
Correct |
5 ms |
344 KB |
Output is correct |
44 |
Correct |
5 ms |
344 KB |
Output is correct |
45 |
Correct |
8 ms |
344 KB |
Output is correct |
46 |
Correct |
8 ms |
344 KB |
Output is correct |
47 |
Correct |
6 ms |
344 KB |
Output is correct |
48 |
Correct |
7 ms |
344 KB |
Output is correct |
49 |
Correct |
8 ms |
344 KB |
Output is correct |
50 |
Correct |
5 ms |
344 KB |
Output is correct |
51 |
Correct |
6 ms |
344 KB |
Output is correct |
52 |
Correct |
6 ms |
344 KB |
Output is correct |
53 |
Correct |
5 ms |
344 KB |
Output is correct |
54 |
Correct |
5 ms |
344 KB |
Output is correct |
55 |
Correct |
5 ms |
436 KB |
Output is correct |
56 |
Correct |
6 ms |
344 KB |
Output is correct |
57 |
Correct |
7 ms |
344 KB |
Output is correct |
58 |
Correct |
5 ms |
344 KB |
Output is correct |
59 |
Correct |
6 ms |
344 KB |
Output is correct |
60 |
Correct |
5 ms |
344 KB |
Output is correct |
61 |
Correct |
8 ms |
436 KB |
Output is correct |
62 |
Correct |
4 ms |
344 KB |
Output is correct |
63 |
Correct |
6 ms |
344 KB |
Output is correct |
64 |
Correct |
6 ms |
440 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 |
8 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 |
10 ms |
344 KB |
Output is correct |
7 |
Correct |
8 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
344 KB |
Output is correct |
9 |
Correct |
6 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 |
4 ms |
344 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
14 |
Correct |
8 ms |
344 KB |
Output is correct |
15 |
Correct |
9 ms |
344 KB |
Output is correct |
16 |
Correct |
8 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
344 KB |
Output is correct |
19 |
Correct |
6 ms |
344 KB |
Output is correct |
20 |
Correct |
6 ms |
344 KB |
Output is correct |
21 |
Correct |
7 ms |
344 KB |
Output is correct |
22 |
Correct |
11 ms |
344 KB |
Output is correct |
23 |
Correct |
7 ms |
344 KB |
Output is correct |
24 |
Correct |
9 ms |
344 KB |
Output is correct |
25 |
Correct |
5 ms |
344 KB |
Output is correct |
26 |
Correct |
7 ms |
344 KB |
Output is correct |
27 |
Correct |
9 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 |
7 ms |
344 KB |
Output is correct |
31 |
Correct |
5 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 |
8 ms |
344 KB |
Output is correct |
35 |
Correct |
8 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 |
5 ms |
344 KB |
Output is correct |
41 |
Correct |
7 ms |
340 KB |
Output is correct |
42 |
Correct |
7 ms |
344 KB |
Output is correct |
43 |
Correct |
4 ms |
344 KB |
Output is correct |
44 |
Correct |
5 ms |
344 KB |
Output is correct |
45 |
Correct |
5 ms |
344 KB |
Output is correct |
46 |
Correct |
6 ms |
344 KB |
Output is correct |
47 |
Correct |
5 ms |
600 KB |
Output is correct |
48 |
Correct |
5 ms |
344 KB |
Output is correct |
49 |
Correct |
5 ms |
344 KB |
Output is correct |
50 |
Correct |
6 ms |
344 KB |
Output is correct |
51 |
Correct |
5 ms |
344 KB |
Output is correct |
52 |
Correct |
7 ms |
344 KB |
Output is correct |
53 |
Correct |
7 ms |
344 KB |
Output is correct |
54 |
Correct |
7 ms |
600 KB |
Output is correct |
55 |
Correct |
6 ms |
600 KB |
Output is correct |
56 |
Correct |
5 ms |
344 KB |
Output is correct |
57 |
Correct |
7 ms |
344 KB |
Output is correct |
58 |
Correct |
6 ms |
344 KB |
Output is correct |
59 |
Correct |
6 ms |
412 KB |
Output is correct |
60 |
Correct |
8 ms |
344 KB |
Output is correct |
61 |
Correct |
6 ms |
344 KB |
Output is correct |
62 |
Correct |
6 ms |
356 KB |
Output is correct |
63 |
Correct |
5 ms |
344 KB |
Output is correct |
64 |
Correct |
6 ms |
344 KB |
Output is correct |
65 |
Correct |
5 ms |
344 KB |
Output is correct |
66 |
Correct |
7 ms |
440 KB |
Output is correct |
67 |
Correct |
6 ms |
436 KB |
Output is correct |
68 |
Correct |
6 ms |
344 KB |
Output is correct |
69 |
Correct |
6 ms |
344 KB |
Output is correct |
70 |
Correct |
8 ms |
344 KB |
Output is correct |
71 |
Correct |
5 ms |
440 KB |
Output is correct |
72 |
Correct |
6 ms |
344 KB |
Output is correct |
73 |
Correct |
7 ms |
344 KB |
Output is correct |
74 |
Correct |
7 ms |
440 KB |
Output is correct |
75 |
Correct |
7 ms |
432 KB |
Output is correct |
76 |
Correct |
7 ms |
344 KB |
Output is correct |
77 |
Correct |
5 ms |
344 KB |
Output is correct |
78 |
Correct |
5 ms |
440 KB |
Output is correct |
79 |
Correct |
8 ms |
344 KB |
Output is correct |
80 |
Correct |
5 ms |
344 KB |
Output is correct |
81 |
Correct |
6 ms |
344 KB |
Output is correct |
82 |
Correct |
7 ms |
344 KB |
Output is correct |