Submission #544770

# Submission time Handle Problem Language Result Execution time Memory
544770 2022-04-02T16:36:35 Z rainboy Fence (CEOI08_fence) C
100 / 100
10 ms 420 KB
#include <stdio.h>
#include <string.h>

#define N	100
#define M	100
#define N_	(N + M)
#define M_	(N * (N - 1))
#define INF	0x3f3f3f3f

int min(int a, int b) { return a < b ? a : b; }

unsigned int X = 12345;

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

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

int cross(int x0, int y0, int x1, int y1, int x2, int y2) {
	return cross2(x1 - x0, y1 - y0, x2 - x0, y2 - y0);
}

int xx[N_], yy[N_], ii[M_], jj[M_];

int compare(int h1, int h2) {
	int x1 = xx[jj[h1]] - xx[ii[h1]], y1 = yy[jj[h1]] - yy[ii[h1]], x2 = xx[jj[h2]] - xx[ii[h2]], y2 = yy[jj[h2]] - yy[ii[h2]], sgn1 = x1 < 0 || x1 == 0 && y1 < 0 ? -1 : 1, sgn2 = x2 < 0 || x2 == 0 && y2 < 0 ? -1 : 1;

	return sgn1 != sgn2 ? sgn1 - sgn2 : cross2(x1, y1, x2, y2);
}

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

		while (j < k) {
			int c = compare(hh[j], h);

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

int main() {
	static int kk[N][N], hh[M_], dp[N][N];
	int n, m, n_, m_, h, i, j, k, ans;

	scanf("%d%d", &n, &m), n_ = n + m;
	for (i = 0; i < n_; i++)
		scanf("%d%d", &xx[i], &yy[i]);
	m_ = 0;
	for (i = 0; i < n; i++)
		for (j = 0; j < n; j++)
			if (i != j) {
				ii[m_] = i, jj[m_] = j, hh[m_] = m_, m_++;
				if (cross2(xx[i], yy[i], xx[j], yy[j]) < 0) {
					for (k = n; k < n + m; k++)
						if (cross2(xx[i], yy[i], xx[k], yy[k]) < 0 && cross2(xx[k], yy[k], xx[j], yy[j]) < 0 && cross(xx[i], yy[i], xx[j], yy[j], xx[k], yy[k]) < 0)
							kk[i][j]++;
				} else {
					for (k = n; k < n + m; k++)
						if (cross2(xx[i], yy[i], xx[k], yy[k]) > 0 && cross2(xx[k], yy[k], xx[j], yy[j]) > 0 && cross(xx[i], yy[i], xx[j], yy[j], xx[k], yy[k]) > 0)
							kk[i][j]--;
				}
			}
	sort(hh, 0, m_);
	for (i = 0; i < n; i++)
		memset(dp[i], 0x3f, n * sizeof *dp[i]), dp[i][i] = m * 111;
	for (h = 0; h < m_; h++) {
		j = ii[hh[h]], k = jj[hh[h]];
		for (i = 0; i < n; i++)
			if (dp[i][j] != INF) {
				int x = dp[i][j] + 20 - kk[j][k] * 111;

				dp[i][k] = min(dp[i][k], x);
			}
	}
	ans = INF;
	for (i = 0; i < n; i++)
		ans = min(ans, dp[i][i]);
	printf("%d\n", ans);
	return 0;
}

Compilation message

fence.c: In function 'compare':
fence.c:29:151: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   29 |  int x1 = xx[jj[h1]] - xx[ii[h1]], y1 = yy[jj[h1]] - yy[ii[h1]], x2 = xx[jj[h2]] - xx[ii[h2]], y2 = yy[jj[h2]] - yy[ii[h2]], sgn1 = x1 < 0 || x1 == 0 && y1 < 0 ? -1 : 1, sgn2 = x2 < 0 || x2 == 0 && y2 < 0 ? -1 : 1;
      |                                                                                                                                               ~~~~~~~~^~~~~~~~~
fence.c:29:196: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   29 |  int x1 = xx[jj[h1]] - xx[ii[h1]], y1 = yy[jj[h1]] - yy[ii[h1]], x2 = xx[jj[h2]] - xx[ii[h2]], y2 = yy[jj[h2]] - yy[ii[h2]], sgn1 = x1 < 0 || x1 == 0 && y1 < 0 ? -1 : 1, sgn2 = x2 < 0 || x2 == 0 && y2 < 0 ? -1 : 1;
      |                                                                                                                                                                                            ~~~~~~~~^~~~~~~~~
fence.c: In function 'main':
fence.c:60:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |  scanf("%d%d", &n, &m), n_ = n + m;
      |  ^~~~~~~~~~~~~~~~~~~~~
fence.c:62:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |   scanf("%d%d", &xx[i], &yy[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 380 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 412 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 420 KB Output is correct
2 Correct 1 ms 212 KB Output is correct