#include<bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define endl '\n'
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
const int mod = 1e9 + 7;
const int N = 1e6 + 15;
const ll inf = 1e18;
vector<int> longest_trip(int n, int D){
vector<int> grp[2];
grp[0] = {0};
grp[1] = {1};
vector<int> num(n);
for (int i=0;i<n;i++)num[i] = i;
// shuffle ?
for (int i=2;i<n;i++){
int u = num[i];
if (i==n-1){
if (are_connected({grp[0].back()}, {u})) grp[0].pb(u);
else if (are_connected({grp[1].back()}, {u})) grp[1].pb(u);
else {
reverse(all(grp[1]));
grp[0].insert(grp[0].end(), grp[1].begin(), grp[1].end());
grp[1] = {u};
}
break;
}
int v = num[i+1];
if (are_connected({grp[0].back()}, {u})){
grp[0].pb(u);
continue;
}
if (are_connected({grp[1].back()}, {u})){
// what ever
grp[1].pb(u);
are_connected({u}, {v}) ? grp[1].pb(v) : grp[0].pb(v);
i++;
continue;
}
if (are_connected({u}, {v})){
// mrg grp0 and grp1
reverse(all(grp[1]));
grp[0].insert(grp[0].end(), grp[1].begin(), grp[1].end());
grp[1] = {u, v};
}
grp[1].pb(v);
reverse(all(grp[1]));
grp[0].insert(grp[0].end(), grp[1].begin(), grp[1].end());
grp[1] = {u};
}
bool done = 0;
if (are_connected({grp[0].back()}, {grp[1].back()})){
reverse(all(grp[1]));
grp[0].insert(grp[0].end(), grp[1].begin(), grp[1].end());
done = 1;
}
if (!done && are_connected({grp[0].front()}, {grp[1].back()})){
grp[1].insert(grp[1].end(), grp[0].begin(), grp[0].end());
swap(grp[0], grp[1]);
done = 1;
}
if (!done && sz(grp[1]) > 1 && are_connected({grp[0].back()}, {grp[1].front()})){
grp[0].insert(grp[0].end(), grp[1].begin(), grp[1].end());
done = 1;
}
if (!done && sz(grp[1]) > 1 && are_connected({grp[0].front()}, {grp[1].front()})){
reverse(all(grp[0]));
grp[0].insert(grp[0].end(), grp[1].begin(), grp[1].end());
done = 1;
}
if (done) return grp[0];
if (!are_connected(grp[0], grp[1])) return (sz(grp[0]) >= sz(grp[1]) ? grp[0] : grp[1]);
// binary search
int lo=0, hi = sz(grp[0]);
while (hi-lo>1){
int mi = (lo+hi)/2;
vector<int> tm;
for (int i=lo;i<mi;i++){
tm.pb(grp[0][i]);
}
if (are_connected(tm, grp[1])){
lo = mi;
}
else {
hi = mi;
}
}
int ans1 = lo;
lo=0, hi = sz(grp[1]);
while (hi-lo>1){
int mi = (lo+hi)/2;
vector<int> tm;
for (int i=lo;i<mi;i++){
tm.pb(grp[1][i]);
}
if (are_connected(tm, {ans1})){
lo = mi;
}
else {
hi = mi;
}
}
int ans2=lo;
vector<int> ans;
for (int i=0;i<sz(grp[0]);i++){
int p = (ans1+i+1)%sz(grp[0]);
ans.pb(grp[0][p]);
}
for (int i=0;i<sz(grp[1]);i++){
int p = (ans2+i)%sz(grp[1]);
ans.pb(grp[1][p]);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
600 KB |
Output is correct |
5 |
Correct |
4 ms |
344 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 |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
8 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 |
600 KB |
Output is correct |
10 |
Correct |
4 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 |
7 ms |
344 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 |
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 |
6 |
Correct |
9 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 |
436 KB |
Output is correct |
10 |
Correct |
5 ms |
600 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
6 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
14 ms |
344 KB |
Output is correct |
15 |
Incorrect |
1 ms |
344 KB |
invalid array |
16 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
600 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 |
600 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 |
14 |
Correct |
9 ms |
344 KB |
Output is correct |
15 |
Incorrect |
0 ms |
344 KB |
invalid array |
16 |
Halted |
0 ms |
0 KB |
- |