#include "longesttrip.h"
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
ll n,i,j,k,s,t,x,y,z,w;
deque<ll> dq1,dq2;
vector<ll>v;
pair<ll,ll> p;
pair<ll,ll> inv(pair<ll,ll> p)
{
return {p.second,p.first};
}
pair<ll,ll> adj(vector<ll> a,vector<ll> b)
{
ll k=0;
if(a.size()<b.size())
{swap(a,b); k=1;}
if(a.size()==1&&b.size()==1)
return {a[0],b[0]};
vector<ll>c;
for(i=0;i<a.size()/2;i++)
c.push_back(a[i]);
if(are_connected(c,b))
{
if(k==1)
{
return inv(adj(c,b));
}
else
return adj(c,b);
}
else
{
c.clear();
for(i=a.size()/2;i<a.size();i++)
c.push_back(a[i]);
if(k==1)
{
return inv(adj(c,b));
}
else
return adj(c,b);
}
}
vector<ll> vectorize(deque<ll> d)
{
ll i;
vector<ll> v;
for(i=0;i<d.size();i++)
v.push_back(d[i]);
return v;
}
void combine()
{
for(auto x:dq2)
dq1.push_front(x);
dq2.clear();
}
void answer()
{
ll i;
v.clear();
for(i=0;i<dq1.size();i++)
{
v.push_back(dq1[i]);
}
}
void init()
{
dq1.clear();
dq2.clear();
v.clear();
s=0;
t=0;
}
vector<ll> longest_trip(ll N, ll D)
{
init();
n=N;
dq1.push_back(0);
dq2.push_back(1);
for(i=2;i<n;i++)
{
if(i==n-1)
{if(are_connected({dq1[0]},{dq2[0]}))
{
combine();
dq2.push_back(i);
continue;
}
if(are_connected({dq1[0]},{i}))
{
dq1.push_front(i);
continue;
}
dq2.push_front(i);
}
else
{
if(are_connected({i},{i+1}))
{
if(are_connected({dq1[0]},{dq2[0]}))
{
combine();
dq2.push_back(i);
dq2.push_back(i+1);
i++;
continue;
}
if(are_connected({dq1[0]},{i}))
{
dq1.push_front(i);
dq1.push_front(i+1);
i++;
continue;
}
dq2.push_front(i);
dq2.push_front(i+1);
}
else
{
if(are_connected({dq1[0]},{i}))
{
dq1.push_front(i);
if(are_connected({dq2[0]},{i}))
{
combine();
dq2.push_back(i+1);
}
else
{
dq2.push_front(i+1);
}
}
else
{
dq1.push_front(i+1);
if(are_connected({dq2[0]},{i+1}))
{
combine();
dq2.push_back(i);
}
else
{
dq2.push_front(i);
}
}
}
i++;
}
}
if(dq1.size()<dq2.size())
swap(dq1,dq2);
if(!are_connected(vectorize(dq1),vectorize(dq2)))
{
answer();
return v;
}
/*for(ll x:dq1)
printf("%d ",x);
printf("\n");
for(ll x:dq2)
printf("%d ",x);
printf("\n");*/
if(dq2.size()==1)
{
if(are_connected({dq1[0]},{dq2[0]}))
{
dq1.push_front(dq2[0]);
}
else if(are_connected({dq1[dq1.size()-1]},{dq2[0]}))
{
dq1.push_back(dq2[0]);
}
else
{
for(i=0;i<dq1.size();i++)
{
v.push_back(dq1[i]);
}
x=adj(vectorize(dq1),vectorize(dq2)).first;
v.clear();
v.push_back(dq2[0]);
for(i=0;i<dq1.size();i++)
{
if(dq1[i]==x)
{
t=1;
}
if(t==1)
v.push_back(dq1[i]);
}
for(i=0;i<dq1.size();i++)
{
if(dq1[i]==x)
t=0;
if(t==1)
v.push_back(dq1[i]);
}
return v;
}
answer();
return v;
}
if(are_connected({dq1[0]},{dq2[0],dq2.back()}))
{
if(are_connected({dq1[0]},{dq2[0]}))
{
for(auto x:dq2)
dq1.push_front(x);
answer();
return v;
}
else
{
for(i=dq2.size()-1;i>=0;i--)
{
dq1.push_front(dq2[i]);
}
answer();
return v;
}
}
else if(are_connected({dq1[dq1.size()-1]},{dq2[0],dq2.back()}))
{
if(are_connected({dq1[dq1.size()-1]},{dq2[0]}))
{
for(auto x:dq2)
dq1.push_back(x);
answer();
return v;
}
else
{
for(i=dq2.size()-1;i>=0;i--)
{
dq1.push_back(dq2[i]);
}
answer();
return v;
}
}
else
{
p=adj(vectorize(dq1),vectorize(dq2));
x=p.first;
y=p.second;
//printf("%d %d\n",x,y);
t=0;
for(i=0;i<dq1.size();i++)
{
if(t==1)
{
v.push_back(dq1[i]);
}
if(dq1[i]==x)
t=1;
}
for(i=0;i<dq1.size();i++)
{
if(t==1)
v.push_back(dq1[i]);
if(dq1[i]==x)
t=0;
}
for(i=0;i<dq2.size();i++)
{
if(dq2[i]==y)
t=1;
if(t==1)
{
v.push_back(dq2[i]);
}
}
for(i=0;i<dq2.size();i++)
{
if(dq2[i]==y)
t=0;
if(t==1)
v.push_back(dq2[i]);
}
return v;
}
}
Compilation message
longesttrip.cpp: In function 'std::pair<int, int> adj(std::vector<int>, std::vector<int>)':
longesttrip.cpp:21:14: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for(i=0;i<a.size()/2;i++)
| ~^~~~~~~~~~~
longesttrip.cpp:35:27: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(i=a.size()/2;i<a.size();i++)
| ~^~~~~~~~~
longesttrip.cpp: In function 'std::vector<int> vectorize(std::deque<int>)':
longesttrip.cpp:49:14: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(i=0;i<d.size();i++)
| ~^~~~~~~~~
longesttrip.cpp: In function 'void answer()':
longesttrip.cpp:63:14: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(i=0;i<dq1.size();i++)
| ~^~~~~~~~~~~
longesttrip.cpp: In function 'std::vector<int> longest_trip(ll, ll)':
longesttrip.cpp:152:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
152 | if(dq1.size()<dq2.size())
| ^~
longesttrip.cpp:154:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
154 | if(!are_connected(vectorize(dq1),vectorize(dq2)))
| ^~
longesttrip.cpp:177:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
177 | for(i=0;i<dq1.size();i++)
| ~^~~~~~~~~~~
longesttrip.cpp:184:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
184 | for(i=0;i<dq1.size();i++)
| ~^~~~~~~~~~~
longesttrip.cpp:193:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
193 | for(i=0;i<dq1.size();i++)
| ~^~~~~~~~~~~
longesttrip.cpp:250:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
250 | for(i=0;i<dq1.size();i++)
| ~^~~~~~~~~~~
longesttrip.cpp:259:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
259 | for(i=0;i<dq1.size();i++)
| ~^~~~~~~~~~~
longesttrip.cpp:266:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
266 | for(i=0;i<dq2.size();i++)
| ~^~~~~~~~~~~
longesttrip.cpp:275:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
275 | for(i=0;i<dq2.size();i++)
| ~^~~~~~~~~~~
longesttrip.cpp:277:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
277 | if(dq2[i]==y)
| ^~
longesttrip.cpp:279:17: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
279 | if(t==1)
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
512 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
600 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
7 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
600 KB |
Output is correct |
11 |
Correct |
5 ms |
600 KB |
Output is correct |
12 |
Correct |
4 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
7 ms |
344 KB |
Output is correct |
6 |
Correct |
9 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 |
344 KB |
Output is correct |
10 |
Correct |
4 ms |
600 KB |
Output is correct |
11 |
Correct |
4 ms |
600 KB |
Output is correct |
12 |
Correct |
4 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
14 |
Correct |
8 ms |
512 KB |
Output is correct |
15 |
Correct |
6 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 |
5 ms |
344 KB |
Output is correct |
19 |
Correct |
6 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 |
5 ms |
600 KB |
Output is correct |
23 |
Correct |
5 ms |
344 KB |
Output is correct |
24 |
Correct |
6 ms |
600 KB |
Output is correct |
25 |
Correct |
6 ms |
344 KB |
Output is correct |
26 |
Correct |
6 ms |
344 KB |
Output is correct |
27 |
Correct |
6 ms |
344 KB |
Output is correct |
28 |
Correct |
8 ms |
344 KB |
Output is correct |
29 |
Correct |
6 ms |
596 KB |
Output is correct |
30 |
Correct |
5 ms |
440 KB |
Output is correct |
31 |
Correct |
7 ms |
444 KB |
Output is correct |
32 |
Correct |
6 ms |
444 KB |
Output is correct |
33 |
Correct |
6 ms |
344 KB |
Output is correct |
34 |
Correct |
5 ms |
344 KB |
Output is correct |
35 |
Correct |
7 ms |
344 KB |
Output is correct |
36 |
Correct |
6 ms |
344 KB |
Output is correct |
37 |
Correct |
6 ms |
344 KB |
Output is correct |
38 |
Correct |
6 ms |
600 KB |
Output is correct |
39 |
Correct |
6 ms |
600 KB |
Output is correct |
40 |
Correct |
6 ms |
600 KB |
Output is correct |
41 |
Correct |
9 ms |
600 KB |
Output is correct |
42 |
Correct |
7 ms |
600 KB |
Output is correct |
43 |
Correct |
7 ms |
600 KB |
Output is correct |
44 |
Correct |
6 ms |
600 KB |
Output is correct |
45 |
Correct |
9 ms |
344 KB |
Output is correct |
46 |
Correct |
9 ms |
344 KB |
Output is correct |
47 |
Correct |
7 ms |
344 KB |
Output is correct |
48 |
Correct |
7 ms |
344 KB |
Output is correct |
49 |
Correct |
7 ms |
596 KB |
Output is correct |
50 |
Correct |
6 ms |
440 KB |
Output is correct |
51 |
Correct |
8 ms |
440 KB |
Output is correct |
52 |
Correct |
7 ms |
440 KB |
Output is correct |
53 |
Correct |
6 ms |
600 KB |
Output is correct |
54 |
Correct |
6 ms |
344 KB |
Output is correct |
55 |
Correct |
6 ms |
600 KB |
Output is correct |
56 |
Correct |
6 ms |
344 KB |
Output is correct |
57 |
Correct |
6 ms |
600 KB |
Output is correct |
58 |
Correct |
7 ms |
604 KB |
Output is correct |
59 |
Correct |
7 ms |
604 KB |
Output is correct |
60 |
Correct |
6 ms |
604 KB |
Output is correct |
61 |
Correct |
8 ms |
604 KB |
Output is correct |
62 |
Correct |
7 ms |
448 KB |
Output is correct |
63 |
Correct |
7 ms |
604 KB |
Output is correct |
64 |
Correct |
6 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
600 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
600 KB |
Output is correct |
10 |
Correct |
4 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
600 KB |
Output is correct |
12 |
Correct |
4 ms |
600 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
11 ms |
344 KB |
Output is correct |
16 |
Correct |
6 ms |
344 KB |
Output is correct |
17 |
Correct |
5 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 |
7 ms |
596 KB |
Output is correct |
22 |
Correct |
6 ms |
344 KB |
Output is correct |
23 |
Correct |
8 ms |
344 KB |
Output is correct |
24 |
Correct |
9 ms |
344 KB |
Output is correct |
25 |
Correct |
7 ms |
600 KB |
Output is correct |
26 |
Correct |
6 ms |
436 KB |
Output is correct |
27 |
Correct |
6 ms |
440 KB |
Output is correct |
28 |
Correct |
6 ms |
440 KB |
Output is correct |
29 |
Correct |
6 ms |
344 KB |
Output is correct |
30 |
Correct |
6 ms |
344 KB |
Output is correct |
31 |
Correct |
6 ms |
344 KB |
Output is correct |
32 |
Correct |
11 ms |
344 KB |
Output is correct |
33 |
Correct |
13 ms |
344 KB |
Output is correct |
34 |
Correct |
8 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 |
6 ms |
436 KB |
Output is correct |
38 |
Correct |
7 ms |
440 KB |
Output is correct |
39 |
Correct |
7 ms |
440 KB |
Output is correct |
40 |
Correct |
6 ms |
600 KB |
Output is correct |
41 |
Correct |
7 ms |
344 KB |
Output is correct |
42 |
Correct |
6 ms |
600 KB |
Output is correct |
43 |
Correct |
6 ms |
600 KB |
Output is correct |
44 |
Correct |
6 ms |
600 KB |
Output is correct |
45 |
Correct |
5 ms |
344 KB |
Output is correct |
46 |
Correct |
5 ms |
600 KB |
Output is correct |
47 |
Correct |
6 ms |
600 KB |
Output is correct |
48 |
Correct |
6 ms |
600 KB |
Output is correct |
49 |
Correct |
7 ms |
600 KB |
Output is correct |
50 |
Correct |
6 ms |
600 KB |
Output is correct |
51 |
Correct |
8 ms |
600 KB |
Output is correct |
52 |
Correct |
6 ms |
600 KB |
Output is correct |
53 |
Correct |
8 ms |
600 KB |
Output is correct |
54 |
Correct |
6 ms |
600 KB |
Output is correct |
55 |
Correct |
6 ms |
852 KB |
Output is correct |
56 |
Correct |
6 ms |
600 KB |
Output is correct |
57 |
Correct |
9 ms |
600 KB |
Output is correct |
58 |
Correct |
6 ms |
440 KB |
Output is correct |
59 |
Correct |
6 ms |
600 KB |
Output is correct |
60 |
Correct |
7 ms |
600 KB |
Output is correct |
61 |
Correct |
6 ms |
600 KB |
Output is correct |
62 |
Correct |
5 ms |
344 KB |
Output is correct |
63 |
Correct |
8 ms |
600 KB |
Output is correct |
64 |
Correct |
7 ms |
604 KB |
Output is correct |
65 |
Correct |
7 ms |
452 KB |
Output is correct |
66 |
Correct |
7 ms |
444 KB |
Output is correct |
67 |
Correct |
7 ms |
604 KB |
Output is correct |
68 |
Correct |
10 ms |
604 KB |
Output is correct |
69 |
Correct |
7 ms |
444 KB |
Output is correct |
70 |
Correct |
7 ms |
604 KB |
Output is correct |
71 |
Correct |
6 ms |
600 KB |
Output is correct |
72 |
Correct |
7 ms |
604 KB |
Output is correct |
73 |
Correct |
7 ms |
604 KB |
Output is correct |
74 |
Correct |
8 ms |
800 KB |
Output is correct |
75 |
Correct |
8 ms |
604 KB |
Output is correct |
76 |
Correct |
8 ms |
448 KB |
Output is correct |
77 |
Correct |
6 ms |
600 KB |
Output is correct |
78 |
Correct |
7 ms |
604 KB |
Output is correct |
79 |
Correct |
8 ms |
604 KB |
Output is correct |
80 |
Correct |
7 ms |
604 KB |
Output is correct |
81 |
Correct |
7 ms |
604 KB |
Output is correct |
82 |
Correct |
9 ms |
856 KB |
Output is correct |