This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |