跳到正文

题解

题解:AT_abc355_c [ABC355C] Bingo 2

lkrqwq约 3 分钟
原文
文章信息与版本

文章信息

发布
2024年5月26日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 3 分钟

版本

  1. v1当前

题意:给定一个 N×NN \times N 的网格,有 TT 次操作,每一次指定一个数 AiA_i,把 AiA_i 所在的位置打上标记,问第一次形成 Bingo 的是第几次。

我们考虑预处理每个数在第几行第几列,然后对于每次操作,给这个位置打上标记,标记的数字表示这是第几次操作。

接着,我们检查是否形成 Bingo,如果形成 Bingo,那么取当前行、列或对角线的最大值,这就表示在第几次形成 Bingo,再取最小值即可。

时间复杂度 O(N2+T)O(N^2+T),空间复杂度 O(N2)O(N^2)。

代码如下:

C++
int a[2005][2005];
int visline[2005 * 2005], viscol[2005 * 2005];
int n, t;
int main() {
	read(n, t);
	rep(i, 1, n, 1) {
		rep(j, 1, n, 1) {
			visline[(i - 1) * n + j] = i;
			viscol[(i - 1) * n + j] = j;
		}
	}
	rep(i, 1, t, 1) {
		int num;
		read(num);
		a[visline[num]][viscol[num]] = i;
	}
	int mn = 1e9;
	rep(i, 1, n, 1) { // check a line
		int cnt = 0, mx = -1;
		rep(j, 1, n, 1) {
			if (a[i][j]) {
				cnt++;
				mx = max(mx, a[i][j]);
			}
		}
		if (cnt == n) {
			mn = min(mn, mx);
		}
	}
	rep(j, 1, n, 1) { // check a column
		int cnt = 0, mx = -1;
		rep(i, 1, n, 1) {
			if (a[i][j]) {
				cnt++;
				mx = max(mx, a[i][j]);
			}
		}
		if (cnt == n) {
			mn = min(mn, mx);
		}
	}
	int cnt = 0, mx = -1;
	rep(i, 1, n, 1) {
		if (a[i][i]) {
			cnt++;
			mx = max(mx, a[i][i]);
		}
	}
	if (cnt == n) {
		mn = min(mn, mx);
	}
	cnt = 0, mx = -1;
	rep(i, 1, n, 1) {
		if (a[i][n - i + 1]) {
			cnt++;
			mx = max(mx, a[i][n - i + 1]);
		}
	}
	if (cnt == n) {
		mn = min(mn, mx);
	}
	if (mn != 1000000000) write(mn);
	else write(-1);
}
原文
存档评论1 条抓取于