1. 题目来源
链接:3412. 邻域均值
2. 题目解析
CCF
认证的第二题也算是送分题。枚举每个点,再确定其邻域的坐标即可,注意边界情况。预处理二维前缀和。
代码:
二维前缀和:
// 二维前缀和,枚举每个点的所有邻域即可,注意边界情况
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 605;
int n, r, l, t;
int a[N][N], s[N][N];
int main() {
cin >> n >> l >> r >> t;
// 不需要 a 矩阵...
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ ) {
cin >> a[i][j];
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
}
int res = 0;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ ) {
int x1 = max(1, i - r), y1 = max(1, j - r);
int x2 = min(n, i + r), y2 = min(n, j + r);
int total = (x2 - x1 + 1) * (y2 - y1 + 1);
int sum = s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1];
if (total * t >= sum) res ++ ;
}
cout << res << endl;
return 0;
}