#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(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);
}
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]}))
{
for(auto x:dq2)
dq1.push_front(x);
answer();
return v;
}
else if(are_connected({dq1[0]},{dq2[dq2.size()-1]}))
{
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]}))
{
for(auto x:dq2)
dq1.push_back(x);
answer();
return v;
}
else if(are_connected({dq1[dq1.size()-1]},{dq2[dq2.size()-1]}))
{
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:97:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
97 | if(dq1.size()<dq2.size())
| ^~
longesttrip.cpp:99:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
99 | if(!are_connected(vectorize(dq1),vectorize(dq2)))
| ^~
longesttrip.cpp:122:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
122 | for(i=0;i<dq1.size();i++)
| ~^~~~~~~~~~~
longesttrip.cpp:129:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
129 | for(i=0;i<dq1.size();i++)
| ~^~~~~~~~~~~
longesttrip.cpp:138:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
138 | for(i=0;i<dq1.size();i++)
| ~^~~~~~~~~~~
longesttrip.cpp:189:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
189 | for(i=0;i<dq1.size();i++)
| ~^~~~~~~~~~~
longesttrip.cpp:198:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
198 | for(i=0;i<dq1.size();i++)
| ~^~~~~~~~~~~
longesttrip.cpp:205:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
205 | for(i=0;i<dq2.size();i++)
| ~^~~~~~~~~~~
longesttrip.cpp:214:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
214 | for(i=0;i<dq2.size();i++)
| ~^~~~~~~~~~~
longesttrip.cpp:216:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
216 | if(dq2[i]==y)
| ^~
longesttrip.cpp:218:17: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
218 | if(t==1)
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 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 |
5 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
7 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 |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
7 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 |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
600 KB |
Output is correct |
12 |
Correct |
5 ms |
344 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 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 |
5 ms |
600 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
14 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
600 KB |
Output is correct |
10 |
Correct |
5 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 |
600 KB |
Output is correct |
14 |
Correct |
6 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 |
5 ms |
344 KB |
Output is correct |
20 |
Correct |
5 ms |
544 KB |
Output is correct |
21 |
Correct |
7 ms |
696 KB |
Output is correct |
22 |
Correct |
5 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 |
9 ms |
344 KB |
Output is correct |
26 |
Correct |
8 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
344 KB |
Output is correct |
28 |
Correct |
10 ms |
344 KB |
Output is correct |
29 |
Correct |
7 ms |
344 KB |
Output is correct |
30 |
Correct |
6 ms |
440 KB |
Output is correct |
31 |
Correct |
7 ms |
440 KB |
Output is correct |
32 |
Correct |
8 ms |
436 KB |
Output is correct |
33 |
Correct |
8 ms |
344 KB |
Output is correct |
34 |
Correct |
7 ms |
344 KB |
Output is correct |
35 |
Correct |
7 ms |
496 KB |
Output is correct |
36 |
Correct |
8 ms |
856 KB |
Output is correct |
37 |
Correct |
9 ms |
600 KB |
Output is correct |
38 |
Correct |
8 ms |
600 KB |
Output is correct |
39 |
Correct |
11 ms |
600 KB |
Output is correct |
40 |
Correct |
8 ms |
600 KB |
Output is correct |
41 |
Correct |
11 ms |
448 KB |
Output is correct |
42 |
Correct |
10 ms |
600 KB |
Output is correct |
43 |
Correct |
9 ms |
600 KB |
Output is correct |
44 |
Correct |
8 ms |
600 KB |
Output is correct |
45 |
Correct |
10 ms |
344 KB |
Output is correct |
46 |
Correct |
10 ms |
344 KB |
Output is correct |
47 |
Correct |
7 ms |
344 KB |
Output is correct |
48 |
Correct |
9 ms |
344 KB |
Output is correct |
49 |
Correct |
12 ms |
344 KB |
Output is correct |
50 |
Correct |
7 ms |
436 KB |
Output is correct |
51 |
Correct |
9 ms |
444 KB |
Output is correct |
52 |
Correct |
9 ms |
440 KB |
Output is correct |
53 |
Correct |
8 ms |
600 KB |
Output is correct |
54 |
Correct |
9 ms |
600 KB |
Output is correct |
55 |
Correct |
11 ms |
344 KB |
Output is correct |
56 |
Correct |
6 ms |
344 KB |
Output is correct |
57 |
Correct |
10 ms |
856 KB |
Output is correct |
58 |
Correct |
10 ms |
600 KB |
Output is correct |
59 |
Correct |
10 ms |
452 KB |
Output is correct |
60 |
Correct |
10 ms |
604 KB |
Output is correct |
61 |
Correct |
9 ms |
604 KB |
Output is correct |
62 |
Correct |
8 ms |
444 KB |
Output is correct |
63 |
Correct |
9 ms |
860 KB |
Output is correct |
64 |
Correct |
13 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 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 |
8 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 |
5 ms |
600 KB |
Output is correct |
10 |
Correct |
4 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
14 |
Correct |
5 ms |
344 KB |
Output is correct |
15 |
Correct |
8 ms |
344 KB |
Output is correct |
16 |
Correct |
10 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 |
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 |
9 ms |
344 KB |
Output is correct |
23 |
Correct |
6 ms |
344 KB |
Output is correct |
24 |
Correct |
7 ms |
344 KB |
Output is correct |
25 |
Correct |
9 ms |
344 KB |
Output is correct |
26 |
Correct |
6 ms |
440 KB |
Output is correct |
27 |
Correct |
10 ms |
440 KB |
Output is correct |
28 |
Correct |
8 ms |
440 KB |
Output is correct |
29 |
Correct |
8 ms |
344 KB |
Output is correct |
30 |
Correct |
6 ms |
344 KB |
Output is correct |
31 |
Correct |
8 ms |
344 KB |
Output is correct |
32 |
Correct |
8 ms |
344 KB |
Output is correct |
33 |
Correct |
9 ms |
344 KB |
Output is correct |
34 |
Correct |
11 ms |
344 KB |
Output is correct |
35 |
Correct |
9 ms |
344 KB |
Output is correct |
36 |
Correct |
9 ms |
344 KB |
Output is correct |
37 |
Correct |
6 ms |
440 KB |
Output is correct |
38 |
Correct |
10 ms |
440 KB |
Output is correct |
39 |
Correct |
9 ms |
436 KB |
Output is correct |
40 |
Correct |
6 ms |
600 KB |
Output is correct |
41 |
Correct |
9 ms |
600 KB |
Output is correct |
42 |
Correct |
8 ms |
344 KB |
Output is correct |
43 |
Correct |
6 ms |
344 KB |
Output is correct |
44 |
Correct |
5 ms |
344 KB |
Output is correct |
45 |
Correct |
6 ms |
344 KB |
Output is correct |
46 |
Correct |
5 ms |
344 KB |
Output is correct |
47 |
Partially correct |
18 ms |
600 KB |
Output is partially correct |
48 |
Partially correct |
10 ms |
448 KB |
Output is partially correct |
49 |
Partially correct |
10 ms |
796 KB |
Output is partially correct |
50 |
Partially correct |
12 ms |
600 KB |
Output is partially correct |
51 |
Partially correct |
9 ms |
600 KB |
Output is partially correct |
52 |
Partially correct |
9 ms |
600 KB |
Output is partially correct |
53 |
Partially correct |
9 ms |
600 KB |
Output is partially correct |
54 |
Partially correct |
9 ms |
600 KB |
Output is partially correct |
55 |
Partially correct |
9 ms |
344 KB |
Output is partially correct |
56 |
Partially correct |
8 ms |
600 KB |
Output is partially correct |
57 |
Partially correct |
10 ms |
600 KB |
Output is partially correct |
58 |
Partially correct |
12 ms |
600 KB |
Output is partially correct |
59 |
Partially correct |
9 ms |
600 KB |
Output is partially correct |
60 |
Partially correct |
11 ms |
600 KB |
Output is partially correct |
61 |
Partially correct |
10 ms |
440 KB |
Output is partially correct |
62 |
Correct |
6 ms |
344 KB |
Output is correct |
63 |
Partially correct |
9 ms |
600 KB |
Output is partially correct |
64 |
Partially correct |
10 ms |
448 KB |
Output is partially correct |
65 |
Partially correct |
8 ms |
604 KB |
Output is partially correct |
66 |
Partially correct |
10 ms |
604 KB |
Output is partially correct |
67 |
Partially correct |
9 ms |
496 KB |
Output is partially correct |
68 |
Partially correct |
10 ms |
604 KB |
Output is partially correct |
69 |
Partially correct |
9 ms |
604 KB |
Output is partially correct |
70 |
Partially correct |
8 ms |
604 KB |
Output is partially correct |
71 |
Partially correct |
12 ms |
600 KB |
Output is partially correct |
72 |
Partially correct |
11 ms |
448 KB |
Output is partially correct |
73 |
Partially correct |
11 ms |
604 KB |
Output is partially correct |
74 |
Partially correct |
9 ms |
604 KB |
Output is partially correct |
75 |
Partially correct |
9 ms |
604 KB |
Output is partially correct |
76 |
Partially correct |
10 ms |
604 KB |
Output is partially correct |
77 |
Partially correct |
10 ms |
600 KB |
Output is partially correct |
78 |
Partially correct |
8 ms |
604 KB |
Output is partially correct |
79 |
Partially correct |
10 ms |
604 KB |
Output is partially correct |
80 |
Partially correct |
8 ms |
604 KB |
Output is partially correct |
81 |
Partially correct |
10 ms |
600 KB |
Output is partially correct |
82 |
Partially correct |
10 ms |
604 KB |
Output is partially correct |