답안 #887914

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
887914 2023-12-15T13:20:55 Z djs100201 가장 긴 여행 (IOI23_longesttrip) C++17
100 / 100
10 ms 956 KB
#include "longesttrip.h"
#include<random>
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#define all(v) v.begin(),v.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using PP = pair<ll, P>;
const ll n_ = 1e5 + 505, inf = (ll)2e9 * (ll)1e9 + 7, mod = 998244353;
ll n, m, tc = 1, a, b, c, d, sum, x, y, z, base, ans, k;
vector<int> longest_trip(int N,int D){
	vector<int>ret;
	n=N;
	if(D==3){
		for(int i=0;i<n;i++)ret.push_back(i);
	}
	else if(D==2){
		vector<int>A={0},B={1},C={2};
		deque<ll>T;
		if(are_connected(A,B)){
			T.push_back(0);
			T.push_back(1);
			for(int i=2;i<n;i++){
				A.clear(),B.clear(),C.clear();
				A.push_back(T.front());
				C.push_back(i);
				if(are_connected(A,C))T.push_front(i);
				else T.push_back(i);
			}
		}
		else if(are_connected(B,C)){
			T.push_back(1);
			T.push_back(2);
			for(int i=0;i<n;i++){
				if(i==1 || i==2)continue;
				A.clear(),B.clear(),C.clear();
				A.push_back(T.front());
				C.push_back(i);
				if(are_connected(A,C))T.push_front(i);
				else T.push_back(i);
			}
		}
		else if(are_connected(A,C)){
			T.push_back(0);
			T.push_back(2);
			for(int i=0;i<n;i++){
				if(i==0 || i==2)continue;
				A.clear(),B.clear(),C.clear();
				A.push_back(T.front());
				C.push_back(i);
				if(are_connected(A,C))T.push_front(i);
				else T.push_back(i);
			}
		}
		while(T.size())ret.push_back(T.back()),T.pop_back();
	}
	else{
		vector<vector<int>>S;
		S.push_back({0});
		for(int i=1;i<n;i++){
			a=S.back().back();
			if(are_connected({a},{i}))S.back().push_back(i);
			else{
				if(S.size()>=2 && S.back().size()==1)S[S.size()-2].push_back(i);
				else S.push_back({i});
			}
		}
		if(S.size()==1){
			for(auto nxt:S[0])ret.push_back(nxt);
		}
		else {
			vector<int>A,B;
			A=S[0],B=S[1];
			for(int i=2;i<S.size();i++){
				vector<int>T=S[i];
				if(are_connected({A.back()},{T[0]})){
					for(auto nxt:T)A.push_back(nxt);
				}
				else if(are_connected({B.back()},{T[0]})){
					for(auto nxt:T)B.push_back(nxt);
				}
				else{
					reverse(all(B));
					for(auto nxt:B)A.push_back(nxt);
					B=T;
				}
			}
			if(A.size()<B.size())swap(A,B);
			if(are_connected(A,B)){
				if(are_connected({A[0]},{B[0]})){
					reverse(all(A));
					for(auto nxt:A)ret.push_back(nxt);
					for(auto nxt:B)ret.push_back(nxt);
				}
				else if(are_connected({A[0]},{B.back()})){
					for(auto nxt:B)ret.push_back(nxt);
					for(auto nxt:A)ret.push_back(nxt);
				}
				else if(are_connected({A.back()},{B[0]})){
					for(auto nxt:A)ret.push_back(nxt);
					for(auto nxt:B)ret.push_back(nxt);
				}
				else if(are_connected({A.back()},{B.back()})){
					reverse(all(B));
					for(auto nxt:A)ret.push_back(nxt);
					for(auto nxt:B)ret.push_back(nxt);
				}
				else{
					ll l=0,r=A.size()-1,aa=A.size(),bb=B.size();
					while(l<=r){
						ll mid=(l+r)/2;
						vector<int>T;
						for(int i=0;i<=mid;i++)T.push_back(A[i]);
						if(are_connected(T,B)){
							aa=min(aa,mid);
							r=mid-1;
						}
						else l=mid+1;
					}
					l=0,r=B.size()-1;
					while(l<=r){
						ll mid=(l+r)/2;
						vector<int>T;
						for(int i=0;i<=mid;i++)T.push_back(B[i]);
						if(are_connected({A[aa]},T)){
							bb=min(bb,mid);
							r=mid-1;
						}
						else l=mid+1;
					}
					for(int i=aa+1;i<A.size();i++)ret.push_back(A[i]);
					for(int i=0;i<=aa;i++)ret.push_back(A[i]);
					for(int i=bb;i<B.size();i++)ret.push_back(B[i]);
					for(int i=0;i<bb;i++)ret.push_back(B[i]);
				}
			}
			else{
				for(auto i:A)ret.push_back(i);
			}
		}
	}
	return ret;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:64:22: warning: narrowing conversion of 'a' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
   64 |    if(are_connected({a},{i}))S.back().push_back(i);
      |                      ^
longesttrip.cpp:64:22: warning: narrowing conversion of 'a' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
longesttrip.cpp:76:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |    for(int i=2;i<S.size();i++){
      |                ~^~~~~~~~~
longesttrip.cpp:133:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |      for(int i=aa+1;i<A.size();i++)ret.push_back(A[i]);
      |                     ~^~~~~~~~~
longesttrip.cpp:135:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  135 |      for(int i=bb;i<B.size();i++)ret.push_back(B[i]);
      |                   ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 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 4 ms 344 KB Output is correct
5 Correct 5 ms 600 KB Output is correct
6 Correct 5 ms 344 KB Output is correct
7 Correct 5 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 600 KB Output is correct
11 Correct 4 ms 344 KB Output is correct
12 Correct 4 ms 600 KB Output is correct
13 Correct 4 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 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 4 ms 344 KB Output is correct
6 Correct 9 ms 596 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 444 KB Output is correct
11 Correct 5 ms 440 KB Output is correct
12 Correct 4 ms 692 KB Output is correct
13 Correct 4 ms 696 KB Output is correct
14 Correct 6 ms 344 KB Output is correct
15 Correct 6 ms 344 KB Output is correct
16 Correct 7 ms 344 KB Output is correct
17 Correct 6 ms 608 KB Output is correct
18 Correct 6 ms 344 KB Output is correct
19 Correct 6 ms 692 KB Output is correct
20 Correct 5 ms 344 KB Output is correct
21 Correct 5 ms 704 KB Output is correct
22 Correct 5 ms 700 KB Output is correct
23 Correct 5 ms 692 KB Output is correct
24 Correct 5 ms 852 KB Output is correct
25 Correct 7 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 4 ms 344 KB Output is correct
31 Correct 6 ms 344 KB Output is correct
32 Correct 6 ms 344 KB Output is correct
33 Correct 4 ms 344 KB Output is correct
34 Correct 5 ms 700 KB Output is correct
35 Correct 5 ms 344 KB Output is correct
36 Correct 4 ms 344 KB Output is correct
37 Correct 5 ms 604 KB Output is correct
38 Correct 5 ms 696 KB Output is correct
39 Correct 6 ms 852 KB Output is correct
40 Correct 6 ms 868 KB Output is correct
41 Correct 6 ms 600 KB Output is correct
42 Correct 6 ms 608 KB Output is correct
43 Correct 6 ms 608 KB Output is correct
44 Correct 7 ms 440 KB Output is correct
45 Correct 10 ms 344 KB Output is correct
46 Correct 9 ms 344 KB Output is correct
47 Correct 7 ms 344 KB Output is correct
48 Correct 7 ms 596 KB Output is correct
49 Correct 7 ms 344 KB Output is correct
50 Correct 5 ms 432 KB Output is correct
51 Correct 7 ms 600 KB Output is correct
52 Correct 7 ms 344 KB Output is correct
53 Correct 5 ms 596 KB Output is correct
54 Correct 5 ms 344 KB Output is correct
55 Correct 5 ms 864 KB Output is correct
56 Correct 5 ms 444 KB Output is correct
57 Correct 5 ms 860 KB Output is correct
58 Correct 6 ms 704 KB Output is correct
59 Correct 7 ms 608 KB Output is correct
60 Correct 6 ms 700 KB Output is correct
61 Correct 7 ms 872 KB Output is correct
62 Correct 6 ms 604 KB Output is correct
63 Correct 6 ms 616 KB Output is correct
64 Correct 7 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 4 ms 348 KB Output is correct
3 Correct 5 ms 352 KB Output is correct
4 Correct 4 ms 540 KB Output is correct
5 Correct 5 ms 600 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 600 KB Output is correct
9 Correct 4 ms 600 KB Output is correct
10 Correct 4 ms 444 KB Output is correct
11 Correct 4 ms 440 KB Output is correct
12 Correct 4 ms 448 KB Output is correct
13 Correct 5 ms 600 KB Output is correct
14 Correct 7 ms 344 KB Output is correct
15 Correct 6 ms 344 KB Output is correct
16 Correct 7 ms 344 KB Output is correct
17 Correct 6 ms 600 KB Output is correct
18 Correct 7 ms 608 KB Output is correct
19 Correct 6 ms 600 KB Output is correct
20 Correct 6 ms 608 KB Output is correct
21 Correct 8 ms 344 KB Output is correct
22 Correct 6 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 6 ms 344 KB Output is correct
26 Correct 4 ms 600 KB Output is correct
27 Correct 5 ms 344 KB Output is correct
28 Correct 6 ms 344 KB Output is correct
29 Correct 5 ms 344 KB Output is correct
30 Correct 5 ms 344 KB Output is correct
31 Correct 5 ms 692 KB Output is correct
32 Correct 10 ms 344 KB Output is correct
33 Correct 8 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 9 ms 344 KB Output is correct
37 Correct 5 ms 432 KB Output is correct
38 Correct 7 ms 344 KB Output is correct
39 Correct 7 ms 344 KB Output is correct
40 Correct 5 ms 440 KB Output is correct
41 Correct 5 ms 600 KB Output is correct
42 Correct 5 ms 440 KB Output is correct
43 Correct 5 ms 700 KB Output is correct
44 Correct 7 ms 688 KB Output is correct
45 Correct 5 ms 860 KB Output is correct
46 Correct 5 ms 444 KB Output is correct
47 Correct 4 ms 600 KB Output is correct
48 Correct 5 ms 696 KB Output is correct
49 Correct 5 ms 696 KB Output is correct
50 Correct 7 ms 600 KB Output is correct
51 Correct 6 ms 608 KB Output is correct
52 Correct 6 ms 600 KB Output is correct
53 Correct 6 ms 444 KB Output is correct
54 Correct 6 ms 448 KB Output is correct
55 Correct 6 ms 608 KB Output is correct
56 Correct 5 ms 512 KB Output is correct
57 Correct 5 ms 440 KB Output is correct
58 Correct 6 ms 440 KB Output is correct
59 Correct 6 ms 604 KB Output is correct
60 Correct 6 ms 448 KB Output is correct
61 Correct 6 ms 868 KB Output is correct
62 Correct 5 ms 448 KB Output is correct
63 Correct 5 ms 704 KB Output is correct
64 Correct 6 ms 456 KB Output is correct
65 Correct 7 ms 616 KB Output is correct
66 Correct 9 ms 956 KB Output is correct
67 Correct 7 ms 600 KB Output is correct
68 Correct 6 ms 604 KB Output is correct
69 Correct 6 ms 616 KB Output is correct
70 Correct 7 ms 704 KB Output is correct
71 Correct 6 ms 704 KB Output is correct
72 Correct 6 ms 452 KB Output is correct
73 Correct 7 ms 700 KB Output is correct
74 Correct 7 ms 604 KB Output is correct
75 Correct 7 ms 604 KB Output is correct
76 Correct 6 ms 448 KB Output is correct
77 Correct 6 ms 708 KB Output is correct
78 Correct 6 ms 700 KB Output is correct
79 Correct 6 ms 448 KB Output is correct
80 Correct 7 ms 860 KB Output is correct
81 Correct 7 ms 424 KB Output is correct
82 Correct 7 ms 448 KB Output is correct