답안 #849503

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
849503 2023-09-14T18:36:30 Z vqpahmad 가장 긴 여행 (IOI23_longesttrip) C++17
100 / 100
13 ms 692 KB
#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);
		}
		else if (are_connected({grp[1].back()}, {u})){
			grp[1].pb(u);
			are_connected({u}, {v}) ? grp[1].pb(v) : grp[0].pb(v);
			i++;
		}
		else 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};
			i++;
		}
		else {
			grp[1].pb(v);
			reverse(all(grp[1]));
			grp[0].insert(grp[0].end(), grp[1].begin(), grp[1].end());
			grp[1] = {u};
			i++;
		}
	}
	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;
	}
	else if (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;
	}
	else if (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;
	}
	else 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])){
			hi = mi;
		}
		else {
			lo = 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, {grp[0][ans1]})){
			hi = mi;
		}
		else {
			lo = 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 596 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 600 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 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 600 KB Output is correct
5 Correct 5 ms 596 KB Output is correct
6 Correct 7 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 5 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 344 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 4 ms 344 KB Output is correct
3 Correct 4 ms 596 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 432 KB Output is correct
10 Correct 5 ms 344 KB Output is correct
11 Correct 5 ms 600 KB Output is correct
12 Correct 5 ms 344 KB Output is correct
13 Correct 4 ms 344 KB Output is correct
14 Correct 11 ms 344 KB Output is correct
15 Correct 10 ms 344 KB Output is correct
16 Correct 8 ms 344 KB Output is correct
17 Correct 5 ms 344 KB Output is correct
18 Correct 5 ms 600 KB Output is correct
19 Correct 6 ms 436 KB Output is correct
20 Correct 6 ms 440 KB Output is correct
21 Correct 5 ms 344 KB Output is correct
22 Correct 5 ms 600 KB Output is correct
23 Correct 5 ms 440 KB Output is correct
24 Correct 5 ms 444 KB Output is correct
25 Correct 9 ms 344 KB Output is correct
26 Correct 11 ms 344 KB Output is correct
27 Correct 8 ms 344 KB Output is correct
28 Correct 9 ms 344 KB Output is correct
29 Correct 7 ms 344 KB Output is correct
30 Correct 4 ms 344 KB Output is correct
31 Correct 5 ms 344 KB Output is correct
32 Correct 6 ms 344 KB Output is correct
33 Correct 5 ms 344 KB Output is correct
34 Correct 7 ms 600 KB Output is correct
35 Correct 5 ms 600 KB Output is correct
36 Correct 6 ms 600 KB Output is correct
37 Correct 5 ms 596 KB Output is correct
38 Correct 5 ms 344 KB Output is correct
39 Correct 5 ms 440 KB Output is correct
40 Correct 5 ms 344 KB Output is correct
41 Correct 6 ms 344 KB Output is correct
42 Correct 6 ms 344 KB Output is correct
43 Correct 8 ms 344 KB Output is correct
44 Correct 7 ms 604 KB Output is correct
45 Correct 10 ms 344 KB Output is correct
46 Correct 9 ms 344 KB Output is correct
47 Correct 8 ms 344 KB Output is correct
48 Correct 6 ms 596 KB Output is correct
49 Correct 6 ms 344 KB Output is correct
50 Correct 5 ms 432 KB Output is correct
51 Correct 7 ms 436 KB Output is correct
52 Correct 7 ms 344 KB Output is correct
53 Correct 5 ms 600 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 600 KB Output is correct
57 Correct 5 ms 440 KB Output is correct
58 Correct 7 ms 600 KB Output is correct
59 Correct 8 ms 444 KB Output is correct
60 Correct 8 ms 444 KB Output is correct
61 Correct 6 ms 600 KB Output is correct
62 Correct 6 ms 600 KB Output is correct
63 Correct 7 ms 604 KB Output is correct
64 Correct 6 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 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 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 5 ms 344 KB Output is correct
8 Correct 4 ms 344 KB Output is correct
9 Correct 4 ms 688 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 5 ms 344 KB Output is correct
14 Correct 12 ms 344 KB Output is correct
15 Correct 9 ms 344 KB Output is correct
16 Correct 6 ms 344 KB Output is correct
17 Correct 5 ms 600 KB Output is correct
18 Correct 6 ms 600 KB Output is correct
19 Correct 5 ms 600 KB Output is correct
20 Correct 5 ms 436 KB Output is correct
21 Correct 10 ms 344 KB Output is correct
22 Correct 13 ms 344 KB Output is correct
23 Correct 9 ms 344 KB Output is correct
24 Correct 6 ms 344 KB Output is correct
25 Correct 6 ms 344 KB Output is correct
26 Correct 5 ms 344 KB Output is correct
27 Correct 7 ms 344 KB Output is correct
28 Correct 7 ms 344 KB Output is correct
29 Correct 5 ms 600 KB Output is correct
30 Correct 5 ms 600 KB Output is correct
31 Correct 5 ms 600 KB Output is correct
32 Correct 8 ms 344 KB Output is correct
33 Correct 7 ms 344 KB Output is correct
34 Correct 7 ms 344 KB Output is correct
35 Correct 6 ms 344 KB Output is correct
36 Correct 9 ms 344 KB Output is correct
37 Correct 5 ms 436 KB Output is correct
38 Correct 6 ms 440 KB Output is correct
39 Correct 7 ms 344 KB Output is correct
40 Correct 5 ms 344 KB Output is correct
41 Correct 6 ms 600 KB Output is correct
42 Correct 6 ms 344 KB Output is correct
43 Correct 4 ms 344 KB Output is correct
44 Correct 5 ms 604 KB Output is correct
45 Correct 5 ms 444 KB Output is correct
46 Correct 5 ms 440 KB Output is correct
47 Correct 4 ms 344 KB Output is correct
48 Correct 5 ms 600 KB Output is correct
49 Correct 5 ms 344 KB Output is correct
50 Correct 5 ms 600 KB Output is correct
51 Correct 5 ms 344 KB Output is correct
52 Correct 5 ms 344 KB Output is correct
53 Correct 6 ms 424 KB Output is correct
54 Correct 5 ms 344 KB Output is correct
55 Correct 6 ms 692 KB Output is correct
56 Correct 6 ms 344 KB Output is correct
57 Correct 5 ms 344 KB Output is correct
58 Correct 7 ms 344 KB Output is correct
59 Correct 6 ms 600 KB Output is correct
60 Correct 8 ms 344 KB Output is correct
61 Correct 7 ms 340 KB Output is correct
62 Correct 4 ms 344 KB Output is correct
63 Correct 7 ms 604 KB Output is correct
64 Correct 6 ms 432 KB Output is correct
65 Correct 6 ms 440 KB Output is correct
66 Correct 7 ms 600 KB Output is correct
67 Correct 6 ms 600 KB Output is correct
68 Correct 6 ms 440 KB Output is correct
69 Correct 6 ms 600 KB Output is correct
70 Correct 10 ms 444 KB Output is correct
71 Correct 5 ms 600 KB Output is correct
72 Correct 7 ms 600 KB Output is correct
73 Correct 6 ms 604 KB Output is correct
74 Correct 7 ms 600 KB Output is correct
75 Correct 6 ms 436 KB Output is correct
76 Correct 6 ms 432 KB Output is correct
77 Correct 5 ms 436 KB Output is correct
78 Correct 6 ms 440 KB Output is correct
79 Correct 6 ms 440 KB Output is correct
80 Correct 6 ms 600 KB Output is correct
81 Correct 8 ms 340 KB Output is correct
82 Correct 7 ms 344 KB Output is correct