Submission #407406

# Submission time Handle Problem Language Result Execution time Memory
407406 2021-05-18T22:05:56 Z rainboy Domino (COCI15_domino) C
150 / 160
3473 ms 59076 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_ = -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("%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 53 ms 8664 KB Output is correct
2 Correct 39 ms 8676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 844 KB Output is correct
2 Correct 1 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1492 ms 59068 KB Output is correct
2 Correct 583 ms 58972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 963 ms 39920 KB Output is correct
2 Correct 361 ms 39880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 418 ms 23780 KB Output is correct
2 Correct 187 ms 23692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2563 ms 58968 KB Output is correct
2 Correct 740 ms 58968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 716 KB Output is correct
2 Correct 1 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2854 ms 58976 KB Output is correct
2 Incorrect 745 ms 59024 KB Output isn't 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 3165 ms 59076 KB Output is correct
2 Correct 788 ms 59008 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 613 ms 23772 KB Output is correct
2 Correct 216 ms 23740 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 3473 ms 58968 KB Output is correct
2 Correct 835 ms 58980 KB Output is correct