Submission #924824

# Submission time Handle Problem Language Result Execution time Memory
924824 2024-02-09T19:44:49 Z rainboy None (KOI16_dist) C
100 / 100
154 ms 1888 KB
#include <stdio.h>

#define N	30000
#define INF	0x3f3f3f3f3f3f3f3fLL

unsigned int X = 12345;

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

long long cross2(int x1, int y1, int x2, int y2) {
	return (long long) x1 * y2 - (long long) x2 * y1;
}

long long dot2(int x1, int y1, int x2, int y2) {
	return (long long) x1 * x2 + (long long) y1 * y2;
}

int xx[N], yy[N], ddx[N], ddy[N], xx_[N], yy_[N], n;

long long cross(int i, int j, int k) {
	return cross2(xx_[j] - xx_[i], yy_[j] - yy_[i], xx_[k] - xx_[i], yy_[k] - yy_[i]);
}

int o;

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

	ti = xx_[i] != xx_[o] ? (xx_[i] < xx_[o] ? 1 : 2) : (yy_[i] != yy_[o] ? (yy_[i] < yy_[o] ? 1 : 2) : 0);
	tj = xx_[j] != xx_[o] ? (xx_[j] < xx_[o] ? 1 : 2) : (yy_[j] != yy_[o] ? (yy_[j] < yy_[o] ? 1 : 2) : 0);
	if (ti != tj)
		return ti - tj;
	c = cross(o, i, j);
	if (c != 0)
		return c < 0 ? -1 : 1;
	return xx_[i] != xx_[j] ? xx_[i] - xx_[j] : yy_[i] - yy_[j];
}

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) {
			int 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;
	}
}

int i_, j_;

long long solve() {
	static int ii[N];
	int cnt, h, h_, i;
	long long d, d_;

	o = -1;
	for (i = 0; i < n; i++)
		if (o == -1 || xx_[o] > xx_[i] || xx_[o] == xx_[i] && yy_[o] > yy_[i])
			o = i;
	for (i = 0; i < n; i++)
		ii[i] = i;
	ii[0] = o, ii[o] = 0;
	sort(ii, 1, n);
	cnt = 0;
	for (i = 0; i < n; i++) {
		if (cnt && compare(ii[cnt - 1], ii[i]) == 0)
			continue;
		while (cnt >= 2 && cross(ii[cnt - 2], ii[cnt - 1], ii[i]) >= 0)
			cnt--;
		ii[cnt++] = ii[i];
	}
	d_ = 0, i_ = j_ = -1;
	for (h = 0, h_ = 0; h < cnt; h++) {
		if (h_ == h)
			h_ = (h_ + 1) % cnt;
		while (cross2(xx_[ii[(h + 1) % cnt]] - xx_[ii[h]], yy_[ii[(h + 1) % cnt]] - yy_[ii[h]], xx_[ii[(h_ + 1) % cnt]] - xx_[ii[h_]], yy_[ii[(h_ + 1) % cnt]] - yy_[ii[h_]]) < 0)
			h_ = (h_ + 1) % cnt;
		d = dot2(xx_[ii[h_]] - xx_[ii[h]], yy_[ii[h_]] - yy_[ii[h]], xx_[ii[h_]] - xx_[ii[h]], yy_[ii[h_]] - yy_[ii[h]]);
		if (d_ < d)
			d_ = d, i_ = ii[h], j_ = ii[h_];
	}
	return d_;
}

int main() {
	int t, i, lower, upper, t_;
	long long d, d_;

	scanf("%d%d", &n, &t);
	for (i = 0; i < n; i++)
		scanf("%d%d%d%d", &xx[i], &yy[i], &ddx[i], &ddy[i]);
	lower = -1, upper = t + 1, t_ = -1, d_ = INF;
	while (upper - lower > 1) {
		t = (lower + upper) / 2;
		for (i = 0; i < n; i++) {
			xx_[i] = xx[i] + ddx[i] * t;
			yy_[i] = yy[i] + ddy[i] * t;
		}
		d = solve();
		if (d_ > d || d_ == d && t_ > t)
			d_ = d, t_ = t;
		if (dot2(xx_[j_] - xx_[i_], yy_[j_] - yy_[i_], ddx[j_] - ddx[i_], ddy[j_] - ddy[i_]) >= 0)
			upper = t;
		else
			lower = t;
	}
	printf("%d\n", t_);
	printf("%lld\n", d_);
	return 0;
}

Compilation message

dist.c: In function 'solve':
dist.c:73:54: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   73 |   if (o == -1 || xx_[o] > xx_[i] || xx_[o] == xx_[i] && yy_[o] > yy_[i])
      |                                     ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
dist.c: In function 'main':
dist.c:115:25: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  115 |   if (d_ > d || d_ == d && t_ > t)
      |                 ~~~~~~~~^~~~~~~~~
dist.c:104:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |  scanf("%d%d", &n, &t);
      |  ^~~~~~~~~~~~~~~~~~~~~
dist.c:106:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |   scanf("%d%d%d%d", &xx[i], &yy[i], &ddx[i], &ddy[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 596 KB Output is correct
7 Correct 0 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 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 596 KB Output is correct
7 Correct 0 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 0 ms 348 KB Output is correct
11 Correct 5 ms 348 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 4 ms 488 KB Output is correct
14 Correct 4 ms 344 KB Output is correct
15 Correct 3 ms 348 KB Output is correct
16 Correct 3 ms 348 KB Output is correct
17 Correct 5 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 3 ms 348 KB Output is correct
20 Correct 4 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 1116 KB Output is correct
2 Correct 40 ms 1248 KB Output is correct
3 Correct 36 ms 1236 KB Output is correct
4 Correct 32 ms 1236 KB Output is correct
5 Correct 35 ms 1236 KB Output is correct
6 Correct 26 ms 1036 KB Output is correct
7 Correct 31 ms 1116 KB Output is correct
8 Correct 10 ms 1492 KB Output is correct
9 Correct 39 ms 1884 KB Output is correct
10 Correct 40 ms 1816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 596 KB Output is correct
7 Correct 0 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 0 ms 348 KB Output is correct
11 Correct 5 ms 348 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 4 ms 488 KB Output is correct
14 Correct 4 ms 344 KB Output is correct
15 Correct 3 ms 348 KB Output is correct
16 Correct 3 ms 348 KB Output is correct
17 Correct 5 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 3 ms 348 KB Output is correct
20 Correct 4 ms 344 KB Output is correct
21 Correct 37 ms 1116 KB Output is correct
22 Correct 40 ms 1248 KB Output is correct
23 Correct 36 ms 1236 KB Output is correct
24 Correct 32 ms 1236 KB Output is correct
25 Correct 35 ms 1236 KB Output is correct
26 Correct 26 ms 1036 KB Output is correct
27 Correct 31 ms 1116 KB Output is correct
28 Correct 10 ms 1492 KB Output is correct
29 Correct 39 ms 1884 KB Output is correct
30 Correct 40 ms 1816 KB Output is correct
31 Correct 150 ms 1888 KB Output is correct
32 Correct 150 ms 1720 KB Output is correct
33 Correct 154 ms 1732 KB Output is correct
34 Correct 139 ms 1624 KB Output is correct
35 Correct 141 ms 1868 KB Output is correct
36 Correct 107 ms 1756 KB Output is correct
37 Correct 123 ms 1624 KB Output is correct
38 Correct 21 ms 1624 KB Output is correct
39 Correct 107 ms 1880 KB Output is correct
40 Correct 146 ms 1840 KB Output is correct
41 Correct 145 ms 1836 KB Output is correct
42 Correct 146 ms 1880 KB Output is correct