#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define el '\n'
#define ff first
#define ss second
#define pii pair <ll, ll>
#define pb push_back
#define mkp make_pair
#define fr(i, l, r) for(ll i = l; i <= r; i++)
#define debug(x) \
{ cout << #x << " = " << x << el; }
template<class T, class S>
inline bool chmax(T &a, const S &b) {
return (a < b ? a = b, 1 : 0);
}
template<class T, class S>
inline bool chmin(T &a, const S &b) {
return (a > b ? a = b, 1 : 0);
}
//const ll N = 2e5 + 10;
const ll M = 1e5 + 10;
const ll K = 400;
const ll INF = 1e18 + 10;
const ll inf = 1e9 + 10;
const ll LOG = 20;
const ll mod = 1000002022;
mt19937 rnd(time(0));
bool are_connected(std::vector<int> A, std::vector<int> B);
std::vector<int> longest_trip(int N, int D) {
vector <int> f, s;
f.pb(0); s.pb(1);
vector <int> left;
fr(i, 2, N - 1)
left.pb(i);
shuffle(left.begin(), left.end(), rnd);
int ends = 1;
for(int i : left) {
if(ends == 1) {
bool fir = are_connected({f.back()}, {i});
bool sec = are_connected({s.back()}, {i});
if(fir) {
f.pb(i);
if(!sec) ends = 0;
else ends = 2;
}
else if(sec) {
s.pb(i);
ends = 0;
}
else {
while(!s.empty()) {
f.pb(s.back());
s.pop_back();
}
s.pb(i);
}
}
else if(ends == 0) {
bool fir = are_connected({f.back()}, {i});
ends = 1;
if(fir)
f.pb(i);
else {
s.pb(i);
ends = 0;
}
}
else {
while(!s.empty()) {
f.pb(s.back());
s.pop_back();
}
s.pb(i);
ends = 1;
}
}
if(!are_connected(f, s)) {
if(f.size() < s.size()) swap(f, s);
return f;
}
vector <int> ans;
if(f.size() == 1 && are_connected({f[0]}, {s[0], s.back()})) {
if(are_connected({f[0]}, {s[0]})) {
ans = f;
for(int j : s)
ans.pb(j);
}
else {
ans = s;
for(int j : f)
ans.pb(j);
}
return ans;
}
else if(s.size() == 1 && are_connected({s[0]}, {f[0], f.back()})) {
if(are_connected({f[0]}, {s[0]})) {
ans = s;
for(int j : f)
ans.pb(j);
}
else {
ans = f;
for(int j : s)
ans.pb(j);
}
return ans;
}
else if(f.size() > 1 && s.size() > 1 && are_connected({f[0], f.back()}, {s[0], s.back()})) {
if(are_connected({f[0]}, {s[0]})) {
reverse(f.begin(), f.end());
ans = f;
for(int j : s)
ans.pb(j);
}
else if(are_connected({f[0]}, {s.back()})) {
ans = s;
for(int j : f)
ans.pb(j);
}
else if(are_connected({f.back()}, {s[0]})) {
ans = f;
for(int j : s)
ans.pb(j);
}
else {
ans = s;
reverse(f.begin(), f.end());
for(int j : f)
ans.pb(j);
}
return ans;
}
int l = 0, r = f.size() - 1;
while(l < r) {
int mid = (l + r) / 2;
vector <int> v;
for(int j = 0; j <= mid; j++)
v.pb(f[j]);
if(are_connected(v, s)) r = mid;
else l = mid + 1;
}
int l2 = 0, r2 = s.size() - 1;
while(l2 < r2) {
int mid = (l2 + r2) / 2;
vector <int> v, v2;
for(int j = 0; j <= l; j++)
v.pb(f[j]);
for(int j = 0; j <= mid; j++)
v2.pb(s[j]);
if(are_connected(v, v2)) r2 = mid;
else l2 = mid + 1;
}
for(int i = l + 1; i < f.size(); i++)
ans.pb(f[i]);
for(int i = 0; i <= l; i++)
ans.pb(f[i]);
for(int i = l2; i < s.size(); i++)
ans.pb(s[i]);
for(int i = 0; i < l2; i++)
ans.pb(s[i]);
return ans;
}
/*
static inline constexpr int maxNumberOfCalls = 32640;
static inline constexpr int maxTotalNumberOfCalls = 150000;
static inline constexpr int maxTotalNumberOfLandmarksInCalls = 1500000;
static int call_counter = 0;
static int total_call_counter = 0;
static int landmark_counter = 0;
static int C, N, D;
static std::vector<std::vector<int>> U;
static std::vector<bool> present;
static inline void protocol_violation(std::string message)
{
printf("Protocol Violation: %s\n", message.c_str());
exit(0);
}
bool are_connected(std::vector<int> A, std::vector<int> B)
{
++call_counter;
++total_call_counter;
if (call_counter > maxNumberOfCalls || total_call_counter > maxTotalNumberOfCalls)
{
protocol_violation("too many calls");
}
int nA = A.size(), nB = B.size();
landmark_counter += nA + nB;
if (landmark_counter > maxTotalNumberOfLandmarksInCalls)
{
protocol_violation("too many elements");
}
if (nA == 0 || nB == 0)
{
protocol_violation("invalid array");
}
for (int i = 0; i < nA; ++i)
{
if (A[i] < 0 || N <= A[i])
{
protocol_violation("invalid array");
}
if (present[A[i]])
{
protocol_violation("invalid array");
}
present[A[i]] = true;
}
for (int i = 0; i < nA; ++i)
{
present[A[i]] = false;
}
for (int i = 0; i < nB; ++i)
{
if (B[i] < 0 || N <= B[i])
{
protocol_violation("invalid array");
}
if (present[B[i]])
{
protocol_violation("invalid array");
}
present[B[i]] = true;
}
for (int i = 0; i < nB; ++i)
{
present[B[i]] = false;
}
for (int i = 0; i < nA; ++i)
{
for (int j = 0; j < nB; ++j)
{
if (A[i] == B[j])
{
protocol_violation("non-disjoint arrays");
}
}
}
for (int i = 0; i < nA; ++i)
{
for (int j = 0; j < nB; ++j)
{
if (U[std::max(A[i], B[j])][std::min(A[i], B[j])] == 1)
{
return true;
}
}
}
return false;
}
int main()
{
assert(1 == scanf("%d", &C));
int maximumCalls = 0;
for (int c = 0; c < C; ++c)
{
call_counter = 0;
assert(2 == scanf("%d %d", &N, &D));
present.assign(N, false);
U.resize(N);
for (int i = 1; i < N; ++i)
{
U[i].resize(i);
for (int j = 0; j < i; ++j)
{
assert(1 == scanf("%d", &U[i][j]));
}
}
for (int i = 2; i < N; ++i)
{
for (int j = 1; j < i; ++j)
{
for (int k = 0; k < j; ++k)
{
if (U[i][j] + U[i][k] + U[j][k] < D)
{
printf("Insufficient Density\n");
exit(0);
}
}
}
}
std::vector<int> t = longest_trip(N, D);
int l = t.size();
//printf("%d\n", l);
bool ok = 1;
for (int i = 0; i < l; ++i)
{
if(i) {
int a = t[i - 1], b = t[i];
if(a > b) swap(a, b);
if(!U[b][a]) ok = 0;
}
//printf(i == 0 ? "%d" : " %d", t[i]);
}
//printf("\n");
if(!ok) cout << "INVALID" << el;
//printf("%d\n", call_counter);
maximumCalls = std::max(maximumCalls, call_counter);
call_counter = 0;
}
printf("%d\n", maximumCalls);
return 0;
}
*/
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:169:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
169 | for(int i = l + 1; i < f.size(); i++)
| ~~^~~~~~~~~~
longesttrip.cpp:173:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
173 | for(int i = l2; i < s.size(); i++)
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
344 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 |
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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 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 |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
11 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
8 ms |
344 KB |
Output is correct |
10 |
Correct |
5 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 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 |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
10 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 |
6 ms |
344 KB |
Output is correct |
10 |
Correct |
6 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 |
6 ms |
344 KB |
Output is correct |
15 |
Correct |
7 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 |
9 ms |
344 KB |
Output is correct |
19 |
Correct |
8 ms |
344 KB |
Output is correct |
20 |
Correct |
4 ms |
344 KB |
Output is correct |
21 |
Correct |
6 ms |
344 KB |
Output is correct |
22 |
Correct |
5 ms |
344 KB |
Output is correct |
23 |
Correct |
6 ms |
448 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 |
9 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 |
4 ms |
344 KB |
Output is correct |
32 |
Correct |
6 ms |
344 KB |
Output is correct |
33 |
Correct |
5 ms |
344 KB |
Output is correct |
34 |
Correct |
7 ms |
344 KB |
Output is correct |
35 |
Correct |
5 ms |
344 KB |
Output is correct |
36 |
Correct |
6 ms |
596 KB |
Output is correct |
37 |
Correct |
8 ms |
344 KB |
Output is correct |
38 |
Correct |
7 ms |
340 KB |
Output is correct |
39 |
Correct |
7 ms |
344 KB |
Output is correct |
40 |
Correct |
8 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 |
6 ms |
344 KB |
Output is correct |
44 |
Correct |
5 ms |
344 KB |
Output is correct |
45 |
Correct |
11 ms |
344 KB |
Output is correct |
46 |
Correct |
9 ms |
344 KB |
Output is correct |
47 |
Correct |
8 ms |
340 KB |
Output is correct |
48 |
Correct |
8 ms |
344 KB |
Output is correct |
49 |
Correct |
7 ms |
344 KB |
Output is correct |
50 |
Correct |
9 ms |
440 KB |
Output is correct |
51 |
Correct |
7 ms |
344 KB |
Output is correct |
52 |
Correct |
7 ms |
344 KB |
Output is correct |
53 |
Correct |
6 ms |
444 KB |
Output is correct |
54 |
Correct |
5 ms |
344 KB |
Output is correct |
55 |
Correct |
7 ms |
444 KB |
Output is correct |
56 |
Correct |
6 ms |
344 KB |
Output is correct |
57 |
Correct |
10 ms |
444 KB |
Output is correct |
58 |
Correct |
7 ms |
344 KB |
Output is correct |
59 |
Correct |
11 ms |
344 KB |
Output is correct |
60 |
Correct |
7 ms |
344 KB |
Output is correct |
61 |
Correct |
7 ms |
600 KB |
Output is correct |
62 |
Correct |
8 ms |
344 KB |
Output is correct |
63 |
Correct |
6 ms |
344 KB |
Output is correct |
64 |
Correct |
8 ms |
448 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 |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
344 KB |
Output is correct |
6 |
Correct |
10 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 |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
344 KB |
Output is correct |
11 |
Correct |
7 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 |
8 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
344 KB |
Output is correct |
16 |
Correct |
9 ms |
344 KB |
Output is correct |
17 |
Correct |
7 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 |
440 KB |
Output is correct |
21 |
Correct |
6 ms |
344 KB |
Output is correct |
22 |
Correct |
7 ms |
344 KB |
Output is correct |
23 |
Correct |
6 ms |
344 KB |
Output is correct |
24 |
Correct |
6 ms |
344 KB |
Output is correct |
25 |
Correct |
6 ms |
344 KB |
Output is correct |
26 |
Correct |
5 ms |
344 KB |
Output is correct |
27 |
Correct |
9 ms |
344 KB |
Output is correct |
28 |
Correct |
7 ms |
344 KB |
Output is correct |
29 |
Correct |
6 ms |
344 KB |
Output is correct |
30 |
Correct |
7 ms |
344 KB |
Output is correct |
31 |
Correct |
7 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 |
5 ms |
344 KB |
Output is correct |
35 |
Correct |
6 ms |
344 KB |
Output is correct |
36 |
Correct |
8 ms |
344 KB |
Output is correct |
37 |
Correct |
7 ms |
344 KB |
Output is correct |
38 |
Correct |
7 ms |
344 KB |
Output is correct |
39 |
Correct |
9 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 |
5 ms |
344 KB |
Output is correct |
43 |
Correct |
6 ms |
444 KB |
Output is correct |
44 |
Correct |
5 ms |
344 KB |
Output is correct |
45 |
Correct |
6 ms |
448 KB |
Output is correct |
46 |
Correct |
6 ms |
344 KB |
Output is correct |
47 |
Correct |
5 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 |
9 ms |
344 KB |
Output is correct |
51 |
Correct |
7 ms |
344 KB |
Output is correct |
52 |
Correct |
7 ms |
344 KB |
Output is correct |
53 |
Correct |
6 ms |
344 KB |
Output is correct |
54 |
Correct |
7 ms |
344 KB |
Output is correct |
55 |
Correct |
7 ms |
344 KB |
Output is correct |
56 |
Correct |
6 ms |
344 KB |
Output is correct |
57 |
Correct |
5 ms |
344 KB |
Output is correct |
58 |
Correct |
6 ms |
344 KB |
Output is correct |
59 |
Correct |
7 ms |
344 KB |
Output is correct |
60 |
Correct |
7 ms |
344 KB |
Output is correct |
61 |
Correct |
7 ms |
344 KB |
Output is correct |
62 |
Correct |
4 ms |
344 KB |
Output is correct |
63 |
Correct |
8 ms |
344 KB |
Output is correct |
64 |
Correct |
7 ms |
456 KB |
Output is correct |
65 |
Correct |
8 ms |
600 KB |
Output is correct |
66 |
Correct |
9 ms |
344 KB |
Output is correct |
67 |
Correct |
6 ms |
344 KB |
Output is correct |
68 |
Correct |
6 ms |
344 KB |
Output is correct |
69 |
Correct |
6 ms |
344 KB |
Output is correct |
70 |
Correct |
7 ms |
344 KB |
Output is correct |
71 |
Correct |
5 ms |
344 KB |
Output is correct |
72 |
Correct |
11 ms |
444 KB |
Output is correct |
73 |
Correct |
10 ms |
344 KB |
Output is correct |
74 |
Correct |
11 ms |
344 KB |
Output is correct |
75 |
Correct |
7 ms |
448 KB |
Output is correct |
76 |
Correct |
7 ms |
444 KB |
Output is correct |
77 |
Correct |
8 ms |
344 KB |
Output is correct |
78 |
Correct |
8 ms |
344 KB |
Output is correct |
79 |
Correct |
8 ms |
344 KB |
Output is correct |
80 |
Correct |
7 ms |
452 KB |
Output is correct |
81 |
Correct |
7 ms |
344 KB |
Output is correct |
82 |
Correct |
6 ms |
452 KB |
Output is correct |