Problem:


Score : 500 points

Problem Statement

We have a two-dimensional grid with H \times W squares. There are M targets to destroy in this grid - the position of the i-th target is \left(h_i, w_i \right).

Takahashi will choose one square in this grid, place a bomb there, and ignite it. The bomb will destroy all targets that are in the row or the column where the bomb is placed. It is possible to place the bomb at a square with a target.

Takahashi is trying to maximize the number of targets to destroy. Find the maximum number of targets that can be destroyed.

Constraints

All values in input are integers.

1 \leq H, W \leq 3 \times 10^5

1 \leq M \leq \min\left(H\times W, 3 \times 10^5\right)

1 \leq h_i \leq H

1 \leq w_i \leq W

\left(h_i, w_i\right) \neq \left(h_j, w_j\right) \left(i \neq j\right)

Input

Input is given from Standard Input in the following format:

H W M
h_1 w_1
\vdots
h_M w_M

Output

Print the answer.

Sample Input 1

2 3 3
2 2
1 1
1 3

Sample Output 1

3

We can destroy all the targets by placing the bomb at \left(1, 2\right).

Sample Input 2

3 3 4
3 3
3 1
1 1
1 2

Sample Output 2

3

Sample Input 3

5 5 10
2 5
4 3
2 3
5 5
2 2
5 4
5 3
5 1
3 5
1 4

Sample Output 3

6



Sample input:
2 3 3
2 2
1 1
1 3


Sample output:
3

Submission status: Runtime Error

Solution code:
H, W, M = map(int, input().split())

hs = [0] * H
ws = [0] * W
checked = set()

for i in range(M):
    r,c = map(int, input().split())
    r -= 1
    c -= 1

    hs[r] += 1
    ws[c] += 1
    checked.add((r, c))

mh, mw = 0, 0
for i in range(H):
    mh = max(mh, hs[i])
    mw = max(mw, ws[i])

max_h_list = []
max_w_list = []
    
for i in range(H):
    if hs[i] == mh: max_h_list.append(i)    
for j in range(W):
    if ws[i] == mw: max_w_list.append(i)

ans = mh + mw

for i in max_h_list:
    for j in max_w_list:
        if (i,j) not in checked:
            print(ans)
            exit()
print(ans-1)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10**6)
H, W, M = map(int, input().split(" "))

import numpy as np

area = np.zeros((H, W), int)

#print("A")

for _ in range(M):
    y, x = map(int, input().split(" "))
    area[y-1][x-1] = 1

#print("B")    
W_sum_ = area.sum(axis = 1)
H_sum_ = area.sum(axis = 0)

#print("C")

W_max = W_sum_.max()
H_max = H_sum_.max()
y_index = np.where(W_sum_ == W_max)[0]
x_index = np.where(H_sum_ == H_max)[0]

max_sum = 0
x_max = y_max = 0
#print("Y_index", y_index)
#print("x_index", x_index)

#print(area)

for y in y_index:
    for x in x_index:
        sum_ = H_sum_[x] + W_sum_[y]
#        print("y", y, " x", x)
        if area[y][x] == 1:
            sum_ -= 1
        if sum_ > max_sum:
            max_sum = sum_
            x_max = x
            y_max = y
            
print(max_sum)

Submission status: Runtime Error

Solution code:
H, W, M = map(int, input().split(" "))

import numpy as np

area = np.zeros((H, W), int)

#print("A")

for _ in range(M):
    y, x = map(int, input().split(" "))
    area[y-1][x-1] = 1

#print("B")    
W_sum_ = area.sum(axis = 1)
H_sum_ = area.sum(axis = 0)

#print("C")

W_max = W_sum_.max()
H_max = H_sum_.max()
y_index = np.where(W_sum_ == W_max)[0]
x_index = np.where(H_sum_ == H_max)[0]

max_sum = 0
x_max = y_max = 0
#print("Y_index", y_index)
#print("x_index", x_index)

#print(area)

for y in y_index:
    for x in x_index:
        sum_ = H_sum_[x] + W_sum_[y]
        print("y", y, " x", x)
        if area[y][x] == 1:
            sum_ -= 1
        if sum_ > max_sum:
            max_sum = sum_
            x_max = x
            y_max = y
            
print(max_sum)

Submission status: Runtime Error

Solution code:
from bisect import bisect_left, bisect_right

H, W, M = map(int,input().split())
targets = [list(map(lambda x: int(x) - 1, input().split())) for i in range(M)]

cnth = [0] * H
cntw = [0] * W
wlist = [[] for i in range(H)]

for i in range(M):
	hi, wi = targets[i]
	cnth[hi] += 1
	cntw[wi] += 1
	wlist[hi].append(wi)

maxhi = []
maxwi = []
maxh = max(cnth)
maxw = max(cntw)

for i in range(H):
	wlist[i].sort()
	if cnth[i] == maxh:
		maxhi.append(i)
for i in range(W):
	if cntw[i] == maxw:
		maxwi.append(i)

for i in range(len(maxhi)):
	for j in range(len(maxwi)):
		if bisect_right(wlist[maxhi[i]], maxwi[j]) - bisect_left(wlist[maxhi[i]], maxwi[j]) == :
			print(maxh + maxw)
			exit()
print(maxh + maxw - 1)

Submission status: Runtime Error

Solution code:
import numpy as np

H,W,M = input().split()
A = []
S = 0
for i in range(int(M)):
    a_ = input().split()
    a = [int(i)-1 for i in a_]
    A.append(a)
A = np.array(A)

B = []
C = []
for i in h_:
        B.append(sum(A[: , 0]==i))
for j in w_:
        C.append(sum(A[: , 1]==j) )
        
b_max,c_max = max(B),max(C)
S = b_max + c_max
i_m = B.index(b_max)
j_m = C.index(c_max)
if if any([all(a ==np.array([i_m,j_m])) for a in A ]):
            S -=1
print(S)

Submission status: Runtime Error

Solution code:
H, W, M = list(map(int, input().split()))
hrz = [0]*H
vrt = [0]*W
targets = set()
for _ in range(M):
    h, w = list(map(lambda x: int(x) - 1, input().split()))
    targets.add((h, w))
    hrz[h] += 1
    vrt[w] += 1

hrz_max = max(hrz)
vrt_max = max(vrt)
for h in [i for i in range(H) if hrz[i] == hrz_max]:
    for w in [i for i in range(H) if vrt[i] == vrt_max]:
        if not (h, w) in targets:
            print(hrz_max+vrt_max)
            exit()
print(hrz_max+vrt_max-1)

Submission status: Runtime Error

Solution code:
H, W, M = list(map(int, input().split()))
hrz = [0]*H
vrt = [0]*W
targets = set()
target_x = set()
target_y = set()
for _ in range(M):
    h, w = list(map(lambda x: int(x) - 1, input().split()))
    targets.add((h, w))
    hrz[h] += 1
    vrt[w] += 1
    target_x.add(w)
    target_y.add(h)

hrz_max = max(hrz)
vrt_max = max(vrt)
for h in [i for i in range(H) if hrz[i] == hrz_max]:
    for w in [i for i in range(H) if vrt[i] == vrt_max]:
        if not (h, w) in targets:
            print(hrz_max+vrt_max)
            exit()
print(hrz_max+vrt_max-1)

Submission status: Runtime Error

Solution code:
import numpy as np

H,W,M=map(int,input().split())
G=[[0]*W for _ in range(H)]
Hs=np.zeros(H,dtype=int)
Ws=np.zeros(W,dtype=int)
MH=0
MW=0
for i in range(M):
    h,w = map(int, input().split())
    G[h-1][w-1]+=1
    Hs[h-1]+=1
    Ws[w-1]+=1
    MH=max(MH,Hs[h-1])
    MW=max(MW,Ws[w-1])

Hmax_index = []
Wmax_index = []
for i in range(H):
    if Hs[i]==MH:
        Hmax_index.append(i)
for i in range(W):
    if Ws[i]==MW:
        Wmax_index.append(i)

for i in Hmax_index:
    for j in Wmax_index:
        if G[i][j]==0:
            ans=MH+MW
            break
    else:
        continue
    break
else:
    ans=MH+MW-1

print(ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque
import sys
H, W, M = map(int, input().split())
dich = defaultdict(list)
dicw = defaultdict(list)
hw = []
s = [[0]*W for _ in range(H)]

for i in range(M):
    h, w = map(int, input().split())
    hw.append([h, w])
    dich[h].append(w)
    dicw[w].append(h)
    s[h-1][w-1]=1

mh = 0
for k in dich:
    mh = max(mh, len(dich[k]))
    
mw= 0
for k in dicw:
    mw = max(mw, len(dicw[k]))
    
hl = []
for i in hw:
    if len(dich[i[0]])==mh:
        hl.append(i[0])
hl =set(hl)

wl = []
for i in hw:
    if len(dicw[i[1]])==mw:
        wl.append(i[1])
wl = set(wl)

for i in hl:
    for j in wl:
        if s[i-1][j-1]==1:
            continue
        print(mh+mw) 
        sys.exit()
print(mh+mw-1)

Submission status: Runtime Error

Solution code:
H, W, M = map(int, input().split())
hallline = [0 for i in range(H)]
wallline = [0 for i in range(W)]
for j in range(W):
    wallline[j] = 0
b = []

for i in range(M):
    tmp = list(map(lambda x: int(x) - 1, input().split()))
    hallline[tmp[0]] += 1
    wallline[tmp[1]] += 1
    b.append([tmp[0], tmp[1]])

t = max(hallline)
s = max(wallline)

a = [i for i in range(H) if hallline[i] == t]
b = [i for i in range(W) if wallline[i] == s]

for i in a:
    for j in b:
        if not tmp[i][j]:
            print(t + s)
            exit()
print(t + s - 1)

Submission status: Runtime Error

Solution code:
h,w,m,*t = map(int,open(0).read().split())
a = [[0]*w for _ in range(h)]
ht = [0]*h
wt = [0]*w
for i in range(m):
  y,x = t[i*2]-1,t[i*2+1]-1
  a[y][x] += 1
  ht[y] += 1
  wt[x] += 1
htm = max(ht)
wtm = max(wt)
htt = [i for i in range(h) if ht[i]==htm]
wtt = [i for i in range(w) if wt[i]==wtm]
ans = htm+wtm-1
for i in htt:
  for j in wtt:
    if a[i][j] == 0:
      print(ans + 1)
      exit()
print(ans)

Submission status: Runtime Error

Solution code:
h,w,m,*t = map(int,open(0).read().split())
a = [[0]*w for _ in range(h)]
ht = [0]*h
wt = [0]*w
for i in range(m):
  y,x = t[i*2]-1,t[i*2+1]-1
  a[y][x] += 1
  ht[y] += 1
  wt[x] += 1
htm = max(ht)
wtm = max(wt)
htt = [i for i in range(h) if ht[i]==htm]
wtt = [i for i in range(w) if wt[i]==wtm]
ans = 0
for i in htt:
  for j in wtt:
    ans = max(ans,ht[i]+wt[j]-a[i][j])
print(ans)

Submission status: Runtime Error

Solution code:
h,w,m,*t = map(int,open(0).read().split())
a = [[0]*w for _ in range(h)]
ht = [0]*h
wt = [0]*w
for i in range(m):
  y,x = t[i*2]-1,t[i*2+1]-1
  a[y][x] += 1
  ht[y] += 1
  wt[x] += 1
htm = max(ht)-1
wtm = max(wt)-1
htt = [i for i in range(h) if ht[i]>=htm]
wtt = [i for i in range(w) if wt[i]>=wtm]
ans = 0
for i in htt:
  for j in wtt:
    ans = max(ans,ht[i]+wt[j]-a[i][j])
print(ans)

Submission status: Runtime Error

Solution code:
h,w,m=map(int,input().split())
s=[0]*m
t=[0]*m
temp=[]*m
for i in range(m):
  sm,tm=map(int,input().split())
  s[i]=sm
  t[i]=tm
  temp[i]=[s[i],t[i]]
st=[0]*h
tt=[0]*w
for i in range(m):
  st[s[i]-1]=st[s[i]-1]+1
  tt[t[i]-1]=tt[t[i]-1]+1
smax=max(st)
tmax=max(tt)
ans=smax+tmax
maxs=[]
maxt=[]
for i in range(h):
  if st[i]==smax:
    maxs.append(i)
for i in range(w):
  if tt[i]==tmax:
    maxt.append(i)

if len(maxs)*len(maxt)>m:
  print(ans)
else:
  for i in range(len(maxs)):
    for j in range(len(maxt)):
      if (maxs[i]+1,maxt[j]+1) not in temp:
        print(ans)
        exit()
  else:
    print(ans-1)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

H, W, M = map(int, input().split())
l = [tuple(map(int, input().split())) for _ in range(M)]

dh, dw = defaultdict(int), defaultdict(int)
used = set()

for h, w in l:
    dh[h] += 1
    dw[w] += 1
    used.add((h, w))

ih = sorted(dh.items(), key=lambda x:-x[1])
iw = sorted(dw.items(), key=lambda x:-x[1])

def get(x):
    h, counth = ih[x]
    w, countw = iw[x]
    ans = counth + countw
    if (h, w) in used:
        ans -= 1
    return ans

print(max(get(0), get(1)))

Submission status: Runtime Error

Solution code:
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8
@njit((i8[:],i8[:],i8[:],i8[:]), cache=True)
def main(lis_h,lis_w,bomb_h,bomb_w):
  bomb = {}
  for h,w in zip(bomb_h,bomb_w):
    bomb[(h,w)] = 1
  max_h = np.where(lis_h==lis_h.max())
  max_w = np.where(lis_w==lis_w.max())
  ans = lis_h.max()+lis_w.max()
  for h in max_h:
    for w in max_w:
      if (h,w) not in bomb:
        return ans
  return ans-1

H, W, M = map(int, input().split())
bombh = np.zeros(M, np.int64)
bombw = np.zeros(M, np.int64)
lis_h = np.zeros(H, np.int64)
lis_w = np.zeros(W, np.int64)
for i in range(M):
    h,w = map(int, input().split())
    h -= 1
    w -= 1
    bombh[i], bombw[i] = h,w
    lis_h[h] += 1
    lis_w[w] += 1
print(main(lis_h,lis_w,bombh,bombw))

Submission status: Runtime Error

Solution code:
import copy
H, W, M=map(int, input().split())
hw=[list(map(int, input().split())) for _ in range(M)]

x=[0]*(H+1)
y=[0]*(W+1)

for h, w in hw:
  x[h]+=1
  y[w]+=1
  
xmax=max(x)
ymax=max(y)
S=set()
a=0
for i in range(1, W+1):
  if y[i]==ymax:
    S.add(i)
    a+=1
    
b=0
T=[set() for _ in range(H+1)]
for j in range(1, H+1):
  if x[j]==xmax:
    T[j]=copy.copy(S)
    b+=1
    
if a*b>M:
  print(xmax+ymax)
  exit()
  
  
for h, w in hw:
  T[h].discard(w)

for i in range(H+1):
  if len(T[i])>0:
    print(xmax+ymax)
    exit()
    
print(xmax+ymax-1)

Submission status: Runtime Error

Solution code:
import numpy as np
(h,w,m),*e=[[*map(int,i.split())]for i in open(0)]
a=np.zeros((h,w),int)
for y,x in e:a[y-1][x-1]=1
y=np.sum(a,axis=1)
x=np.sum(a,axis=0)
mx,my=max(x),max(y)
# b=sum(y[i-1]+x[j-1]==mx+my for i,j in e)
# b-=len(np.where(y==my)[0])*len(np.where(x==mx)[0])
print(mx+my)

Submission status: Runtime Error

Solution code:
import numpy as np
(h,w,m),*e=[[*map(int,i.split())]for i in open(0)]
a=np.zeros((h,w),int)
for y,x in e:a[y-1][x-1]=1
y=np.sum(a,axis=1)
x=np.sum(a,axis=0)
mx,my=max(x),max(y)
b=sum(y[i-1]+x[j-1]==mx+my for i,j in e)
b-=len(np.where(y==my)[0])*len(np.where(x==mx)[0])
print(mx+my-(b>=0))

Submission status: Runtime Error

Solution code:
import numpy as np
(h,w,m),*e=[[*map(int,i.split())]for i in open(0)]
a=np.zeros((h,w),int)
for y,x in e:
    a[y-1][x-1]=1
y=np.sum(a,axis=1)
x=np.sum(a,axis=0)
ans=max(y)+max(x)
b=sum(y[i-1]+x[j-1]==ans for i,j in e)
b-=len(np.where(y==max(y))[0])*len(np.where(x==max(x))[0])
print(ans-(b>=0))

Submission status: Runtime Error

Solution code:
import numpy as np

n, w, m = map(int, input().split())
matrix = np.zeros((n,w))

for i in range(m):
    h1,w1 = map(int, input().split())
    h1,w1 = h1-1, w1-1
    matrix[h1, w1] = 1

max_count = 0
for j in range(n):
    for k in range(w):
        row = matrix[j]
        count1 = np.count_nonzero(row == 1)
        col = matrix[:,k]
        count2 = np.count_nonzero(col == 1)

        count3 = count1 + count2
        if matrix[j][k] == 1: count3 -=1
        if count3 > max_count:
            max_count = count3

print(max_count)

Submission status: Runtime Error

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    H, W, M = map(int, input().split())
    bombs = []
    hs = [0]*(H)
    ws = [0]*(W)
    for _ in range(M):
        h, w = map(int, input().split())
        bombs.append([h-1, w-1])
        hs[h-1] += 1
        ws[w-1] += 1
    maxh = max(hs)
    maxw = max(ws)
    ans = maxh + maxw

    maxhindex = [i for i, x in enumerate(hs) if x == maxh]
    maxwindex = [i for i, x in enumerate(ws) if x == maxw]

    isfound = False

    for i in maxindex:
        for j in maxwindex:
            if [i, j] not in bombs:
                print(ans)
                exit()

    print(ans-1)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = map(int, input().split())
HW = []
A = np.zeros((H, W), dtype='int')
print(1)
exit()

for _ in range(M):
    h, w = map(int, input().split())
    h, w = h - 1, w - 1  # 0-indexed
    HW.append((h, w))
    A[h, w] = 1

arr = A.sum(axis=0)
col_max = arr.max()
col_idx = np.where(arr == col_max)[0]

arr = A.sum(axis=1)
row_max = arr.max()
row_idx = np.where(arr == row_max)[0]

# print(A.sum(axis=0).argsort()[-1])

ans = col_max + row_max
for r in row_idx:
    for c in col_idx:
        if A[r, c] == 0:
            print(ans)
            exit()
print(ans - 1)

Submission status: Runtime Error

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    H, W, M = map(int, input().split())
    bombs
    hs = [0]*(H)
    ws = [0]*(W)
    for _ in range(M):
        h, w = map(int, input().split())
        bombs.append([h-1, w-1])
        hs[h-1] += 1
        ws[w-1] += 1
    maxh = max(hs)
    maxw = max(ws)
    ans = maxh + maxw

    maxhindex = [i for i, x in enumerate(hs) if x == maxh]
    maxwindex = [i for i, x in enumerate(ws) if x == maxw]

    isfound = False

    # for i in maxindex:
    #     for j in maxwindex:
    #         if [i, j] not in bombs:
    #             print(ans)
    #             exit()

    # print(ans-1)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys

H, W, M = map(int, sys.stdin.readline().strip().split())

h = [0] * H
w = [0] * W

g = [[False] * W for _ in range(H)]

for _ in range(M):
    Th, Tw = map(int, sys.stdin.readline().strip().split())

    h[Th - 1] += 1
    w[Tw - 1] += 1

    g[Th - 1][Tw - 1] = True

ans = max(h) + max(w)

index_h = [n for n, v in enumerate(h) if v == max(h)]
index_w = [n for n, v in enumerate(w) if v == max(w)]

sub = 1
for ih in index_h:
    for iv in index_w:
        if g[ih][iv] is false:
            sub = 0
            break
    else:
        continue
    break

ans -= sub
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = map(int, input().split())

A = np.zeros((H, W), dtype='bool')
for _ in range(M):
    h, w = map(int, input().split())
    h, w = h - 1, w - 1  # 0-indexed
    A[h, w] = 1

arr = A.sum(axis=0)
col_max = arr.max()
col_idx = np.where(arr == col_max)[0]

arr = A.sum(axis=1)
row_max = arr.max()
row_idx = np.where(arr == row_max)[0]

# print(A.sum(axis=0).argsort()[-1])

ans = col_max + row_max
for r in row_idx:
    for c in col_idx:
        if A[r, c] == 0:
            print(ans)
            exit()
print(ans - 1)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = map(int, input().split())
HW = []

A = np.zeros((H, W), dtype='bool')
for _ in range(M):
    h, w = map(int, input().split())
    h, w = h - 1, w - 1  # 0-indexed
    HW.append((h, w))
    A[h, w] = 1

arr = A.sum(axis=0)
col_max = arr.max()
col_idx = np.where(arr == col_max)[0]

arr = A.sum(axis=1)
row_max = arr.max()
row_idx = np.where(arr == row_max)[0]

# print(A.sum(axis=0).argsort()[-1])

ans = col_max + row_max
for r in row_idx:
    for c in col_idx:
        if A[r, c] == 0:
            print(ans)
            exit()
print(ans - 1)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = map(int, input().split())
HW = []
A = np.zeros((H, W), dtype='int')

print(1)
exit()

for _ in range(M):
    h, w = map(int, input().split())
    h, w = h - 1, w - 1  # 0-indexed
    HW.append((h, w))
    A[h, w] = 1

arr = A.sum(axis=0)
col_max = arr.max()
col_idx = np.where(arr == col_max)[0]

arr = A.sum(axis=1)
row_max = arr.max()
row_idx = np.where(arr == row_max)[0]

# print(A.sum(axis=0).argsort()[-1])

ans = col_max + row_max
for r in row_idx:
    for c in col_idx:
        if A[r, c] == 0:
            print(ans)
            exit()
print(ans - 1)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = map(int, input().split())
HW = []
A = np.zeros((H, W), dtype='int')

for _ in range(M):
    h, w = map(int, input().split())
    h, w = h - 1, w - 1  # 0-indexed
    HW.append((h, w))
    A[h, w] = 1

print(1)
exit()

arr = A.sum(axis=0)
col_max = arr.max()
col_idx = np.where(arr == col_max)[0]

arr = A.sum(axis=1)
row_max = arr.max()
row_idx = np.where(arr == row_max)[0]

# print(A.sum(axis=0).argsort()[-1])

ans = col_max + row_max
for r in row_idx:
    for c in col_idx:
        if A[r, c] == 0:
            print(ans)
            exit()
print(ans - 1)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = map(int, input().split())
HW = []
A = np.zeros((H, W), dtype='int')

for _ in range(M):
    h, w = map(int, input().split())
    h, w = h - 1, w - 1  # 0-indexed
    HW.append((h, w))
    A[h, w] = 1

arr = A.sum(axis=0)
col_max = arr.max()
col_idx = np.where(arr == col_max)[0]

arr = A.sum(axis=1)
row_max = arr.max()
row_idx = np.where(arr == row_max)[0]

# print(A.sum(axis=0).argsort()[-1])

print(1)
exit()

ans = col_max + row_max
for r in row_idx:
    for c in col_idx:
        if A[r, c] == 0:
            print(ans)
            exit()
print(ans - 1)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = map(int, input().split())
HW = []
A = np.zeros((H, W), dtype='int')

for _ in range(M):
    h, w = map(int, input().split())
    h, w = h - 1, w - 1  # 0-indexed
    HW.append((h, w))
    A[h, w] = 1

arr = A.sum(axis=0)
col_max = arr.max()
col_idx = np.where(arr == col_max)[0]

arr = A.sum(axis=1)
row_max = arr.max()
row_idx = np.where(arr == row_max)[0]

# print(A.sum(axis=0).argsort()[-1])

print(1)

ans = col_max + row_max
for r in row_idx:
    for c in col_idx:
        if A[r, c] == 0:
            print(ans)
            exit()
print(ans - 1)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = map(int, input().split())
HW = []
A = np.zeros((H, W), dtype='int')

for _ in range(M):
    h, w = map(int, input().split())
    h, w = h - 1, w - 1  # 0-indexed
    HW.append((h, w))
    A[h, w] = 1

arr = A.sum(axis=0)
col_max = arr.max()
col_idx = np.where(arr == col_max)[0]

arr = A.sum(axis=1)
row_max = arr.max()
row_idx = np.where(arr == row_max)[0]

# print(A.sum(axis=0).argsort()[-1])
if len(col_idx) * len(row_idx) >= M:
    print(col_max + row_max)
    exit()

ans = col_max + row_max
for r in row_idx:
    for c in col_idx:
        if A[r, c] == 0:
            print(ans)
            exit()
print(ans - 1)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = map(int, input().split())
HW = []
A = np.zeros((H, W), dtype='int')

for _ in range(M):
    h, w = map(int, input().split())
    h, w = h - 1, w - 1  # 0-indexed
    HW.append((h, w))
    A[h, w] = 1

arr = A.sum(axis=0)
col_max = arr.max()
col_idx = np.where(arr == col_max)[0]

arr = A.sum(axis=1)
row_max = arr.max()
row_idx = np.where(arr == row_max)[0]

# print(A.sum(axis=0).argsort()[-1])
if len(col_idx) * len(row_idx) >= M:
    print(col_max + row_max)
    exit()

for r in row_idx:
    for c in col_idx:
        if A[r, c] == 0:
            print(col_max + row_max)
            exit()
print(col_max + row_max - 1)

Submission status: Runtime Error

Solution code:
h,w,m=map(int,input().split())
hw=[[0]*w for i in range(h)]
for i in range(m):
    hi,wi=map(int,input().split())
    hw[hi-1][wi-1]=1    

ws=[0]*h
for i in range(h):
    ws[i]=0
    for j in range(w):
        ws[i]+=hw[i][j]
hs=[0]*w
for j in range(w):
    hs[j]=0
    for i in range(h):
        hs[j]+=hw[i][j]
        
hwmax=0
for i in range(h):
    for j in range(w):
        if hw[i][j]==1:
            x=ws[i]+hs[j]-1
        else:
            x=ws[i]+hs[j]
#        print(i,j,x)
        hwmax=max(hwmax,x)
        
print(hwmax)

Submission status: Runtime Error

Solution code:

import sys
input=sys.stdin.readline

h,w,m=map(int,input().split())
hw=[[0]*w for i in range(h)]
for i in range(m):
    hi,wi=map(int,input().split())
    hw[hi-1][wi-1]=1    

ws=[0]*h
for i in range(h):
    ws[i]=0
    for j in range(w):
        ws[i]+=hw[i][j]
hs=[0]*w
for j in range(w):
    hs[j]=0
    for i in range(h):
        hs[j]+=hw[i][j]
        
hwmax=0
for i in range(h):
    for j in range(w):
        if hw[i][j]==1:
            x=ws[i]+hs[j]-1
        else:
            x=ws[i]+hs[j]
#        print(i,j,x)
        hwmax=max(hwmax,x)
        
print(hwmax)

Submission status: Runtime Error

Solution code:
def main():
    H, W, M = list(map(int, input().split()))
    h = []
    w = []

    bombs = []
    for i in range(M):
        hi, wi = list(map(int, input().split()))
        h.append(hi)
        w.append(wi)
        bombs.append(tuple([hi,wi]))
    count_h = []
    count_w = []
    for i in range(M):
        count_h.append(sum(x==i for x in h))
        count_w.append(sum(x==i for x in w))

    max_h = max(count_h)
    indexes_h = [i for i, v in enumerate(count_h) if v == max(count_h)]

    max_w = max(count_w)
    indexes_w = [i for i, v in enumerate(count_w) if v == max(count_w)]

    ans = max_h + max_w
    if len(indexes_h) * len(indexes_w) > M:
        print(ans)
        exit()

	bombs = set(bombs)
    for i in indexes_h:
        for j in indexes_w:
            if (i, j) not in bombs:
                print(ans)
                exit()

    print(ans-1)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = map(int, input().split())
HW = []
A = np.zeros((H, W), dtype='int')

for _ in range(M):
    h, w = map(int, input().split())
    h, w = h - 1, w - 1  # 0-indexed
    HW.append((h, w))
    A[h, w] = 1

arr = A.sum(axis=0)
col_max = arr.max()
col_idx = np.where(arr == col_max)[0]

arr = A.sum(axis=1)
row_max = arr.max()
row_idx = np.where(arr == row_max)[0]

# print(A.sum(axis=0).argsort()[-1])

for r in row_idx:
    for c in col_idx:
        if A[r, c] == 0:
            print(col_max + row_max)
            exit()
print(col_max + row_max - 1)

Submission status: Runtime Error

Solution code:
def main():
    H,W,M = map(int,input().split())
    list = []
    dict_H = {key:0 for key in range(1,H+1,1)}
    dict_W = {key:0 for key in range(1,W+1,1)}
    for i in range(M):
        h,w = map(int,input().split())
        dict_H[h] += 1
        dict_W[w] += 1
        list.append((h,w))
    #print(list)
    maxH = max(dict_H.values())
    maxW = max(dict_W.values())
    wi = [i for i in range(1,W+1,1) if dict_H[i] == maxW]
    #print(retuH,gyoW,wi)
    for h in [i for i in range(1,H+1,1) if dict_W[i] == maxH]:
        for w in wi:
            if ((h,w) not in list):
                return maxH + maxW
    return maxH + maxW -1

print(main())

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = map(int, input().split())
HW = []
A = np.zeros((H, W), dtype='int')

for _ in range(M):
    h, w = map(int, input().split())
    h, w = h - 1, w - 1  # 0-indexed
    HW.append((h, w))
    A[h, w] = 1

arr = A.sum(axis=0)
col_max = arr.max()
col_idx = np.where(arr == col_max)[0]

arr = A.sum(axis=1)
row_max = arr.max()
row_idx = np.where(arr == row_max)[0]

# print(A.sum(axis=0).argsort()[-1])

for r in row_idx:
    for c in col_idx:
        if A[r, c] == 0:
            ans = col_max + row_max
            print(ans)
            exit()
ans = col_max + row_max - 1
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = map(int, input().split())
HW = []
A = np.zeros((H, W), dtype='int')

for _ in range(M):
    h, w = map(int, input().split())
    h, w = h - 1, w - 1
    HW.append((h, w))
    A[h, w] = 1
# print(f'{HW=}')
# print(f'{A=}')

arr = A.sum(axis=0)
col_idx = np.where(arr == arr.max())[0]
col_max = arr.max()

arr = A.sum(axis=1)
row_idx = np.where(arr == arr.max())[0]
row_max = arr.max()

# print(A.sum(axis=0).argsort()[-1])

for r in row_idx:
    for c in col_idx:
        if A[r, c] == 0:
            ans = col_max + row_max
            print(ans)
            exit()
ans = col_max + row_max - 1
print(ans)

Submission status: Runtime Error

Solution code:
H,W,M = map(int,input().split())
HW = []
for i in range(M):
	HW.append(list(map(int,input().split())))

#H方向の走査
Hmax = 0
Hmax_temp = 0
y_max = []
for y in range(1,H+1):
	for j in HW:
		if y == j[0]:
			Hmax_temp += 1
	if Hmax_temp >= Hmax:
		Hmax = Hmax_temp
		y_max.append(y)
	Hmax_temp = 0

#W方向の走査
Wmax = 0
Wmax_temp = 0
x_max = []
for x in range(1,W+1):
	for j in HW:
		if x == j[1]:
			Wmax_temp += 1
	if Wmax_temp >= Wmax:
		Wmax = Wmax_temp
		x_max.append(x)
	Wmax_temp = 0

#爆弾設置位置に対象が存在するか？
flag = False
for i in y_max:
	for  j in x_max:
		if [i,j] not in HW:
			flag = True
			break
	if flag:
		break

#答えのセット
if flag:
	ans = Wmax + Hmax
elif :
	ans = Wmax + Hmax - 1
print(ans)

Submission status: Runtime Error

Solution code:
h,w,m=map(int,input().split())

c=[0]*w
r=[0]*h
s=set()
for _ in range(m):
    i,j=map(int,input().split())
    i-=1
    j-=1
    r[i]+=1
    c[j]+=1
    s.add(i*w+j)

rmx=max(r)
cmx=max(c)
rc=[i for i in range(w) if r[i]==rmx]
cc=[j for j in range(h) if c[j]==cmx]

ans=rmx+cmx-1
ok=False
for i in rc:
    for j in cc:
        if i*w+j not in s:
            ans+=1
            ok=True
            break
    if ok:
        break

print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

def main()
    h,w,m = map(int,input().split())

    h_array = [ 0 for _ in range(h) ]
    w_array = [ 0 for _ in range(w) ]
    ps = set()
    for i in range(m):
        hi,wi = map( lambda x : int(x) - 1 , input().split() )
        h_array[hi] += 1
        w_array[wi] += 1
        ps.add( (hi,wi) )

    h_great = max(h_array)
    w_great = max(w_array)
    h_greats = list()
    w_greats = list()
    for i , hi in enumerate(h_array):
        if hi == h_great:
            h_greats.append(i)
    for i , wi in enumerate(w_array):
        if wi == w_great:
            w_greats.append(i)

    ans = h_great + w_great


    for _h in h_greats:
        for _w in w_greats:
            if (_h,_w) in ps:
                continue
            print(ans)
            exit()

    print(ans-1)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

def main()
    h,w,m = map(int,input().split())

    h_array = [ 0 for _ in range(h) ]
    w_array = [ 0 for _ in range(w) ]
    ps = set()
    for i in range(m):
        hi,wi = map( lambda x : int(x) - 1 , input().split() )
        h_array[hi] += 1
        w_array[wi] += 1
        ps.add( (hi,wi) )

    h_great = max(h_array)
    w_great = max(w_array)
    h_greats = list()
    w_greats = list()
    for i , hi in enumerate(h_array):
        if hi == h_great:
            h_greats.append(i)
    for i , wi in enumerate(w_array):
        if wi == w_great:
            w_greats.append(i)

    ans = h_great + w_great


    for _h in h_greats:
        for _w in w_greats:
            if (_h,_w) in ps:
                continue
            print(ans)
            exit()

    print(ans-1)

if __name__ == '__main__'
    main()

Submission status: Runtime Error

Solution code:
H, W, m = map(int, input().split())
xs = [[0] * W for _ in range(H)]
for _ in range(m):
    h, w = map(int, input().split())
    xs[h - 1][w - 1] += 1

mxx = [0] * H
for i in range(H):
    sm = 0
    for j in range(W):
        sm += xs[i][j]
    mxx[i] = sm
mxy = [0] * W
for i in range(W):
    sm = 0
    for j in range(H):
        sm += xs[j][i]
    mxy[i] = sm
ans = 0
for i in range(H):
    for j in range(W):
        ans = max(ans, mxx[i] + mxy[j] - xs[i][j])
print(ans)

Submission status: Runtime Error

Solution code:
H, W, m = map(int, input().split())
xs = [[0] * W for _ in range(H)]
for _ in range(m):
    h, w = map(int, input().split())
    xs[h - 1][w - 1] += 1

mxx = [0] * H
for i in range(H):
    mxx[i] = sum(xs[i])
mxy = [0] * W
for i in range(W):
    sm = 0
    for j in range(H):
        sm += xs[j][i]
    mxy[i] = sm
ans = 0
for i in range(H):
    for j in range(W):
        ans = max(ans, mxx[i] + mxy[j] - xs[i][j])
print(ans)

Submission status: Runtime Error

Solution code:
########### E
H, W, M = map(int, input().split())
bombs = []
for _ in range(M):
    bomb = list(map(int, input().split()))
    bombs.append(bomb)

H_list = [0]*H
W_list = [0]*W
sign = True
for i in range(M):
    H_list[bombs[i][0]-1] += 1
    W_list[bombs[i][1]-1] += 1

max_H = [i for i,k in enumerate(H_list) if k == max(H_list)]
max_W = [i for i,k in enumerate(W_list) if k == max(W_list)]

cross = []

for i in max_H:
    for k in max_W:
        if [i+1,k+1] not in bombs:
            sign = False
            print(max(H_list) + max(W_list))
            break
    break

if sign: