Submission #847522

# Submission time Handle Problem Language Result Execution time Memory
847522 2023-09-09T19:04:29 Z rainboy Longest Trip (IOI23_longesttrip) C++17
100 / 100
11 ms 936 KB
/* upsolved using some ideas from EmeraldBlock */
#include "longesttrip.h"
#include <vector>

using namespace std;

typedef vector<int> vi;

const int N = 256;

int qu1[N], qu2[N], cnt1, cnt2;

vi longest_trip(int n, int d) {
	cnt1 = cnt2 = 0;
	for (int i = 0; i < n; i++)
		if (cnt1 == 0)
			qu1[cnt1++] = i;
		else if (cnt2 == 0)
			qu2[cnt2++] = i;
		else if (are_connected({ qu1[cnt1 - 1] }, { i }))
			qu1[cnt1++] = i;
		else if (i + 1 == n) {
			if (!are_connected({ qu2[cnt2 - 1] }, { i }))
				while (cnt2)
					qu1[cnt1++] = qu2[--cnt2];
			qu2[cnt2++] = i;
		} else {
			if (are_connected({ i }, { i + 1 })) {
				if (!are_connected({ qu2[cnt2 - 1] }, { i }))
					while (cnt2)
						qu1[cnt1++] = qu2[--cnt2];
				qu2[cnt2++] = i, qu2[cnt2++] = i + 1;
			} else {
				qu1[cnt1++] = i + 1;
				if (!are_connected({ qu2[cnt2 - 1] }, { i }))
					while (cnt2)
						qu1[cnt1++] = qu2[--cnt2];
				qu2[cnt2++] = i;
			}
			i++;
		}
	vi uu, vv, ww;
	if (cnt2 == 0)
		for (int h = 0; h < cnt1; h++)
			ww.push_back(qu1[h]);
	else if (cnt1 == 0)
		for (int h = 0; h < cnt2; h++)
			ww.push_back(qu2[h]);
	else {
		vi uu, vv;
		int lower = 0, upper = cnt1 + 1;
		while (upper - lower > 1) {
			int k = (lower + upper) / 2;
			uu.clear();
			for (int h = 0; h < k; h++)
				uu.push_back(qu1[(h - 1 + cnt1) % cnt1]);
			vv.clear();
			for (int h = 0; h < cnt2; h++)
				vv.push_back(qu2[h]);
			if (are_connected(uu, vv))
				upper = k;
			else
				lower = k;
		}
		int h1 = lower;
		if (h1 == cnt1) {
			if (cnt1 > cnt2)
				for (int h = 0; h < cnt1; h++)
					ww.push_back(qu1[h]);
			else
				for (int h = 0; h < cnt2; h++)
					ww.push_back(qu2[h]);
		} else {
			lower = 0, upper = cnt2;
			while (upper - lower > 1) {
				int k = (lower + upper) / 2;
				uu.clear();
				uu.push_back(qu1[(h1 - 1 + cnt1) % cnt1]);
				vv.clear();
				for (int h = 0; h < k; h++)
					vv.push_back(qu2[(h - 1 + cnt2) % cnt2]);
				if (are_connected(uu, vv))
					upper = k;
				else
					lower = k;
			}
			int h2 = lower;
			if (h1 == 1)
				for (int h = cnt1 - 1; h >= 0; h--)
					ww.push_back(qu1[h]);
			else
				for (int h = 0; h < cnt1; h++)
					ww.push_back(qu1[(h1 + h) % cnt1]);
			if (h2 == 1)
				for (int h = 0; h < cnt2; h++)
					ww.push_back(qu2[h]);
			else
				for (int h = cnt2 - 1; h >= 0; h--)
					ww.push_back(qu2[(h2 + h) % cnt2]);
		}
	}
	return ww;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 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 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 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 428 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 6 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 5 ms 600 KB Output is correct
12 Correct 4 ms 424 KB Output is correct
13 Correct 5 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 7 ms 344 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 4 ms 420 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 344 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 4 ms 428 KB Output is correct
11 Correct 5 ms 600 KB Output is correct
12 Correct 4 ms 424 KB Output is correct
13 Correct 4 ms 600 KB Output is correct
14 Correct 10 ms 344 KB Output is correct
15 Correct 7 ms 344 KB Output is correct
16 Correct 8 ms 344 KB Output is correct
17 Correct 7 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 5 ms 344 KB Output is correct
21 Correct 5 ms 428 KB Output is correct
22 Correct 5 ms 856 KB Output is correct
23 Correct 5 ms 600 KB Output is correct
24 Correct 5 ms 424 KB Output is correct
25 Correct 6 ms 344 KB Output is correct
26 Correct 7 ms 344 KB Output is correct
27 Correct 8 ms 344 KB Output is correct
28 Correct 6 ms 344 KB Output is correct
29 Correct 7 ms 344 KB Output is correct
30 Correct 5 ms 344 KB Output is correct
31 Correct 6 ms 344 KB Output is correct
32 Correct 7 ms 344 KB Output is correct
33 Correct 5 ms 344 KB Output is correct
34 Correct 5 ms 852 KB Output is correct
35 Correct 7 ms 344 KB Output is correct
36 Correct 5 ms 428 KB Output is correct
37 Correct 5 ms 428 KB Output is correct
38 Correct 5 ms 432 KB Output is correct
39 Correct 5 ms 436 KB Output is correct
40 Correct 6 ms 432 KB Output is correct
41 Correct 6 ms 424 KB Output is correct
42 Correct 7 ms 420 KB Output is correct
43 Correct 6 ms 432 KB Output is correct
44 Correct 6 ms 604 KB Output is correct
45 Correct 7 ms 344 KB Output is correct
46 Correct 7 ms 344 KB Output is correct
47 Correct 8 ms 344 KB Output is correct
48 Correct 6 ms 344 KB Output is correct
49 Correct 6 ms 344 KB Output is correct
50 Correct 5 ms 428 KB Output is correct
51 Correct 6 ms 424 KB Output is correct
52 Correct 7 ms 344 KB Output is correct
53 Correct 4 ms 344 KB Output is correct
54 Correct 5 ms 600 KB Output is correct
55 Correct 5 ms 344 KB Output is correct
56 Correct 5 ms 432 KB Output is correct
57 Correct 5 ms 692 KB Output is correct
58 Correct 6 ms 692 KB Output is correct
59 Correct 6 ms 428 KB Output is correct
60 Correct 6 ms 440 KB Output is correct
61 Correct 6 ms 608 KB Output is correct
62 Correct 7 ms 428 KB Output is correct
63 Correct 6 ms 432 KB Output is correct
64 Correct 6 ms 432 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 4 ms 344 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 5 ms 424 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 9 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 4 ms 432 KB Output is correct
11 Correct 5 ms 600 KB Output is correct
12 Correct 6 ms 428 KB Output is correct
13 Correct 5 ms 600 KB Output is correct
14 Correct 7 ms 344 KB Output is correct
15 Correct 7 ms 344 KB Output is correct
16 Correct 11 ms 344 KB Output is correct
17 Correct 6 ms 344 KB Output is correct
18 Correct 8 ms 344 KB Output is correct
19 Correct 5 ms 600 KB Output is correct
20 Correct 5 ms 424 KB Output is correct
21 Correct 7 ms 596 KB Output is correct
22 Correct 7 ms 344 KB Output is correct
23 Correct 6 ms 344 KB Output is correct
24 Correct 8 ms 344 KB Output is correct
25 Correct 8 ms 344 KB Output is correct
26 Correct 5 ms 344 KB Output is correct
27 Correct 6 ms 340 KB Output is correct
28 Correct 6 ms 340 KB Output is correct
29 Correct 5 ms 344 KB Output is correct
30 Correct 5 ms 344 KB Output is correct
31 Correct 6 ms 600 KB Output is correct
32 Correct 7 ms 344 KB Output is correct
33 Correct 7 ms 344 KB Output is correct
34 Correct 6 ms 344 KB Output is correct
35 Correct 8 ms 344 KB Output is correct
36 Correct 8 ms 596 KB Output is correct
37 Correct 5 ms 428 KB Output is correct
38 Correct 6 ms 428 KB Output is correct
39 Correct 6 ms 344 KB Output is correct
40 Correct 5 ms 344 KB Output is correct
41 Correct 5 ms 600 KB Output is correct
42 Correct 5 ms 344 KB Output is correct
43 Correct 5 ms 428 KB Output is correct
44 Correct 6 ms 600 KB Output is correct
45 Correct 4 ms 428 KB Output is correct
46 Correct 5 ms 600 KB Output is correct
47 Correct 5 ms 604 KB Output is correct
48 Correct 5 ms 604 KB Output is correct
49 Correct 5 ms 604 KB Output is correct
50 Correct 5 ms 428 KB Output is correct
51 Correct 6 ms 604 KB Output is correct
52 Correct 6 ms 432 KB Output is correct
53 Correct 6 ms 600 KB Output is correct
54 Correct 8 ms 604 KB Output is correct
55 Correct 7 ms 612 KB Output is correct
56 Correct 6 ms 604 KB Output is correct
57 Correct 7 ms 428 KB Output is correct
58 Correct 6 ms 428 KB Output is correct
59 Correct 6 ms 436 KB Output is correct
60 Correct 7 ms 600 KB Output is correct
61 Correct 7 ms 600 KB Output is correct
62 Correct 5 ms 436 KB Output is correct
63 Correct 6 ms 600 KB Output is correct
64 Correct 6 ms 608 KB Output is correct
65 Correct 7 ms 608 KB Output is correct
66 Correct 8 ms 688 KB Output is correct
67 Correct 6 ms 428 KB Output is correct
68 Correct 7 ms 684 KB Output is correct
69 Correct 6 ms 692 KB Output is correct
70 Correct 6 ms 604 KB Output is correct
71 Correct 6 ms 936 KB Output is correct
72 Correct 5 ms 688 KB Output is correct
73 Correct 6 ms 440 KB Output is correct
74 Correct 6 ms 608 KB Output is correct
75 Correct 7 ms 612 KB Output is correct
76 Correct 7 ms 680 KB Output is correct
77 Correct 5 ms 608 KB Output is correct
78 Correct 6 ms 600 KB Output is correct
79 Correct 6 ms 604 KB Output is correct
80 Correct 7 ms 860 KB Output is correct
81 Correct 6 ms 424 KB Output is correct
82 Correct 8 ms 612 KB Output is correct