I will do E --Bomber that I couldn't solve in ABC yesterday.
E - Bomber Keeping the grid in a two-dimensional array is cumbersome and time-consuming, so manage the number of bombs on each of the $ H and W $ axes. If you select the maximum number of each bomb on the $ H and W $ axes and return the sum of the maximum values, the answer seems to be correct, but that is not enough. This is because it cannot handle the case where there is a bomb at the intersection. If there are bombs at the intersections, the sum of their maximums is -1.
Hold the intersecting points by putting tuples in the dict. 0 is the point without a bomb and 1 is the point with a bomb.
from collections import defaultdict
h, w, m = map(int, input().split())
cnt_h = [0] * h
cnt_w = [0] * w
dic = defaultdict(int)
for _ in range(m):
h_m, w_m = map(int, input().split())
cnt_h[h_m - 1] += 1
cnt_w[w_m - 1] += 1
dic[(h_m - 1, w_m - 1)] += 1
max_h = max(cnt_h)
max_w = max(cnt_w)
mh = []
mw = []
for i in range(h):
if max_h == cnt_h[i]:
mh.append(i)
for i in range(w):
if max_w == cnt_w[i]:
mw.append(i)
flag = False
for i in mh:
for j in mw:
if dic[(i, j)] == 0:
flag = True #If there is a point of 0, it will be the maximum value, so break it.
break
if flag:
break
if flag:
print(max_h + max_w)
else:
print(max_h + max_w - 1)
Recommended Posts