This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// CF template, version 3.0
#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
#define improvePerformance ios_base::sync_with_stdio(false); cin.tie(0)
#define getTest int t; cin >> t
#define eachTest for (int _var=0;_var<t;_var++)
#define get(name) int (name); cin >> (name)
#define out(o) cout << (o)
#define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
#define sortl(name) sort((name).begin(), (name).end())
#define rev(name) reverse((name).begin(), (name).end())
#define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
#define decision(b) if (b){out("YES");}else{out("NO");}
//#define int long long int
template <typename T, typename I>
struct segtree {
int n;
vector<T> tree;
vector<I> initial;
T id;
segtree(int i_n, vector<I> i_initial, T i_id): n(i_n), initial(i_initial), id(i_id) {
tree.resize(4 * n);
}
T conquer(T left, T right) {
// write your conquer function
}
T value(I inp) {
// write your value function
}
void build(int v, int l, int r) {
if (l == r) tree[v] = value(initial[l]);
else {
int middle = (l + r) / 2;
build(2 * v, l, middle);
build(2 * v + 1, middle + 1, r);
tree[v] = conquer(tree[2 * v], tree[2 * v + 1]);
}
}
void upd(int v, int l, int r, int i, I x) {
if (l == r) tree[v] = value(x);
else {
int middle = (l + r) / 2;
if (middle >= i) upd(2 * v, l, middle, i, x);
else upd(2 * v + 1, middle + 1, r, i, x);
tree[v] = conquer(tree[2 * v], tree[2 * v + 1]);
}
}
T query(int v, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) return tree[v];
else if (r < ql || qr < l) return id;
int middle = (l + r) / 2;
T left = query(2 * v, l, middle, ql, qr);
T right = query(2 * v + 1, middle + 1, r, ql, qr);
return conquer(left, right);
}
};
// vector<int>
// Find the first node in Y (if any) connected to at least one node in X.
int bin_search(vector<int> A, vector<int> Y) {
int M = Y.size();
int l = 0;
int r = M;
while (r - l >= 1) {
int middle = (l + r) / 2;
vector<int> B;
forto(middle + 1, i) B.push_back(Y[i]);
if (are_connected(A, B)) r = middle;
else l = middle + 1;
}
if (l == M) return -1;
return Y[l];
}
vector<int> longest_trip(int N, int _D) {
vector<int> path;
vector<bool> other(N, true); // think of this as "free".
vector<int> A1;
A1.push_back(0);
vector<int> B1;
forto(N, i) if (i) B1.push_back(i);
int initial = bin_search(A1, B1);
if (initial == -1) {
return B1;
}
path.push_back(0);
path.push_back(initial);
other[0] = other[initial] = false;
while (path.size() < N) {
vector<int> A2;
vector<int> B2;
A2.push_back(path[0]);
B2.push_back(path.back());
if (are_connected(A2, B2)) {
vector<int> remain;
forto(N, i) if (other[i]) remain.push_back(i);
int first = bin_search(B2, remain);
if (first == -1) {
int firsthere = bin_search(remain, path);
if (firsthere == -1) {
if (path.size() >= (N + 1) / 2) return path;
return remain;
}
vector<int> A3;
A3.push_back(firsthere);
int firstout = bin_search(A3, remain);
vector<int> newpath;
for (int r: remain) if (r != firstout) newpath.push_back(r);
newpath.push_back(firstout);
int ind;
int s = path.size();
forto(s, i) if (path[i] == firsthere) ind = i;
for (int i = ind; i < s; i++) newpath.push_back(path[i]);
forto(ind, i) newpath.push_back(path[i]);
return newpath;
} else {
path.push_back(first);
other[first] = false;
}
} else {
int last;
forto(N, i) if (other[i]) last = i;
vector<int> A3;
vector<int> B3;
A3.push_back(path[0]);
B3.push_back(last);
if (are_connected(A3, B3)) {
rev(path);
path.push_back(last);
} else {
path.push_back(last);
}
other[last] = false;
}
}
return path;
}
Compilation message (stderr)
longesttrip.cpp: In function 'int bin_search(std::vector<int>, std::vector<int>)':
longesttrip.cpp:16:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
16 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
longesttrip.cpp:80:9: note: in expansion of macro 'forto'
80 | forto(middle + 1, i) B.push_back(Y[i]);
| ^~~~~
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:16:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
16 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
longesttrip.cpp:94:5: note: in expansion of macro 'forto'
94 | forto(N, i) if (i) B1.push_back(i);
| ^~~~~
longesttrip.cpp:102:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
102 | while (path.size() < N) {
| ~~~~~~~~~~~~^~~
longesttrip.cpp:16:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
16 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
longesttrip.cpp:109:13: note: in expansion of macro 'forto'
109 | forto(N, i) if (other[i]) remain.push_back(i);
| ^~~~~
longesttrip.cpp:114:37: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
114 | if (path.size() >= (N + 1) / 2) return path;
| ~~~~~~~~~~~~^~~~~~~~~~~~~~
longesttrip.cpp:16:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
16 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
longesttrip.cpp:125:17: note: in expansion of macro 'forto'
125 | forto(s, i) if (path[i] == firsthere) ind = i;
| ^~~~~
longesttrip.cpp:16:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
16 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
longesttrip.cpp:127:17: note: in expansion of macro 'forto'
127 | forto(ind, i) newpath.push_back(path[i]);
| ^~~~~
longesttrip.cpp:16:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
16 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
longesttrip.cpp:135:13: note: in expansion of macro 'forto'
135 | forto(N, i) if (other[i]) last = i;
| ^~~~~
longesttrip.cpp:123:21: warning: 'ind' may be used uninitialized in this function [-Wmaybe-uninitialized]
123 | int ind;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |