Submission #1056385

# Submission time Handle Problem Language Result Execution time Memory
1056385 2024-08-13T09:08:50 Z jcelin Longest Trip (IOI23_longesttrip) C++17
100 / 100
11 ms 600 KB
#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;

typedef long long ll;

typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<pll> vpll;

#define PB push_back
#define PF push_front
#define PPB pop_back
#define PPF pop_front
#define X first
#define Y second
#define MP make_pair
#define all(x) (x).begin(), (x).end()

const int mod = 1e9 + 7; //998244353;
const int inf = 1e9 + 7;
const ll INF = 1e18 + 7;
const int logo = 20;
const int MAXN = 1e6 + 7;
const int off = 1 << logo;
const int trsz = off << 1;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};


void nadi(vi &a, vi b){
	int lo = 0, hi = (int)a.size() - 1, ret = -1;
	while(lo <= hi){
		int mid = (lo + hi) / 2;
		vi vec;
		for(int i=0; i<=mid; i++) vec.PB(a[i]);
		if(are_connected(vec, b)) ret = mid, hi = mid - 1;
		else lo = mid + 1;
	}
	
	int vl = a[ret];
	while(a.back() != vl){
		int x = a.back();
		a.PPB();
		reverse(all(a));
		a.PB(x);
		reverse(all(a));
	}
}

vi longest_trip(int n, int d){
	vi a = {0}, b = {1};
	
	vi arr;
	for(int i=2; i<n; i++) arr.PB(i);
	shuffle(all(arr), mt19937(time(0)));
	bool nula = 0;
	for(int j=2; j<n; j++){
		int i = arr[j - 2];
		if(are_connected({a.back()}, {i})){
			a.PB(i);
			nula = 0;
			continue;
		}
		
		if(nula or are_connected({b.back()}, {i})){
			b.PB(i);
			nula = 1;
			continue;
		}
		
		while(b.size()) a.PB(b.back()), b.PPB();
		b.PB(i);
	}
	if(a.size() < b.size()) swap(a, b);
	if(!are_connected(a, b)) return a;
	
	if(are_connected({a.front()}, {b.front()})){
		reverse(all(a));
		reverse(all(b));
		while(b.size()) a.PB(b.back()), b.PPB();
		return a;
	}
	
	if(are_connected({a.front()}, {b.back()})){
		reverse(all(a));
		while(b.size()) a.PB(b.back()), b.PPB();
		return a;
	}
	
	if(are_connected({a.back()}, {b.back()})){
		while(b.size()) a.PB(b.back()), b.PPB();
		return a;
	}
	
	
	nadi(b, a);
	nadi(a, {b.back()});
	while(b.size()) a.PB(b.back()), b.PPB();
	return a;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 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 4 ms 344 KB Output is correct
# 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 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 10 ms 344 KB Output is correct
7 Correct 7 ms 344 KB Output is correct
8 Correct 4 ms 344 KB Output is correct
9 Correct 4 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 6 ms 344 KB Output is correct
13 Correct 4 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 6 ms 344 KB Output is correct
5 Correct 4 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 4 ms 340 KB Output is correct
10 Correct 4 ms 344 KB Output is correct
11 Correct 4 ms 344 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 4 ms 344 KB Output is correct
14 Correct 6 ms 344 KB Output is correct
15 Correct 6 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 428 KB Output is correct
20 Correct 6 ms 344 KB Output is correct
21 Correct 5 ms 344 KB Output is correct
22 Correct 5 ms 444 KB Output is correct
23 Correct 3 ms 344 KB Output is correct
24 Correct 5 ms 344 KB Output is correct
25 Correct 5 ms 344 KB Output is correct
26 Correct 8 ms 344 KB Output is correct
27 Correct 5 ms 344 KB Output is correct
28 Correct 5 ms 344 KB Output is correct
29 Correct 5 ms 344 KB Output is correct
30 Correct 6 ms 344 KB Output is correct
31 Correct 5 ms 344 KB Output is correct
32 Correct 5 ms 344 KB Output is correct
33 Correct 5 ms 344 KB Output is correct
34 Correct 4 ms 344 KB Output is correct
35 Correct 6 ms 344 KB Output is correct
36 Correct 4 ms 344 KB Output is correct
37 Correct 6 ms 344 KB Output is correct
38 Correct 8 ms 344 KB Output is correct
39 Correct 8 ms 344 KB Output is correct
40 Correct 7 ms 344 KB Output is correct
41 Correct 5 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 7 ms 344 KB Output is correct
46 Correct 8 ms 344 KB Output is correct
47 Correct 6 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 7 ms 344 KB Output is correct
51 Correct 6 ms 344 KB Output is correct
52 Correct 5 ms 344 KB Output is correct
53 Correct 5 ms 344 KB Output is correct
54 Correct 5 ms 440 KB Output is correct
55 Correct 6 ms 344 KB Output is correct
56 Correct 5 ms 444 KB Output is correct
57 Correct 5 ms 344 KB Output is correct
58 Correct 6 ms 344 KB Output is correct
59 Correct 6 ms 452 KB Output is correct
60 Correct 6 ms 344 KB Output is correct
61 Correct 5 ms 344 KB Output is correct
62 Correct 6 ms 344 KB Output is correct
63 Correct 6 ms 424 KB Output is correct
64 Correct 7 ms 436 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 4 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 3 ms 344 KB Output is correct
9 Correct 6 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 5 ms 340 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 5 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 5 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 5 ms 344 KB Output is correct
24 Correct 5 ms 344 KB Output is correct
25 Correct 8 ms 344 KB Output is correct
26 Correct 4 ms 344 KB Output is correct
27 Correct 7 ms 344 KB Output is correct
28 Correct 5 ms 344 KB Output is correct
29 Correct 3 ms 344 KB Output is correct
30 Correct 4 ms 344 KB Output is correct
31 Correct 6 ms 344 KB Output is correct
32 Correct 8 ms 344 KB Output is correct
33 Correct 11 ms 344 KB Output is correct
34 Correct 7 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 344 KB Output is correct
38 Correct 6 ms 344 KB Output is correct
39 Correct 7 ms 344 KB Output is correct
40 Correct 7 ms 440 KB Output is correct
41 Correct 5 ms 344 KB Output is correct
42 Correct 5 ms 440 KB Output is correct
43 Correct 5 ms 344 KB Output is correct
44 Correct 5 ms 424 KB Output is correct
45 Correct 5 ms 344 KB Output is correct
46 Correct 6 ms 344 KB Output is correct
47 Correct 5 ms 344 KB Output is correct
48 Correct 4 ms 344 KB Output is correct
49 Correct 5 ms 344 KB Output is correct
50 Correct 6 ms 344 KB Output is correct
51 Correct 5 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 5 ms 344 KB Output is correct
55 Correct 5 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 5 ms 344 KB Output is correct
59 Correct 4 ms 344 KB Output is correct
60 Correct 6 ms 424 KB Output is correct
61 Correct 5 ms 344 KB Output is correct
62 Correct 6 ms 428 KB Output is correct
63 Correct 5 ms 600 KB Output is correct
64 Correct 5 ms 344 KB Output is correct
65 Correct 6 ms 444 KB Output is correct
66 Correct 7 ms 596 KB Output is correct
67 Correct 4 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 6 ms 448 KB Output is correct
71 Correct 4 ms 344 KB Output is correct
72 Correct 5 ms 344 KB Output is correct
73 Correct 8 ms 344 KB Output is correct
74 Correct 7 ms 344 KB Output is correct
75 Correct 6 ms 432 KB Output is correct
76 Correct 6 ms 452 KB Output is correct
77 Correct 5 ms 344 KB Output is correct
78 Correct 8 ms 344 KB Output is correct
79 Correct 6 ms 448 KB Output is correct
80 Correct 6 ms 344 KB Output is correct
81 Correct 6 ms 344 KB Output is correct
82 Correct 6 ms 344 KB Output is correct