Submission #485896

# Submission time Handle Problem Language Result Execution time Memory
485896 2021-11-09T15:58:39 Z rainboy ČVENK (COI15_cvenk) C
60 / 100
142 ms 3696 KB
#include <stdio.h>

#define N	100000
#define X	512
#define L	29
#define INF	0x3f3f3f3f3f3f3f3fLL

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

int kk[X][X], n, x_, y_;

int dfs1(int x, int y) {
	int k, k1, k2;

	if (x >= X || y >= X || (x & y) != 0)
		return 0;
	k1 = dfs1(x, y + 1), k2 = dfs1(x + 1, y), k = kk[x][y] + k1 + k2;
	if (k1 * 2 <= n && k2 * 2 <= n && (n - k) * 2 <= n)
		x_ = x, y_ = y;
	return k;
}

long long ans;

void lca(int x1, int y1, int x2, int y2, int *x, int *y, int l) {
	if (l == 0)
		*x = 0, *y = 0;
	else {
		int n = 1 << l - 1;

		if (x1 >= n && y1 < n && x2 >= n && y2 < n)
			lca(x1 - n, y1, x2 - n, y2, x, y, l - 1), *x += n;
		else if (x1 < n && y1 >= n && x2 < n && y2 >= n)
			lca(x1, y1 - n, x2, y2 - n, x, y, l - 1), *y += n;
		else {
			if (x1 >= n && y1 < n)
				x1 = n - 1, y1 = 0;
			else if (x1 < n && y1 >= n)
				x1 = 0, y1 = n - 1;
			if (x2 >= n && y2 < n)
				x2 = n - 1, y2 = 0;
			else if (x2 < n && y2 >= n)
				x2 = 0, y2 = n - 1;
			lca(x1, y1, x2, y2, x, y, l - 1);
		}
	}
}

long long dist(int x1, int y1, int x2, int y2) {
	int x, y;

	lca(x1, y1, x2, y2, &x, &y, L);
	return (long long) (x1 + y1) + (x2 + y2) - (long long) (x + y) * 2;
}

int main() {
	static int xx[N], yy[N];
	int i, j, k;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d%d", &xx[i], &yy[i]);
	if (n <= 100) {
		ans = INF;
		for (i = 0; i < n; i++)
			for (j = 0; j < n; j++) {
				long long d;

				lca(xx[i], yy[i], xx[j], yy[j], &x_, &y_, L);
				d = 0;
				for (k = 0; k < n; k++)
					d += dist(xx[k], yy[k], x_, y_);
				ans = min(ans, d);
			}
	} else {
		for (i = 0; i < n; i++)
			kk[xx[i]][yy[i]]++;
		dfs1(0, 0);
		ans = 0;
		for (i = 0; i < n; i++)
			ans += dist(xx[i], yy[i], x_, y_);
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

cvenk.c: In function 'lca':
cvenk.c:29:18: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   29 |   int n = 1 << l - 1;
      |                ~~^~~
cvenk.c: In function 'main':
cvenk.c:60:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
cvenk.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 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 204 KB Output is correct
2 Correct 141 ms 276 KB Output is correct
3 Correct 142 ms 280 KB Output is correct
4 Correct 90 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 1584 KB Output is correct
2 Correct 24 ms 1484 KB Output is correct
3 Correct 24 ms 1332 KB Output is correct
4 Correct 23 ms 1408 KB Output is correct
5 Correct 23 ms 1484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 25 ms 3696 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -