Submission #407408

# Submission time Handle Problem Language Result Execution time Memory
407408 2021-05-18T22:09:30 Z rainboy Domino (COCI15_domino) C
160 / 160
3456 ms 59080 KB
#include <stdio.h>
#include <string.h>

#define N	2000

int di[] = { -1, 1, 0, 0 };
int dj[] = { 0, 0, -1, 1 };

unsigned int X = 12345;

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

int aa[N * N], n;

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)
			if (aa[ii[j]] == aa[i_])
				j++;
			else if (aa[ii[j]] < aa[i_]) {
				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 ii[N][N], jj[N][N]; char visited[N][N];
int is, js, a_, is_, js_, it_, jt_;

void dfs1(int it, int jt) {
	if (visited[it][jt])
		return;
	visited[it][jt] = 1;
	if (ii[it][jt] == -1) {
		int a = aa[is * n + js] + aa[it * n + jt];

		if (a_ < a)
			a_ = a, is_ = is, js_ = js, it_ = it, jt_ = jt;
	} else {
		int is_ = ii[it][jt], js_ = jj[it][jt], d;

		for (d = 0; d < 4; d++) {
			int it_ = is_ + di[d], jt_ = js_ + dj[d];

			if (it_ >= 0 && it_ < n && jt_ >= 0 && jt_ < n)
				dfs1(it_, jt_);
		}
	}
}

int dfs2(int it, int jt) {
	if (visited[it][jt])
		return 0;
	visited[it][jt] = 1;
	if (ii[it][jt] == -1)
		return it == it_ && jt == jt_;
	else {
		int is_ = ii[it][jt], js_ = jj[it][jt], d;

		for (d = 0; d < 4; d++) {
			int it_ = is_ + di[d], jt_ = js_ + dj[d];

			if (it_ >= 0 && it_ < n && jt_ >= 0 && jt_ < n && dfs2(it_, jt_)) {
				ii[is_][js_] = it_, jj[is_][js_] = jt_, ii[it_][jt_] = is_, jj[it_][jt_] = js_;
				return 1;
			}
		}
		return 0;
	}
}

int main() {
	static int ij[N * N];
	int n_, k, h, i, j, d;
	long long ans;

	scanf("%d%d", &n, &k);
	ans = 0;
	for (i = 0; i < n; i++)
		for (j = 0; j < n; j++)
			scanf("%d", &aa[i * n + j]), ans += aa[i * n + j];
	n_ = 0;
	for (i = 0; i < n; i++)
		for (j = i % 2; j < n; j += 2)
			ij[n_++] = i * n + j;
	sort(ij, 0, n_);
	for (i = 0; i < n; i++)
		memset(ii[i], -1, n * sizeof *ii[i]), memset(jj[i], -1, n * sizeof *jj[i]);
	while (k--) {
		for (i = 0; i < n; i++)
			memset(visited[i], 0, n * sizeof *visited[i]);
		a_ = -1, is_ = js_ = -1, it_ = jt_ = -1;
		for (h = n_ - 1; h >= 0; h--) {
			i = ij[h] / n, j = ij[h] % n;
			if (ii[i][j] == -1)
				for (d = 0; d < 4; d++) {
					int i_ = i + di[d], j_ = j + dj[d];

					if (i_ >= 0 && i_ < n && j_ >= 0 && j_ < n)
						is = i, js = j, dfs1(i_, j_);
				}
		}
		ans -= a_;
		for (i = 0; i < n; i++)
			memset(visited[i], 0, n * sizeof *visited[i]);
		i = is_, j = js_;
		for (d = 0; d < 4; d++) {
			int i_ = i + di[d], j_ = j + dj[d];

			if (i_ >= 0 && i_ < n && j_ >= 0 && j_ < n && dfs2(i_, j_)) {
				ii[i][j] = i_, jj[i][j] = j_, ii[i_][j_] = i, jj[i_][j_] = j;
				break;
			}
		}
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

domino.c: In function 'main':
domino.c:86:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |  scanf("%d%d", &n, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~
domino.c:90:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |    scanf("%d", &aa[i * n + j]), ans += aa[i * n + j];
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 50 ms 8644 KB Output is correct
2 Correct 34 ms 8604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 844 KB Output is correct
2 Correct 1 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1425 ms 58972 KB Output is correct
2 Correct 552 ms 58876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 893 ms 39904 KB Output is correct
2 Correct 359 ms 39952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 378 ms 23724 KB Output is correct
2 Correct 170 ms 23784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2440 ms 59080 KB Output is correct
2 Correct 720 ms 59012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 844 KB Output is correct
2 Correct 1 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2784 ms 59076 KB Output is correct
2 Correct 749 ms 58972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1356 KB Output is correct
2 Correct 3 ms 1356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3115 ms 59080 KB Output is correct
2 Correct 790 ms 59032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 972 KB Output is correct
2 Correct 2 ms 972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 596 ms 23772 KB Output is correct
2 Correct 219 ms 23976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3456 ms 58976 KB Output is correct
2 Correct 840 ms 58916 KB Output is correct