#include <iostream>
#include <cmath>
using namespace std;
bool Judge(int **A, int n, int r, int t, int i, int j, double &exist_sum, int &exist_y0, int &exist_y1)
{
double sum = 0;
int count = 0;
double average;
int x0, y0, x1, y1;
if(i - r >= 0) x0 = i - r;
else x0 = 0;
if(i + r < n) x1 = i + r;
else x1 = n - 1;
if(j - r >= 0) y0 = j - r;
else y0 = 0;
if(j + r < n) y1 = j + r;
else y1 = n - 1;
if(j == 0){
for(int x = x0; x <= x1; x++)
for(int y = y0; y <= y1; y++)
{
sum += A[x][y];
count++;
}
}
else
{
for(int x = x0; x <= x1; x++){
if(exist_y0 != y0 && exist_y1 != y1)
{
if(sum == 0) sum = (exist_sum) - A[x][y0-1] + A[x][y1];
else sum = sum - A[x][y0-1] + A[x][y1];
}
else if(exist_y0 != y0 && exist_y1 == y1)
{
if(sum == 0) sum = (exist_sum) - A[x][y0-1];
else sum = sum - A[x][y0-1];
}
else
{
if(sum == 0) sum = (exist_sum) + A[x][y1];
else sum = sum + A[x][y1];
}
count = (x1-x0+1)*(y1-y0+1);
}
}
exist_sum = sum;
exist_y0 = y0;
exist_y1 = y1;
average = sum / count;
if(average <= t) return true;
else return false;
}
int main()
{
int n, L, r, t;
cin >> n >> L >> r >> t;
int **A = new int*[n];
for(int i = 0; i < n; i++)
A[i] = new int[n];
long long result = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
cin >> A[i][j];
for(int i = 0; i < n; i++)
{
double exist_sum;
int exist_y0, exist_y1;
for(int j = 0; j < n; j++)
{
if(Judge(A, n, r, t, i, j, exist_sum, exist_y0, exist_y1)) result++;
}
}
cout << result;
return 0;
}
【题解】 CCF CSP 202104-2 —— 邻域均值
最新推荐文章于 2023-05-12 01:48:03 发布