답안 #924361

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
924361 2024-02-08T22:28:23 Z rainboy 레이저 센서 (KOI16_laser) C
100 / 100
166 ms 860 KB
/* https://math.stackexchange.com/questions/4650848/prove-that-if-2n-points-are-colored-red-or-blue-then-we-can-always-connect-th */
#include <stdio.h>
#include <string.h>

#define N	1000

unsigned int Z = 12345;

int rand_() {
	return (Z *= 3) >> 1;
}

int xx[N * 4], yy[N * 4], uu[N], vv[N], n, o;

long long cross(int i, int j, int k) {
	return (long long) (xx[j] - xx[i]) * (yy[k] - yy[i]) - (long long) (xx[k] - xx[i]) * (yy[j] - yy[i]);
}

int compare(int i, int j) {
	long long c;

	if (xx[i] == xx[j] && yy[i] == yy[j])
		return 0;
	if (xx[i] == xx[o] && yy[i] == yy[o])
		return -1;
	if (xx[j] == xx[o] && yy[j] == yy[o])
		return 1;
	c = cross(o, i, j);
	return c < 0 ? -1 : 1;
}

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k) {
			long long c = compare(ii[j], i_);

			if (c == 0)
				j++;
			else if (c < 0) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		}
		sort(ii, l, i);
		l = k;
	}
}

void solve(int *ii, int m) {
	int h, h_, i, j, tmp, d;

	if (m == 2) {
		if (ii[0] >= n * 2)
			tmp = ii[0], ii[0] = ii[1], ii[1] = tmp;
		i = ii[0] >> 1, j = ii[1] - n * 2;
		if (uu[i] == -1)
			uu[i] = j;
		else
			vv[i] = j;
		return;
	}
	h_ = -1;
	for (h = 0; h < m; h++)
		if (h_ == -1 || xx[ii[h_]] > xx[ii[h]] || xx[ii[h_]] == xx[ii[h]] && yy[ii[h_]] > yy[ii[h]])
			h_ = h;
	o = ii[h_];
	tmp = ii[0], ii[0] = ii[h_], ii[h_] = tmp;
	sort(ii, 1, m);
	d = ii[0] < n * 2 ? 1 : -1;
	for (h = 1; h + 1 < m; h++)
		if ((d += ii[h] < n * 2 ? 1 : -1) == 0) {
			solve(ii, h + 1), solve(ii + h + 1, m - h - 1);
			return;
		}
	tmp = ii[1], ii[1] = ii[m - 1], ii[m - 1] = tmp;
	solve(ii, 2), solve(ii + 2, m - 2);
}

int main() {
	static int ii[N * 4];
	int i, x, y;

	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%d%d", &x, &y);
		xx[i << 1 | 0] = x, yy[i << 1 | 0] = y;
		xx[i << 1 | 1] = x, yy[i << 1 | 1] = y;
	}
	for (i = 0; i < n * 2; i++)
		scanf("%d%d", &xx[n * 2 + i], &yy[n * 2 + i]);
	for (i = 0; i < n * 4; i++)
		ii[i] = i;
	memset(uu, -1, n * sizeof *uu), memset(vv, -1, n * sizeof *vv);
	solve(ii, n * 4);
	for (i = 0; i < n; i++)
		printf("%d %d\n", uu[i] + 1, vv[i] + 1);
	return 0;
}

Compilation message

laser.c: In function 'solve':
laser.c:69:69: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   69 |   if (h_ == -1 || xx[ii[h_]] > xx[ii[h]] || xx[ii[h_]] == xx[ii[h]] && yy[ii[h_]] > yy[ii[h]])
      |                                             ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
laser.c: In function 'main':
laser.c:88:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
laser.c:90:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |   scanf("%d%d", &x, &y);
      |   ^~~~~~~~~~~~~~~~~~~~~
laser.c:95:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |   scanf("%d%d", &xx[n * 2 + i], &yy[n * 2 + i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 504 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 504 KB Output is correct
12 Correct 0 ms 600 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 428 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 2 ms 348 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 504 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 504 KB Output is correct
12 Correct 0 ms 600 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 428 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 2 ms 348 KB Output is correct
37 Correct 2 ms 348 KB Output is correct
38 Correct 2 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 600 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 436 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 344 KB Output is correct
49 Correct 2 ms 348 KB Output is correct
50 Correct 2 ms 348 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 2 ms 344 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 1 ms 348 KB Output is correct
57 Correct 1 ms 552 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 1 ms 452 KB Output is correct
62 Correct 1 ms 344 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 1 ms 348 KB Output is correct
65 Correct 1 ms 344 KB Output is correct
66 Correct 1 ms 348 KB Output is correct
67 Correct 1 ms 348 KB Output is correct
68 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 504 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 504 KB Output is correct
12 Correct 0 ms 600 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 428 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 2 ms 348 KB Output is correct
37 Correct 2 ms 348 KB Output is correct
38 Correct 2 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 600 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 436 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 344 KB Output is correct
49 Correct 2 ms 348 KB Output is correct
50 Correct 2 ms 348 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 2 ms 344 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 1 ms 348 KB Output is correct
57 Correct 1 ms 552 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 1 ms 452 KB Output is correct
62 Correct 1 ms 344 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 1 ms 348 KB Output is correct
65 Correct 1 ms 344 KB Output is correct
66 Correct 1 ms 348 KB Output is correct
67 Correct 1 ms 348 KB Output is correct
68 Correct 1 ms 348 KB Output is correct
69 Correct 19 ms 348 KB Output is correct
70 Correct 16 ms 348 KB Output is correct
71 Correct 28 ms 860 KB Output is correct
72 Correct 35 ms 348 KB Output is correct
73 Correct 31 ms 348 KB Output is correct
74 Correct 34 ms 348 KB Output is correct
75 Correct 19 ms 348 KB Output is correct
76 Correct 20 ms 516 KB Output is correct
77 Correct 20 ms 344 KB Output is correct
78 Correct 21 ms 344 KB Output is correct
79 Correct 142 ms 348 KB Output is correct
80 Correct 147 ms 592 KB Output is correct
81 Correct 143 ms 460 KB Output is correct
82 Correct 159 ms 344 KB Output is correct
83 Correct 166 ms 348 KB Output is correct
84 Correct 154 ms 344 KB Output is correct
85 Correct 152 ms 508 KB Output is correct
86 Correct 158 ms 504 KB Output is correct
87 Correct 12 ms 348 KB Output is correct
88 Correct 8 ms 500 KB Output is correct
89 Correct 7 ms 344 KB Output is correct
90 Correct 27 ms 348 KB Output is correct
91 Correct 19 ms 348 KB Output is correct
92 Correct 20 ms 516 KB Output is correct
93 Correct 24 ms 348 KB Output is correct
94 Correct 28 ms 344 KB Output is correct
95 Correct 15 ms 344 KB Output is correct
96 Correct 31 ms 348 KB Output is correct
97 Correct 28 ms 344 KB Output is correct
98 Correct 20 ms 600 KB Output is correct