답안 #990348

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
990348 2024-05-30T09:06:56 Z Pyqe 가장 긴 여행 (IOI23_longesttrip) C++17
100 / 100
12 ms 2752 KB
#include <bits/stdc++.h>
#include "longesttrip.h"

using namespace std;

long long n,nn[2],nn2[2],ex[2][100069],ex2[2][100069],sq[100069],zs;

inline bool qr(vector<long long> v,vector<long long> v2)
{
	long long i,sz;
	vector<int> cv,cv2;
	
	sz=v.size();
	for(i=0;i<sz;i++)
	{
		cv.push_back(v[i]-1);
	}
	sz=v2.size();
	for(i=0;i<sz;i++)
	{
		cv2.push_back(v2[i]-1);
	}
	return are_connected(cv,cv2);
}

vector<int> longest_trip(int on,int d)
{
	long long i,j,ii,jj,k,l,e,lh,rh,md,zz;
	vector<long long> cv,cv2;
	vector<int> ret;
	
	n=on;
	for(ii=0;ii<2;ii++)
	{
		nn[ii]=0;
	}
	for(i=1;i<=n;i+=4)
	{
		for(ii=0;ii<2;ii++)
		{
			nn2[ii]=0;
		}
		for(j=i;j<=min(i+3,n);j++)
		{
			e=(!nn2[0]||nn2[1]<nn2[0])&&!!nn2[1];
			if(!nn2[e])
			{
				nn2[e]++;
				ex2[e][nn2[e]]=j;
			}
			else
			{
				k=ex2[e][nn2[e]];
				if(qr({j},{k}))
				{
					nn2[e]++;
					ex2[e][nn2[e]]=j;
					reverse(ex2[e]+1,ex2[e]+nn2[e]+1);
				}
				else
				{
					nn2[!e]++;
					ex2[!e][nn2[!e]]=j;
				}
			}
		}
		e=!nn[0]&&!!nn[1];
		if(!nn[e])
		{
			for(ii=0;ii<2;ii++)
			{
				for(j=1;j<=nn2[ii];j++)
				{
					nn[ii]++;
					ex[ii][nn[ii]]=ex2[ii][j];
				}
			}
		}
		else
		{
			k=ex[e][nn[e]];
			l=ex2[0][1];
			if(!nn[!e])
			{
				if(qr({k},{l}))
				{
					for(ii=0;ii<2;ii++)
					{
						for(j=1;j<=nn2[ii];j++)
						{
							nn[e^ii]++;
							ex[e^ii][nn[e^ii]]=ex2[ii][j];
						}
					}
				}
				else
				{
					for(j=nn2[0];j;j--)
					{
						nn[!e]++;
						ex[!e][nn[!e]]=ex2[0][j];
					}
					if(nn2[1])
					{
						k=ex[0][nn[0]];
						l=ex2[1][1];
						e=!qr({k},{l});
						for(j=1;j<=nn2[1];j++)
						{
							nn[e]++;
							ex[e][nn[e]]=ex2[1][j];
						}
						k=ex[0][nn[0]];
						l=ex[1][nn[1]];
						if(qr({k},{l}))
						{
							for(j=nn[1];j;j--)
							{
								nn[0]++;
								ex[0][nn[0]]=ex[1][j];
							}
							nn[1]=0;
						}
					}
				}
			}
			else
			{
				e^=!qr({k},{l});
				for(j=1;j<=nn2[0];j++)
				{
					nn[e]++;
					ex[e][nn[e]]=ex2[0][j];
				}
				if(!nn2[1])
				{
					k=ex[0][nn[0]];
					l=ex[1][nn[1]];
					if(qr({k},{l}))
					{
						for(j=nn[1];j;j--)
						{
							nn[0]++;
							ex[0][nn[0]]=ex[1][j];
						}
						nn[1]=0;
					}
				}
				else
				{
					k=ex[!e][nn[!e]];
					l=ex2[1][nn2[1]];
					if(qr({k},{l}))
					{
						for(j=nn2[1];j;j--)
						{
							nn[!e]++;
							ex[!e][nn[!e]]=ex2[1][j];
						}
						k=ex[0][nn[0]];
						l=ex[1][nn[1]];
						if(qr({k},{l}))
						{
							for(j=nn[1];j;j--)
							{
								nn[0]++;
								ex[0][nn[0]]=ex[1][j];
							}
							nn[1]=0;
						}
					}
					else
					{
						for(j=nn[!e];j;j--)
						{
							nn[e]++;
							ex[e][nn[e]]=ex[!e][j];
						}
						nn[!e]=0;
						k=ex[e][nn[e]];
						l=ex2[1][1];
						if(qr({k},{l}))
						{
							for(j=1;j<=nn2[1];j++)
							{
								nn[e]++;
								ex[e][nn[e]]=ex2[1][j];
							}
						}
						else
						{
							for(j=nn2[1];j;j--)
							{
								nn[!e]++;
								ex[!e][nn[!e]]=ex2[1][j];
							}
						}
					}
				}
			}
		}
	}
	zs=0;
	for(i=1;i<=nn[0];i++)
	{
		cv.push_back(ex[0][i]);
	}
	for(i=1;i<=nn[1];i++)
	{
		cv2.push_back(ex[1][i]);
	}
	if(!nn[0]||!nn[1]||!qr(cv,cv2))
	{
		e=nn[1]>nn[0];
		for(i=1;i<=nn[e];i++)
		{
			zs++;
			sq[zs]=ex[e][i];
		}
	}
	else
	{
		for(ii=0;ii<2;ii++)
		{
			k=ex[ii][1];
			l=ex[ii][nn[ii]];
			if(nn[ii]>2&&!qr({k},{l}))
			{
				break;
			}
		}
		if(ii<2)
		{
			for(ii=0;ii<2;ii++)
			{
				k=ex[0][1];
				for(jj=0;jj<2;jj++)
				{
					l=ex[1][1];
					if(!zs&&qr({k},{l}))
					{
						for(i=nn[0];i;i--)
						{
							zs++;
							sq[zs]=ex[0][i];
						}
						for(i=1;i<=nn[1];i++)
						{
							zs++;
							sq[zs]=ex[1][i];
						}
					}
					reverse(ex[1]+1,ex[1]+nn[1]+1);
				}
				reverse(ex[0]+1,ex[0]+nn[0]+1);
			}
		}
		else
		{
			cv2.clear();
			for(i=1;i<=nn[1];i++)
			{
				cv2.push_back(ex[1][i]);
			}
			for(ii=0;ii<2;ii++)
			{
				for(lh=1,rh=nn[ii];lh<=rh;)
				{
					md=(lh+rh)/2;
					cv.clear();
					for(i=1;i<=md;i++)
					{
						cv.push_back(ex[ii][i]);
					}
					if(qr(cv,cv2))
					{
						zz=md;
						rh=md-1;
					}
					else
					{
						lh=md+1;
					}
				}
				k=zz;
				swap(k,l);
				cv2.clear();
				for(i=1;i<=zz;i++)
				{
					cv2.push_back(ex[ii][i]);
				}
			}
			for(i=k+1;i<=nn[0];i++)
			{
				zs++;
				sq[zs]=ex[0][i];
			}
			for(i=1;i<=k;i++)
			{
				zs++;
				sq[zs]=ex[0][i];
			}
			for(i=l;i<=nn[1];i++)
			{
				zs++;
				sq[zs]=ex[1][i];
			}
			for(i=1;i<l;i++)
			{
				zs++;
				sq[zs]=ex[1][i];
			}
		}
	}
	for(i=1;i<=zs;i++)
	{
		ret.push_back(sq[i]-1);
	}
	return ret;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:288:14: warning: 'zz' may be used uninitialized in this function [-Wmaybe-uninitialized]
  288 |     for(i=1;i<=zz;i++)
      |             ~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 2 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 2392 KB Output is correct
2 Correct 5 ms 2392 KB Output is correct
3 Correct 7 ms 2392 KB Output is correct
4 Correct 5 ms 2392 KB Output is correct
5 Correct 8 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2392 KB Output is correct
2 Correct 4 ms 2392 KB Output is correct
3 Correct 5 ms 2392 KB Output is correct
4 Correct 7 ms 2392 KB Output is correct
5 Correct 6 ms 2484 KB Output is correct
6 Correct 8 ms 2392 KB Output is correct
7 Correct 5 ms 2644 KB Output is correct
8 Correct 8 ms 2392 KB Output is correct
9 Correct 4 ms 2392 KB Output is correct
10 Correct 5 ms 2512 KB Output is correct
11 Correct 5 ms 2392 KB Output is correct
12 Correct 5 ms 2504 KB Output is correct
13 Correct 5 ms 2500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 2392 KB Output is correct
2 Correct 7 ms 2392 KB Output is correct
3 Correct 5 ms 2392 KB Output is correct
4 Correct 5 ms 2392 KB Output is correct
5 Correct 8 ms 2484 KB Output is correct
6 Correct 8 ms 2392 KB Output is correct
7 Correct 6 ms 2392 KB Output is correct
8 Correct 8 ms 2392 KB Output is correct
9 Correct 5 ms 2392 KB Output is correct
10 Correct 7 ms 2512 KB Output is correct
11 Correct 5 ms 2392 KB Output is correct
12 Correct 5 ms 2392 KB Output is correct
13 Correct 7 ms 2508 KB Output is correct
14 Correct 7 ms 2392 KB Output is correct
15 Correct 6 ms 2392 KB Output is correct
16 Correct 8 ms 2392 KB Output is correct
17 Correct 7 ms 2392 KB Output is correct
18 Correct 6 ms 2392 KB Output is correct
19 Correct 10 ms 2648 KB Output is correct
20 Correct 6 ms 2392 KB Output is correct
21 Correct 6 ms 2392 KB Output is correct
22 Correct 7 ms 2644 KB Output is correct
23 Correct 5 ms 2392 KB Output is correct
24 Correct 6 ms 2392 KB Output is correct
25 Correct 6 ms 2392 KB Output is correct
26 Correct 6 ms 2392 KB Output is correct
27 Correct 10 ms 2388 KB Output is correct
28 Correct 7 ms 2392 KB Output is correct
29 Correct 6 ms 2392 KB Output is correct
30 Correct 6 ms 2392 KB Output is correct
31 Correct 7 ms 2392 KB Output is correct
32 Correct 10 ms 2392 KB Output is correct
33 Correct 9 ms 2392 KB Output is correct
34 Correct 6 ms 2392 KB Output is correct
35 Correct 9 ms 2392 KB Output is correct
36 Correct 6 ms 2392 KB Output is correct
37 Correct 5 ms 2392 KB Output is correct
38 Correct 7 ms 2392 KB Output is correct
39 Correct 7 ms 2648 KB Output is correct
40 Correct 6 ms 2392 KB Output is correct
41 Correct 9 ms 2392 KB Output is correct
42 Correct 6 ms 2392 KB Output is correct
43 Correct 7 ms 2508 KB Output is correct
44 Correct 8 ms 2504 KB Output is correct
45 Correct 12 ms 2392 KB Output is correct
46 Correct 10 ms 2644 KB Output is correct
47 Correct 9 ms 2392 KB Output is correct
48 Correct 8 ms 2392 KB Output is correct
49 Correct 9 ms 2392 KB Output is correct
50 Correct 6 ms 2392 KB Output is correct
51 Correct 11 ms 2392 KB Output is correct
52 Correct 9 ms 2388 KB Output is correct
53 Correct 8 ms 2392 KB Output is correct
54 Correct 7 ms 2392 KB Output is correct
55 Correct 7 ms 2648 KB Output is correct
56 Correct 7 ms 2512 KB Output is correct
57 Correct 7 ms 2396 KB Output is correct
58 Correct 8 ms 2400 KB Output is correct
59 Correct 8 ms 2396 KB Output is correct
60 Correct 9 ms 2532 KB Output is correct
61 Correct 7 ms 2388 KB Output is correct
62 Correct 7 ms 2520 KB Output is correct
63 Correct 8 ms 2392 KB Output is correct
64 Correct 11 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2392 KB Output is correct
2 Correct 6 ms 2392 KB Output is correct
3 Correct 5 ms 2644 KB Output is correct
4 Correct 8 ms 2392 KB Output is correct
5 Correct 5 ms 2488 KB Output is correct
6 Correct 8 ms 2644 KB Output is correct
7 Correct 7 ms 2392 KB Output is correct
8 Correct 5 ms 2392 KB Output is correct
9 Correct 7 ms 2392 KB Output is correct
10 Correct 5 ms 2512 KB Output is correct
11 Correct 6 ms 2652 KB Output is correct
12 Correct 6 ms 2652 KB Output is correct
13 Correct 5 ms 2392 KB Output is correct
14 Correct 7 ms 2392 KB Output is correct
15 Correct 8 ms 2392 KB Output is correct
16 Correct 8 ms 2392 KB Output is correct
17 Correct 7 ms 2504 KB Output is correct
18 Correct 7 ms 2392 KB Output is correct
19 Correct 6 ms 2392 KB Output is correct
20 Correct 8 ms 2392 KB Output is correct
21 Correct 7 ms 2392 KB Output is correct
22 Correct 9 ms 2392 KB Output is correct
23 Correct 7 ms 2392 KB Output is correct
24 Correct 7 ms 2392 KB Output is correct
25 Correct 5 ms 2392 KB Output is correct
26 Correct 6 ms 2392 KB Output is correct
27 Correct 8 ms 2392 KB Output is correct
28 Correct 9 ms 2392 KB Output is correct
29 Correct 6 ms 2392 KB Output is correct
30 Correct 5 ms 2392 KB Output is correct
31 Correct 7 ms 2392 KB Output is correct
32 Correct 9 ms 2392 KB Output is correct
33 Correct 12 ms 2392 KB Output is correct
34 Correct 8 ms 2392 KB Output is correct
35 Correct 8 ms 2392 KB Output is correct
36 Correct 8 ms 2392 KB Output is correct
37 Correct 9 ms 2392 KB Output is correct
38 Correct 8 ms 2392 KB Output is correct
39 Correct 8 ms 2392 KB Output is correct
40 Correct 5 ms 2648 KB Output is correct
41 Correct 5 ms 2392 KB Output is correct
42 Correct 11 ms 2392 KB Output is correct
43 Correct 6 ms 2392 KB Output is correct
44 Correct 8 ms 2392 KB Output is correct
45 Correct 6 ms 2392 KB Output is correct
46 Correct 8 ms 2392 KB Output is correct
47 Correct 6 ms 2504 KB Output is correct
48 Correct 6 ms 2392 KB Output is correct
49 Correct 10 ms 2504 KB Output is correct
50 Correct 8 ms 2392 KB Output is correct
51 Correct 8 ms 2392 KB Output is correct
52 Correct 9 ms 2392 KB Output is correct
53 Correct 7 ms 2392 KB Output is correct
54 Correct 8 ms 2484 KB Output is correct
55 Correct 9 ms 2392 KB Output is correct
56 Correct 7 ms 2392 KB Output is correct
57 Correct 7 ms 2392 KB Output is correct
58 Correct 7 ms 2392 KB Output is correct
59 Correct 8 ms 2512 KB Output is correct
60 Correct 7 ms 2392 KB Output is correct
61 Correct 9 ms 2392 KB Output is correct
62 Correct 6 ms 2392 KB Output is correct
63 Correct 6 ms 2392 KB Output is correct
64 Correct 8 ms 2500 KB Output is correct
65 Correct 8 ms 2392 KB Output is correct
66 Correct 8 ms 2392 KB Output is correct
67 Correct 8 ms 2500 KB Output is correct
68 Correct 10 ms 2752 KB Output is correct
69 Correct 12 ms 2392 KB Output is correct
70 Correct 11 ms 2524 KB Output is correct
71 Correct 6 ms 2392 KB Output is correct
72 Correct 7 ms 2524 KB Output is correct
73 Correct 6 ms 2392 KB Output is correct
74 Correct 9 ms 2648 KB Output is correct
75 Correct 10 ms 2504 KB Output is correct
76 Correct 11 ms 2512 KB Output is correct
77 Correct 10 ms 2392 KB Output is correct
78 Correct 8 ms 2516 KB Output is correct
79 Correct 10 ms 2516 KB Output is correct
80 Correct 8 ms 2392 KB Output is correct
81 Correct 11 ms 2520 KB Output is correct
82 Correct 11 ms 2392 KB Output is correct