Submission #407401

# Submission time Handle Problem Language Result Execution time Memory
407401 2021-05-18T22:00:56 Z rainboy Domino (COCI15_domino) C
150 / 160
3443 ms 74784 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, 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_ = 0, 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("%d\n", ans);
	return 0;
}

Compilation message

domino.c: In function 'main':
domino.c:85:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |  scanf("%d%d", &n, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~
domino.c:89:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |    scanf("%d", &aa[i * n + j]), ans += aa[i * n + j];
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 49 ms 8644 KB Output is correct
2 Correct 39 ms 8656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 844 KB Output is correct
2 Correct 1 ms 832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1443 ms 74060 KB Output is correct
2 Correct 588 ms 74564 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 907 ms 39920 KB Output is correct
2 Correct 335 ms 46476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 391 ms 23892 KB Output is correct
2 Correct 173 ms 23748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2456 ms 74156 KB Output is correct
2 Correct 687 ms 70596 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 2780 ms 74232 KB Output is correct
2 Incorrect 786 ms 74636 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1356 KB Output is correct
2 Correct 4 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 3132 ms 74268 KB Output is correct
2 Correct 826 ms 74784 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 23744 KB Output is correct
2 Correct 219 ms 23768 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 3443 ms 74196 KB Output is correct
2 Correct 879 ms 74536 KB Output is correct