Submission #851700

# Submission time Handle Problem Language Result Execution time Memory
851700 2023-09-20T11:50:53 Z denniskim Longest Trip (IOI23_longesttrip) C++17
100 / 100
12 ms 856 KB
#include "longesttrip.h"
#include <bits/stdc++.h>

using namespace std;
typedef int ll;
typedef __int128 lll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
#define MAX 9223372036854775807LL
#define MIN -9223372036854775807LL
#define INF 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed; cout.precision(10);
#define sp << " "
#define en << "\n"
#define compress(v) sort(v.begin(), v.end()), v.erase(unique(v.begin(), v.end()), v.end())

ll n;
ll chk[310];

ll query(ll X, ll Y)
{
	vector<ll> tmp1, tmp2;
	
	tmp1.push_back(X);
	tmp2.push_back(Y);
	
	return are_connected(tmp1, tmp2);
}

vector<ll> longest_trip(ll N, ll D)
{
	vector<ll> ans;
	set<ll> st;
	n = N;
	
	for(ll i = 0 ; i < n ; i++)
	{
		chk[i] = 0;
		st.insert(i);
	}
	
	if(D == 3)
	{
		for(ll i = 0 ; i < n ; i++)
			ans.push_back(i);
		
		return ans;
	}
	
	if(D == 2)
	{
		ans.push_back(0);
		chk[0] = 1;
		st.erase(0);
		
		while(1)
		{
			ll ff = 0;
			
			for(auto &i : st)
			{
				if(query(ans.back(), i))
				{
					ans.push_back(i);
					chk[i] = 1;
					st.erase(i);
					ff = 1;
					break;
				}
			}
			
			if(!ff)
				break;
		}
		
		reverse(ans.begin(), ans.end());
		
		for(auto &i : st)
			ans.push_back(i);
		
		return ans;
	}
	
	vector<ll> P1, P2;
	ll ST = 0;
	
	if(n % 2 == 0)
	{
		if(query(0, 1))
		{
			P1.push_back(0);
			P1.push_back(1);
		}
		
		else
		{
			P1.push_back(0);
			P2.push_back(1);
		}
		
		ST = 2;
	}
	
	else
	{
		P1.push_back(0);
		ST = 1;
	}
	
	for(ll i = ST ; i < n ; i += 2)
	{
		if(P2.empty())
		{
			ll Q1 = query(i, i + 1);
			ll Q2 = query(P1.back(), i);
			ll Q3 = query(P1.back(), i + 1);
			
			if(Q1)
			{
				if(Q2)
				{
					P1.push_back(i);
					P1.push_back(i + 1);
				}
				
				else if(Q3)
				{
					P1.push_back(i + 1);
					P1.push_back(i);
				}
				
				else
				{
					P2.push_back(i);
					P2.push_back(i + 1);
				}
			}
			
			else
			{
				if(Q2)
				{
					P1.push_back(i);
					P2.push_back(i + 1);
				}
				
				else
				{
					P1.push_back(i + 1);
					P2.push_back(i);
				}
			}
		}
		
		else
		{
			ll Q1 = query(i, i + 1);
			
			if(Q1)
			{
				ll Q2 = query(P1.back(), i);
				
				if(!Q2)
					swap(P1, P2);
				
				P1.push_back(i);
				
				if(query(P2.back(), i + 1))
				{
					P2.push_back(i + 1);
					reverse(P2.begin(), P2.end());
					
					for(auto &j : P2)
						P1.push_back(j);
					
					P2.clear();
				}
				
				else
					P1.push_back(i + 1);
			}
			
			else
			{
				ll Q2 = query(P1.back(), i);
				
				if(!Q2)
					swap(P1, P2);
				
				if(query(P2.back(), i + 1))
				{
					P1.push_back(i);
					P2.push_back(i + 1);
				}
				
				else
				{
					P1.push_back(i + 1);
					P2.push_back(i);
				}
			}
		}
		
		if((ll)P1.size() < (ll)P2.size())
			swap(P1, P2);
	}
	
	if(P2.empty())
		return P1;
	
	if(!are_connected(P1, P2))
		return P1;
	
	vector<ll> tmp1;
	
	tmp1.push_back(P2[0]);
	
	if((ll)P2.size() > 1)
		tmp1.push_back(P2.back());
	
	if(are_connected({P1.back()}, tmp1))
	{
		if(are_connected({P1.back()}, {P2[0]}))
		{
			for(auto &i : P2)
				P1.push_back(i);
			
			return P1;
		}
		
		reverse(P2.begin(), P2.end());
		
		for(auto &i : P2)
			P1.push_back(i);
		
		return P1;
	}
	
	else if(are_connected({P1[0]}, tmp1))
	{
		reverse(P1.begin(), P1.end());
		
		if(are_connected({P1.back()}, {P2[0]}))
		{
			for(auto &i : P2)
				P1.push_back(i);
			
			return P1;
		}
		
		reverse(P2.begin(), P2.end());
		
		for(auto &i : P2)
			P1.push_back(i);
		
		return P1;
	}
	
	ll l = 0, r = (ll)P1.size() - 1;
	
	while(l <= r)
	{
		ll mid = (l + r) >> 1;
		vector<ll> Pcut;
		
		for(ll i = 0 ; i <= mid ; i++)
			Pcut.push_back(P1[i]);
		
		if(are_connected(Pcut, P2))
			r = mid - 1;
		else
			l = mid + 1;
	}
	
	ll idx1 = l;
	
	l = 0, r = (ll)P2.size() - 1;
	
	while(l <= r)
	{
		ll mid = (l + r) >> 1;
		vector<ll> Pcut;
		
		for(ll i = 0 ; i <= mid ; i++)
			Pcut.push_back(P2[i]);
		
		if(are_connected({P1[idx1]}, Pcut))
			r = mid - 1;
		else
			l = mid + 1;
	}
	
	ll idx2 = l;
	
	for(ll i = idx1 - 1 ; i >= 0 ; i--)
		ans.push_back(P1[i]);
	
	for(ll i = (ll)P1.size() - 1 ; i >= idx1 ; i--)
		ans.push_back(P1[i]);
	
	for(ll i = idx2 ; i < (ll)P2.size() ; i++)
		ans.push_back(P2[i]);
	
	for(ll i = 0 ; i < idx2 ; i++)
		ans.push_back(P2[i]);
	
	return ans;
}
# 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 2 ms 344 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 6 ms 344 KB Output is correct
4 Correct 6 ms 344 KB Output is correct
5 Correct 4 ms 600 KB Output is correct
6 Correct 6 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 6 ms 852 KB Output is correct
11 Correct 5 ms 600 KB Output is correct
12 Correct 5 ms 444 KB Output is correct
13 Correct 4 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 8 ms 344 KB Output is correct
3 Correct 7 ms 344 KB Output is correct
4 Correct 8 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 10 ms 344 KB Output is correct
8 Correct 6 ms 344 KB Output is correct
9 Correct 7 ms 344 KB Output is correct
10 Correct 8 ms 600 KB Output is correct
11 Correct 7 ms 600 KB Output is correct
12 Correct 6 ms 344 KB Output is correct
13 Correct 7 ms 344 KB Output is correct
14 Correct 12 ms 344 KB Output is correct
15 Correct 7 ms 596 KB Output is correct
16 Correct 11 ms 344 KB Output is correct
17 Correct 7 ms 344 KB Output is correct
18 Correct 7 ms 344 KB Output is correct
19 Correct 7 ms 344 KB Output is correct
20 Correct 7 ms 344 KB Output is correct
21 Correct 7 ms 600 KB Output is correct
22 Correct 8 ms 600 KB Output is correct
23 Correct 8 ms 344 KB Output is correct
24 Correct 7 ms 600 KB Output is correct
25 Correct 7 ms 344 KB Output is correct
26 Correct 8 ms 344 KB Output is correct
27 Correct 6 ms 344 KB Output is correct
28 Correct 8 ms 348 KB Output is correct
29 Correct 7 ms 348 KB Output is correct
30 Correct 8 ms 356 KB Output is correct
31 Correct 8 ms 356 KB Output is correct
32 Correct 6 ms 356 KB Output is correct
33 Correct 7 ms 600 KB Output is correct
34 Correct 8 ms 348 KB Output is correct
35 Correct 7 ms 348 KB Output is correct
36 Correct 8 ms 604 KB Output is correct
37 Correct 8 ms 344 KB Output is correct
38 Correct 7 ms 348 KB Output is correct
39 Correct 7 ms 608 KB Output is correct
40 Correct 6 ms 348 KB Output is correct
41 Correct 9 ms 600 KB Output is correct
42 Correct 7 ms 600 KB Output is correct
43 Correct 6 ms 344 KB Output is correct
44 Correct 7 ms 444 KB Output is correct
45 Correct 10 ms 344 KB Output is correct
46 Correct 9 ms 344 KB Output is correct
47 Correct 11 ms 344 KB Output is correct
48 Correct 9 ms 344 KB Output is correct
49 Correct 9 ms 344 KB Output is correct
50 Correct 7 ms 344 KB Output is correct
51 Correct 8 ms 344 KB Output is correct
52 Correct 8 ms 344 KB Output is correct
53 Correct 7 ms 436 KB Output is correct
54 Correct 7 ms 344 KB Output is correct
55 Correct 7 ms 600 KB Output is correct
56 Correct 7 ms 600 KB Output is correct
57 Correct 8 ms 604 KB Output is correct
58 Correct 9 ms 708 KB Output is correct
59 Correct 9 ms 708 KB Output is correct
60 Correct 9 ms 608 KB Output is correct
61 Correct 8 ms 344 KB Output is correct
62 Correct 7 ms 600 KB Output is correct
63 Correct 8 ms 448 KB Output is correct
64 Correct 10 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 9 ms 340 KB Output is correct
3 Correct 6 ms 344 KB Output is correct
4 Correct 6 ms 596 KB Output is correct
5 Correct 7 ms 344 KB Output is correct
6 Correct 9 ms 344 KB Output is correct
7 Correct 8 ms 344 KB Output is correct
8 Correct 6 ms 344 KB Output is correct
9 Correct 7 ms 344 KB Output is correct
10 Correct 7 ms 344 KB Output is correct
11 Correct 6 ms 452 KB Output is correct
12 Correct 6 ms 596 KB Output is correct
13 Correct 7 ms 600 KB Output is correct
14 Correct 8 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 7 ms 600 KB Output is correct
18 Correct 6 ms 344 KB Output is correct
19 Correct 7 ms 344 KB Output is correct
20 Correct 6 ms 348 KB Output is correct
21 Correct 9 ms 348 KB Output is correct
22 Correct 7 ms 348 KB Output is correct
23 Correct 9 ms 344 KB Output is correct
24 Correct 6 ms 344 KB Output is correct
25 Correct 8 ms 344 KB Output is correct
26 Correct 7 ms 344 KB Output is correct
27 Correct 7 ms 344 KB Output is correct
28 Correct 9 ms 344 KB Output is correct
29 Correct 7 ms 600 KB Output is correct
30 Correct 7 ms 600 KB Output is correct
31 Correct 6 ms 344 KB Output is correct
32 Correct 9 ms 344 KB Output is correct
33 Correct 11 ms 340 KB Output is correct
34 Correct 9 ms 344 KB Output is correct
35 Correct 12 ms 344 KB Output is correct
36 Correct 9 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 8 ms 344 KB Output is correct
40 Correct 7 ms 344 KB Output is correct
41 Correct 7 ms 600 KB Output is correct
42 Correct 8 ms 440 KB Output is correct
43 Correct 8 ms 600 KB Output is correct
44 Correct 6 ms 344 KB Output is correct
45 Correct 7 ms 600 KB Output is correct
46 Correct 8 ms 344 KB Output is correct
47 Correct 7 ms 344 KB Output is correct
48 Correct 8 ms 600 KB Output is correct
49 Correct 7 ms 448 KB Output is correct
50 Correct 8 ms 600 KB Output is correct
51 Correct 6 ms 448 KB Output is correct
52 Correct 7 ms 600 KB Output is correct
53 Correct 7 ms 344 KB Output is correct
54 Correct 6 ms 344 KB Output is correct
55 Correct 7 ms 344 KB Output is correct
56 Correct 9 ms 340 KB Output is correct
57 Correct 7 ms 344 KB Output is correct
58 Correct 6 ms 344 KB Output is correct
59 Correct 6 ms 344 KB Output is correct
60 Correct 6 ms 620 KB Output is correct
61 Correct 8 ms 600 KB Output is correct
62 Correct 10 ms 456 KB Output is correct
63 Correct 7 ms 456 KB Output is correct
64 Correct 8 ms 856 KB Output is correct
65 Correct 8 ms 704 KB Output is correct
66 Correct 7 ms 856 KB Output is correct
67 Correct 8 ms 344 KB Output is correct
68 Correct 7 ms 600 KB Output is correct
69 Correct 8 ms 600 KB Output is correct
70 Correct 8 ms 704 KB Output is correct
71 Correct 9 ms 596 KB Output is correct
72 Correct 9 ms 604 KB Output is correct
73 Correct 9 ms 600 KB Output is correct
74 Correct 8 ms 704 KB Output is correct
75 Correct 8 ms 856 KB Output is correct
76 Correct 8 ms 600 KB Output is correct
77 Correct 8 ms 708 KB Output is correct
78 Correct 9 ms 856 KB Output is correct
79 Correct 8 ms 856 KB Output is correct
80 Correct 7 ms 708 KB Output is correct
81 Correct 7 ms 852 KB Output is correct
82 Correct 8 ms 600 KB Output is correct