202104-1 灰度直方图
题目链接
http://118.190.20.162/view.page?gpid=T128
代码
n,m,l = map(int,input().split())
res = [0 for i in range(l)]
for i in range(n):
temp = list(map(int,input().split()))
for j in range(m):
res[temp[j]] += 1
print(' '.join(map(str,res)))
202104-2 邻域均值
题目链接
http://118.190.20.162/view.page?gpid=T127
题型分析
二维前缀和!
代码
n, l, r, t = map(int, input().split())
A = []
res = 0 # 记录小于阈值t的像素个数
for i in range(n):
A.append(list(map(int, input().split())))
s = [[0 for i in range(n)] for i in range(n)]
# 处理二维前缀和
for i in range(n):
for j in range(n):
if i == 0 and j == 0:
s[i][j] = A[i][j]
elif j == 0:
s[i][j] = s[i - 1][j] + A[i][j]
elif i == 0:
s[i][j] = s[i][j - 1] + A[i][j]
else:
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + A[i][j]
for i in range(n):
top = i - r if i - r >= 0 else 0
bottom = i + r if i + r < n else n - 1
for j in range(n):
right = j + r if j + r < n else n - 1
left = j - r if j - r >= 0 else 0
number = (right - left + 1) * (bottom - top + 1) # 个数
if top == 0 and left == 0:
sum = s[bottom][right]
elif top == 0:
sum = s[bottom][right] - s[bottom][left - 1]
elif left == 0:
sum = s[bottom][right] - s[top - 1][right]
else:
sum = s[bottom][right] - s[top - 1][right] - s[bottom][left - 1] + s[top - 1][left - 1]
avg = sum / number
if avg <= t:
res += 1
print(res)