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:
# -*- coding: utf-8 -*-
import numpy as np
import sys
def input(): return sys.stdin.readline().strip()
from numba import jit

@jit
H, W, M = [int(x) for x in input().split()]
init = np.zeros((H,W))

for i in range(M):
  x, y = [int(x)-1 for x in input().split()]
  init[x,y] = 1

#print(init)

row = np.sum(init, axis=1)
col = np.sum(init, axis=0)

max_H = np.where(row == row.max())[0]
max_W = np.where(col == col.max())[0]
#print(max_H)
#print(max_W)

before_point = 0
max_point = row[max_H[0]] + col[max_W[0]]
#print(max_point)

if len(max_H) != 1 or len(max_W) != 1:
  for h in max_H:
    for w in max_W:
      point = row[h] + col[w]
      point -= init[h,w]
      if point > before_point:
        max_point = point
      before_point = point
else:
  for h in max_H:
    for w in max_W:
      max_point -= init[h,w]
  
print(int(max_point))

Submission status: Runtime Error

Solution code:
import copy
import sys
H, W, M= map(int,input().split())
height = [0]*H
width = [0]*W
object = []
for i in range(H):
   wi = [0]*W
   object.append(wi)
for s in sys.stdin.readlines():
    h, w = map(int, s.split())
    height[h-1] += 1
    width[w-1] += 1
    object[h-1][w-1] = 1
h_max = max(height)
w_max = max(width)
cand_w = []
cand_h = []
for i in range(W):
   if width[i] == w_max:
      cand_w.append(i)
for i in range(H):
   if height[i] == h_max:
      cand_h.append(i)
ans = h_max + w_max - 1
p = len(cand_h)*len(cand_w)
if M >= p:
   for i in range(len(cand_h)):
      for j in range(len(cand_w)):
         if object[cand_h[i]][cand_w[j]] == 0:
            ans = h_max + w_max            
else:
   ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
H, W, m = map(int, input().split())
hs = [0] * (H+1)
ws = [0] * (W+1)
fs = [[0]*(W+1) for i in range(H+1)]
for i in range(m):
    hi,wi = map(int, input().split())
    hs[hi-1] += 1
    ws[wi-1] += 1
    fs[hi-1][wi-1] += 1

N = 10**6
Hs = [h*N+i for i,h in enumerate(hs)]
Ws = [w*N+i for i,w in enumerate(ws)]
Hs.sort(reverse=True)
Ws.sort(reverse=True)
hmx = Hs[0]//N
wmx = Hs[0]//N
mx = 0
cnt = 0

Submission status: Runtime Error

Solution code:
H,W,M=map(int,input().split())
row_boms=[0 for i in range(H)]
col_boms=[0 for i in range(W)]

boms=[[0]*W for i in range(H)]
for i in range(M):
  h,w=map(int,input().split())
  boms[h-1][w-1]=1
  row_boms[h-1]+=1
  col_boms[w-1]+=1

row_max=max(row_boms)
col_max=max(col_boms)

rowmax_h=[]
colmax_w=[]
for i in range(H):
  if row_boms[i]==row_max:
    rowmax_h+=[i]
for i in range(W):
  if col_boms[i]==col_max:
    colmax_w+=[i]
rows=len(rowmax_h)
cols=len(colmax_w)
tf=0
for i in rowmax_h:
  for j in colmax_w:
    if boms[i][j]==0:
      tf=1
      break

if tf==1:
  print(row_max+col_max)
else:
  print(row_max+col_max-1)

Submission status: Runtime Error

Solution code:
import numpy as np
H,W,M=map(int, input().split())
hl=np.array([0 for i in range(H+1)])
wl=np.array([0 for i in range(W+1)])
ms=[]
for i in range(M):
    h,w=map(int, input().split())
    hl[h]+=1
    wl[w]+=1
    ms.append([h,w])
a=max(hl)
b=max(wl)
hmax=[]
wmax=[]
s=0
for x in ms:
    if  hl[x[0]]==a and wl[x[1]]==b:
        s+=1
c=hl.count(a)
d=wl.count(b)
if s<c*d:
    print(a+b)
else:
    print(a+b-1)

Submission status: Runtime Error

Solution code:
import numpy as np
H,W,M=map(int, input().split())
hl=np.array([0 for i in range(H+1)])
wl=np.array([0 for i in range(W+1)])
ms=[]
for i in range(M):
    h,w=map(int, input().split())
    hl[h]+=1
    wl[w]+=1
    ms.append([h,w])
a=max(hl)
b=max(wl)
hmax=[]
wmax=[]
s=0
for x in ms:
    if  hl[x[0]-1]==a and wl[x[1]-1]==b:
        s+=1
c=hl.count(a)
d=wl.count(b)
if s<c*d:
    print(a+b)
else:
    print(a+b-1)

Submission status: Runtime Error

Solution code:
H,W,M = map(int,input().split())
B = [[0]*W for i in range(H)]
B_H = [0]*H
B_W = [0]*W
for i in range(M):
    h,w = map(lambda x:int(x)-1,input().split())
    B[h][w] = 1
    B_H[h] += 1
    B_W[w] += 1

duplication = 1
max_H = max(B_H)
max_W = max(B_W)

can_H = []
can_W = []
for h in range(H):
    if B_H[h] == max_H:
        can_H.append(h)
for w in range(W):
    if B_W[w] == max_W:
        can_W.append(w)

for h in can_H:
    for w in can_W:
        duplication = B[h][w]
        if duplication == 0:
            break
    if duplication == 0:
        break

print(max_H + max_W - duplication)

Submission status: Runtime Error

Solution code:
h,w,m=map(int, input().split())
r = [0]*max(h,w)
c = [0]*max(h,w)
l=[]

for i in range(m):
    x,y=map(int, input().split())
    r[x-1]+=1
    c[y-1]+=1
    l.append([x,y])

cm = max(c)
rm = max(r)
ans = cm + rm

cnt = 0
#print(l,ci,ri)
for i in range(m):
    if r[l[i][0]-1] == rm and c[l[i][1]-1] ==cm:
        cnt += 1

if cnt == len(ci)*len(ri):
    print(int(ans)-1)
else:
    print(int(ans))

Submission status: Runtime Error

Solution code:
import numpy as np
h, w, n = map(int, input().split())
b_lists = []
for i in range(n):
   b_lists.append(list(map(int, list(input().split()))))

mx = np.zeros((h,w))
for b_list in b_lists:
  m_h = b_list[0] - 1
  m_w = b_list[1] - 1 
  mx[m_h, m_w] = 1

max_h = []
count_h_max = 0
max_w = []
count_w_max = 0


for i in range(h):
    icount = list(map(str, list(mx[i,:]))).count("1.0")
    if count_h_max <= icount:
      max_h.append(i)
      count_h_max = icount
for j in range(w):
    jcount = list(map(str, list(mx[:,j]))).count("1.0")
    if count_w_max <= jcount:
      max_w.append(j)
      count_w_max = jcount

sum = count_h_max + count_w_max
flag = False

for b_list in b_lists:
  m_h = b_list[0] - 1
  m_w = b_list[1] - 1 
  # print ("m_h = ", m_h)
  # print ("m_w = ", m_w)
  if m_h in max_h and m_w in max_w:
    flag = True
    break

#print ("max_h = ", count_h_max)
#print ("max_w = ", count_w_max)

if flag:
  print(sum-1)
else:
  print (sum)

Submission status: Runtime Error

Solution code:
import numpy as np

H, W, N = list(map(int, input().rstrip().split(' ')))

dist = np.zeros([H, W])

for _ in range(N):
    h, w = list(map(int, input().rstrip().split(' ')))
    dist[h - 1, w - 1] = 1
        
w_sum = dist.sum(axis=0)
h_sum = dist.sum(axis=1)

new_dist = w_sum.repeat(H).reshape(H, W) + h_sum.repeat(W).reshape(H, W) - dist

print(int(new_dist.max()))

Submission status: Runtime Error

Solution code:
h,w,m=map(int, input().split())
a=[0]*m
hc=[0]*w
wc=[0]*h
for i in range(m):
	x,y=map(int, input().split())
	a[i]=[x-1,y-1]
	hc[y-1]+=1
	wc[x-1]+=1
hcm=max(hc)
wcm=max(wc)
hk=[0]*h
wk=[0]*w
for i in range(len(hc)):
	if hc[i]==hcm:
		hk[i]=1
for i in range(len(wc)):
	if wc[i]==wcm:
		wk[i]=1
c=0
for i in range(len(a)):
	if a[i][0]==wk[i] and a[i][1]==hk[i]:
		c+=1
if c==len(hk)*len(wk):
	print(hcm+wcm-1)		
else:
	print(hcm+wcm)

Submission status: Runtime Error

Solution code:
import numpy as np
import itertools

def pd(s):
    #print(s)
    pass


# input
H, W, M = map(int, input().split())
ms = [
    [int(i) for i in input().split()]
    for m in range(M)
]
pd(f"{H=} {W=} {M=}")
pd(f"{ms=}")

rows = [0]*(H+1)
cols = [0]*(W+1)
bombs = np.zeros((H+1,W+1))

for m in ms:
    rows[m[0]] += 1
    cols[m[1]] += 1
    bombs[m[0]][m[1]] += 1


rows = np.array(rows)
max_rows = np.where(rows == rows.max())[0].tolist()
pd(f"{max_rows=}")

cols = np.array(cols)
max_cols = np.where(cols == cols.max())[0].tolist()
pd(f"{max_cols=}")

total_max = max(rows) + max(cols)


p = itertools.product(max_rows, max_cols)
for v in p:
    h = v[0]
    w = v[1]
    pd(f"{h=} {w=} {bombs[h][w]=}")
    if bombs[h][w] == 0:
        print(total_max)
        exit()

print(total_max-1)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
import sys
def input(): return sys.stdin.readline().strip()

H, W, M = [int(x) for x in input().split()]
init = np.zeros((H,W))

for i in range(M):
  x, y = [int(x)-1 for x in input().split()]
  init[x,y] = 1

#print(init)

row = np.sum(init, axis=1)
col = np.sum(init, axis=0)

max_H = np.where(row == row.max())[0]
max_W = np.where(col == col.max())[0]
#print(max_H)
#print(max_W)

before_point = 0
max_point = row[max_H[0]] + col[max_W[0]]
#print(max_point)

if len(max_H) != 1 or len(max_W) != 1:
  for h in max_H:
    for w in max_W:
      point = row[h] + col[w]
      point -= init[h,w]
      if point > before_point:
        max_point = point
      before_point = point
else:
  max_point -= init[h,w]
  

print(int(max_point))

Submission status: Runtime Error

Solution code:
H,W,M=map(int,input().split())
hlist=[]
wlist=[]
for i in range(M):
  h,w=map(int,input().split())
  hlist.append(h)
  wlist.append(w)
hmax=0
wmax=0
hi=0
wi=0
for i in range(1,H+1):
  a=hlist.count(i)
  if hmax<a:
    hmax=a
    hi=i
for i in range(1,W+1):
  b=hlist.count(i)
  if wmax<b:
    wmax=b
    wi=i
if hlist.index(hi)==wlist.index(wi):
  print(hmax+wmax-2)
else:
  print(hmax+wmax-1)

Submission status: Runtime Error

Solution code:
h, w, m = map(int, input().split())
lsh = [0] * h
lsw = [0] * w

bom = []
for _ in range(m):
    m1, m2 = map(int, input().split())
    lsh[m1 - 1] += 1
    lsw[m2 - 1] += 1
    bom.append((m1, m2))

lshmax = max(lsh)
lswmax = max(lsw)

for i in range(h)
    if lsh[i] == lshmax:
        for j in range(w):
            if lsw[j] == lswmax:
                if (i,j) in bom:
                    print(lshmax+lswmax-1)
                else:
                    print(lshmax+lswmax)

Submission status: Runtime Error

Solution code:
H,W,M = map(int,input().split())
B = [[0]*W for i in range(H)]
B_H = [0]*H
B_W = [0]*W
for i in range(M):
    h,w = map(lambda x:int(x)-1,input().split())
    B[h][w] = 1
    B_H[h] += 1
    B_W[w] += 1

finished = False
max_H = max(B_H)
max_W = max(B_W)
ans = 0

for h in range(H):
    if B_H[h] != max_H:
        continue
    for w in range(W):
        if B_W[w] != max_W:
            continue
        ans = max_H+max_W-B[h][w]
        if B[h][w] == 0:
            finished = True
            break
    if finished:
        break
print(ans)

Submission status: Runtime Error

Solution code:
def my_index(l, x):
    return [i for i, _x in enumerate(l) if _x == x]

H,W,M=map(int,input().split())
P=list()
P_X=[0]*(H+1)
P_Y=[0]*(W+1)
for m  in range(M):
    P.append(list(map(int,input().split())))
    P_X[P[m][0]]+=1
    P_Y[P[m][1]]+=1
Max_X=max(P_X)
Max_Y=max(P_Y)
m_count=0
for k in P:
    if  P_X[k[0]] == Max_X and P_Y[k [1]] == Max_Y:
        m_count += 1
if m_count==len(max_X)*len(max_Y):
    answer=Max_X+Max_Y-1
else:
     answer=Max_X+Max_Y
    
print(answer)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

H, W, M = [int(x) for x in input().split()]
init = np.zeros((H,W))

for i in range(M):
  x, y = [int(x)-1 for x in input().split()]
  init[x,y] = 1

#print(init)

row = np.sum(init, axis=1)
col = np.sum(init, axis=0)

max_H = np.where(row == row.max())[0]
max_W = np.where(col == col.max())[0]
#print(max_H)
#print(max_W)

before_point = 0
max_point = row[max_H[0]] + col[max_W[0]]
#print(max_point)

if len(max_H) != 1 or len(max_W) != 1:
  for h in max_H:
    for w in max_W:
      point = row[h] + col[w]
      point -= init[h,w]
      if point > before_point:
        max_point = point
      before_point = point
else:
  max_point -= init[h,w]
  

print(int(max_point))

Submission status: Runtime Error

Solution code:
import itertools
def my_index(l, x):
    return [i for i, _x in enumerate(l) if _x == x]

H,W,M=map(int,input().split())
P=list()
P_X=[0]*(H+1)
P_Y=[0]*(W+1)
for m  in range(M):
    P.append(list(map(int,input().split())))
    P_X[P[m][0]]+=1
    P_Y[P[m][1]]+=1
Max_X=max(P_X)
Max_Y=max(P_Y)
#max_X=my_index(P_X,Max_X)
#max_Y=my_index(P_Y,Max_Y)
m_count=0
for k in P:
    if  P_X[k[0]] == Max_X and P_Y[k [1]] == Max_Y:
        m_count += 1
if m_count==len(max_X)*len(max_Y):
    answer=Max_X+Max_Y-1
else:
     answer=Max_X+Max_Y
    
print(answer)

Submission status: Runtime Error

Solution code:
def main():
    H,W,M = map(int,input().split())
    h = [[0,i] for i in range(H)]
    w = [[0,i] for i in range(W)]
    X = []
    for i in range(M):
        tmp_0,tmp_1 = map(int,input().split())
        tmp_0 -= 1
        tmp_1 -= 1
        h[tmp_0][0] += 1
        w[tmp_1][0] += 1
        X.append([tmp_0,tmp_1])
    
    list.sort(h, reverse=True)
    list.sort(w, reverse=True)
    max_h = h[0][0]
    max_w = w[0][0]
    ans = max_h + max_w
    x0 = []
    x1 = []
    for i in range(M):
        if h[i][0] < max_h:
            h = h[:i]
            break
        x0.append(h[i][1])
        
    for i in range(M):
        if w[i][0] < max_w:
            w = w[:i]
            break
        x1.append(w[i][1])
    
    
    flag = True
    for i in x0:
        for j in x1:
            if [i,j] not in X:
                flag = False
    
    if flag:
        ans -= 1
    
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
H, W, M = map(int, sys.stdin.readline().split())
hlst = {}
wlst = {}
hwlst = []

for i in range(M):
    h, w = map(int, sys.stdin.readline().split())
    hlst[h] += 1
    wlst[w] += 1
    hwlst.append([h, w])

h = max(hlst)
w = max(wlst)
hmax = [i for i in range(H+1) if hlst[i] == h]
wmax = [i for i in range(W+1) if wlst[i] == w]
ans = h + w -1


for i in hmax:
    for j in wmax:
        if [i, j]  not in hwlst:
            ans += 1
            break

print(ans)

Submission status: Runtime Error

Solution code:
import copy
import sys
H, W, M= map(int,input().split())
height = [0]*H
width = [0]*W
object = []
for i in range(H):
   wi = [0]*W
   object.append(wi)
for s in sys.stdin.readlines():
    h, w = map(int, s.split())
    height[h-1] += 1
    width[w-1] += 1
    object[h-1][w-1]
h_max = max(height)
w_max = max(width)
cand_w = []
cand_h = []
for i in range(W):
   if width[i] == w_max:
      cand_w.append(i)
for i in range(H):
   if height[i] == h_max:
      cand_h.append(i)
ans = h_max + w_max - 1
p = len(cand_h)*len(cand_w)
if M >= p:
   for i in range(len(cand_h)):
      for j in range(len(cand_w)):
         if object[cand_h[i]][cand_w[j]] == 0:
            ans = h_max + w_max            
else:
   ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
H,W,M=map(int, input().split())
hl=np.array([0 for i in range(H+1)])
wl=np.array([0 for i in range(W+1)])
ms=[]
for i in range(M):
    h,w=map(int, input().split())
    hl[h]+=1
    wl[w]+=1
    ms.append([h,w])
a=max(hl)
b=max(wl)
hmax=[]
wmax=[]
for i in range(len(hl))l:
    if hl[i]==a:
        hmax.append(i)
for i in range(len(wl))l:
    if wl[i]==a:
        wmax.append(i)
# hmax=np.where(hl == hl.max())[0]
# wmax=np.where(wl == wl.max())[0]
bl=False
if len(hmax)*len(wmax)>M:
    bl=True
else:
    for x in hmax:
        for y in wmax:
            if [x,y] not in ms:
                bl=True
                break
        if bl:
            break
if bl:
    print(a+b)
else:
    print(a+b-1)

Submission status: Runtime Error

Solution code:
import numpy as np
h, w, n = map(int, input().split())
b_lists = []
for i in range(n):
   b_lists.append(list(map(int, list(input().split()))))

mx = np.zeros((h,w))
for b_list in b_lists:
  m_h = b_list[0] - 1
  m_w = b_list[1] - 1 
  mx[m_h, m_w] = 1

max_h = []
count_h_max = 0
max_w = []
count_w_max = 0


for i in range(h):
    icount = list(map(str, list(mx[i,:]))).count("1.0")
    if count_h_max <= icount:
      max_h.append(i)
      count_h_max = icount
for j in range(w):
    jcount = list(map(str, list(mx[:,j]))).count("1.0")
    if count_w_max <= jcount:
      max_w.append(j)
      count_w_max = jcount

sum = count_h_max + count_w_max
flag = True

for b_list in b_lists:
  m_h = b_list[0] - 1
  m_w = b_list[1] - 1 
  # print ("m_h = ", m_h)
  # print ("m_w = ", m_w)
  if not(m_h in max_h and m_w in max_w):
    flag = False
    break

# print ("max_h = ", max_h)
# print ("max_w = ", max_w)

if flag:
  print(sum-1)
else:
  print (sum)

Submission status: Runtime Error

Solution code:
from collections import Counter
H, W, M = [int(i) for i in input().split()]
g = [[0] * W for i in range(H)]
h = [0] * M
w = [0] * M
for i in range(M):
    h[i], w[i] = [int(j) for j in input().split()]
    g[h[i]-1][w[i]-1] = 1

ch = Counter(h)
cw = Counter(w)

hi = [i for i in ch.keys() if ch[i] == max(ch.values())]
wi = [i for i in cw.keys() if cw[i] == max(cw.values())]

for i in hi:
    for j in wi:
        if g[i-1][j-1] == 0:
            print(ch[i] + cw[j])
            exit()
print(ch[hi[0]] + cw[wi[0]] - 1)

Submission status: Runtime Error

Solution code:
import sys

h, w, m = map(int, input().split())
row = [0]*h
col = [0]*w
bomb = set()
for x in sys.stdin.readlines():
    H, W = map(int, input().split())
    bomb.add((H-1, W-1))
    row[H-1] += 1
    col[W-1] += 1

maxrow = max(row)
maxcol = max(col)
ans = maxcol + maxrow - 1
p, q = [], []
for i in range(h):
    if row[i] == maxrow:
        p.append(i)
for i in range(w):
    if col[i] == maxcol:
        q.append(i)

for i in p:
    for j in q:
        if (i, j) not in bomb:
            print(ans + 1)
            exit()
print(ans)

Submission status: Runtime Error

Solution code:
h, w, m = map(int, input().split())
boms = set()
Hs = [0] * h
Ws = [0] * w
for i in range(m):
  _h, _w = map(int, input().split())
  boms.add([_h - 1,_w - 1])
  Hs[_h - 1] += 1
  Ws[_w - 1] += 1
Hmax = max(Hs)
Hcount = Hs.count(Hmax)
Wmax = max(Ws)
Wcount = Ws.count(Wmax)
Hindexs = [0] * Hcount
Windexs = [0] * Wcount
hcount = 0
wcount = 0
for i in range(h):
  if Hs[i] == Hmax:
    Hindexs[hcount] = i
    hcount += 1
for i in range(w):
  if Ws[i] == Wmax:
    Windexs[wcount] = i
    wcount += 1
count = 0
for bom in boms:
  if (bom[0] in Hindexs) and (bom[1] in Windexs):
    count += 1
if count == len(Hindexs) * len(Windexs):
  print(Hmax + Wmax - 1)
else:
  print(Hmax + Wmax)

Submission status: Runtime Error

Solution code:
def main():
    H,W,M = map(int,input().split())
    h = [[0,i] for i in range(M)]
    w = [[0,i] for i in range(M)]
    X = []
    for i in range(M):
        tmp_0,tmp_1 = map(int,input().split())
        tmp_0 -= 1
        tmp_1 -= 1
        h[tmp_0][0] += 1
        w[tmp_1][0] += 1
        X.append([tmp_0,tmp_1])
    
    list.sort(h, reverse=True)
    list.sort(w, reverse=True)
    max_h = h[0][0]
    max_w = w[0][0]
    ans = max_h + max_w
    x0 = []
    x1 = []
    for i in range(M):
        if h[i][0] < max_h:
            h = h[:i]
            break
        x0.append(h[i][1])
        
    for i in range(M):
        if w[i][0] < max_w:
            w = w[:i]
            break
        x1.append(w[i][1])
    
    
    flag = True
    for i in x0:
        for j in x1:
            if [i,j] not in X:
                flag = False
    
    if flag:
        ans -= 1
    
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import collections
list_H = []
list_W = []
H,W,M = map(int,input().split())
for i in range(M):
    S1,S2 = map(int,input().split())
    list_H.append(S1)
    list_W.append(S2)
c1 = collections.Counter(list_H)
mode1 =c1.most_common()
list_mode1 = []
list_mode1.append(mode1[0])
j = 1
while True:
    if mode1[j-1][1]==mode1[j][1]:
        list_mode1.append(mode1[j])
        j+=1
    else:
        break
    if j==len(mode1):
        break
c2 = collections.Counter(list_W)
mode2 =c2.most_common()
list_mode2 = []
list_mode2.append(mode2[0])
k = 1
while True:
    if mode2[k-1][1]==mode2[k][1]:
        list_mode2.append(mode2[k])
        k+=1
    else:
        break
    if k==len(mode2):
        break
len_mode1 = len(list_mode1)
len_mode2 = len(list_mode2)
bomb_list = []
for i in range(len_mode1):
    for j in range(len_mode2):
        bomb_list.append([list_mode1[i][0],list_mode2[j][0]])
num = list_mode1[0][1]+list_mode2[0][1]
len_bomb = len(bomb_list)
flag = False
for k in range(len_bomb):
    for l in range(M):
        if bomb_list[k][0]==list_H[l] and bomb_list[k][1]==list_W[l]:
            break
        if l==M-1:
            flag = True
            print(num)
            break
    if flag == True:
        break
if flag ==False:
    print(num-1)

Submission status: Runtime Error

Solution code:
import numpy as np

m = np.zeros((3*10**4, 3*10**4))

Submission status: Runtime Error

Solution code:
import sys

h, w, m = map(int, input().split())
row = [0]*h
col = [0]*w
bomb = set()
for x in range(m):
    H, W = map(int, input().split())
    bomb.add([H-1, W-1])
    row[H-1] += 1
    col[W-1] += 1

maxrow = max(row)
maxcol = max(col)
ans = maxcol + maxrow - 1
p, q = [], []
for i in range(h):
    if row[i] == maxrow:
        p.append(i)
for i in range(w):
    if col[i] == maxcol:
        q.append(i)

for i in p:
    for j in q:
        if [i, j] not in bomb:
            print(ans + 1)
            exit()
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
from collections import deque

h, w = map(int, input().split())
ch, cw = map(int, input().split())
gh, gw = map(int, input().split())

s = []
for i in range(h):
   s.append(input().rstrip('\n'))

INF = 10 ** 10
d = [INF] * (h*w)

A = 10**18
B = 10**9

nex = []
for i in range(-2, 3):
    for j in range(-2, 3):
        if (i, j) in [(-1,0),(1,0),(0,-1),(0,1),(0,0)]:
            continue
        else:
            nex.append((i,j))

d[(ch-1) * w + (cw-1)] = 0

T = ((-1,0), (1,0), (0,1), (0,-1))

q = [(ch-1)*A + (cw-1)*B + 0]
use = [0] * (h * w)

for x in q:
    curh = (x // A)
    x -= curh * A
    curw = (x // B)
    cnt = x - curw * B
    tmp = curh * w + curw
    if d[tmp] < cnt or use[tmp]: continue
    d[tmp] = cnt
    use[tmp] = 1

    for dh, dw in T:
        nh = curh + dh
        nw = curw + dw
        if nh >= 0 and nh < h and nw >= 0 and nw < w and s[nh][nw] == '.' and d[nh * w + nw] > cnt:
            d[nh * w + nw] = cnt
            use[nh * w + nw] = 0
            q.append(nh*A + nw*B + cnt)
    for dh, dw in nex:
        nh = curh + dh
        nw = curw + dw
        if nh >= 0 and nh < h and nw >= 0 and nw < w and s[nh][nw] == '.' and d[nh * w + nw] > cnt:
            d[nh * w + nw] = cnt + 1
            use[nh * w + nw]
            q.append(nh*A + nw*B + cnt+1)

# for item in d:
#     print(item)
gh -= 1
gw -= 1

if d[gh*w + gw] == INF:
    print(-1)
else:
    print(d[gh*w + gw])

Submission status: Runtime Error

Solution code:
IN = iter(Input.split('\n')).__next__
def input():
    return IN()

import itertools
def my_index(l, x):
    return [i for i, _x in enumerate(l) if _x == x]

H,W,M=map(int,input().split())
P=list()
P_X=[0]*(H+1)
P_Y=[0]*(W+1)
for m  in range(M):
    P.append(list(map(int,input().split())))
    P_X[P[m][0]]+=1
    P_Y[P[m][1]]+=1
Max_X=max(P_X)
Max_Y=max(P_Y)
max_X=my_index(P_X,Max_X)
max_Y=my_index(P_Y,Max_Y)
m_count=0
for k in P:
    if  k[0] in max_X and k [1] in max_Y:
        m_count += 1
if m_count==len(max_X)*len(max_Y):
    answer=Max_X+Max_Y-1
else:
     answer=Max_X+Max_Y
    
print(answer)

Submission status: Runtime Error

Solution code:
# 初期入力
import sys
from collections import defaultdict
import numpy as np
input = sys.stdin.readline  #文字列では使わない
H,W,M = map(int, input().split())
bomb =np.zeros((H,W),np.int64)
posision =defaultdict(int)

for i in range(M):
    h,w = map(int, input().split())
    bomb[h-1,:] +=1
    bomb[:,w-1] +=1
    bomb[h-1,w-1] -=1
    posision[(h-1,w-1)] =1

#index =np.unravel_index(np.argmax(bomb), bomb.shape)
bom_max =np.max(bomb)
print(int(bom_max))

Submission status: Runtime Error

Solution code:
H,W,M=map(int,input().split())
BH=[0]*H
BW=[0]*W
B = [[0] * W for i in range(H)]
X=0
for i in range (M):
  X,Y=map(int,input().split())
  B[X-1][Y-1]=1
  BH[X-1]+=1
  BW[Y-1]+=1
for i in range (H):
  for ii in range (W):
    if B[i][ii]==1:
      XX=BH[i]+BW[ii]-1
    else:
      XX=BH[i]+BW[ii]
    if X<XX:
      X=XX
print(X)

Submission status: Runtime Error

Solution code:
def main():
    H,W,M = map(int,input().split())
    h = [[0,i] for i in range(M)]
    w = [[0,i] for i in range(M)]
    X = []
    for i in range(M):
        tmp_0,tmp_1 = map(int,input().split())
        tmp_0 -= 1
        tmp_1 -= 1
        h[tmp_0][0] += 1
        w[tmp_1][0] += 1
        X.append([tmp_0,tmp_1])
    
    list.sort(h, reverse=True)
    list.sort(w, reverse=True)
    max_h = h[0][0]
    max_w = w[0][0]
    ans = max_h + max_w
    x0 = []
    x1 = []
    for i in range(M):
        if h[i][0] < max_h:
            h = h[:i]
            break
        x0.append(h[i][1])
        
    for i in range(M):
        if w[i][0] < max_w:
            w = w[:i]
            break
        x1.append(w[i][1])
    
    
    flag = False
    for x in X:
        if (x[0] not in x0) or (x[1] not in x1):
            flag = True
            break
    
    if flag:
        ans -= 1
    
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, W, M = MAP()
L = [LIST() for i in range(M)]

yoko = [0] * W
tate = [0] * H
Map = [[0] * W for i in range(H)]

for h, w in L:
    Map[h-1][w-1] = 1

for h in range(H):
    tate[h] = sum(Map[h])

for w in range(W):
    for h in range(H):
        yoko[w] += Map[h][w]

Map2 = [[0] * W for i in range(H)]

ma = 0
for h in range(H):
    for w in range(W):
        if Map[h][w] == 1:
            Map2[h][w] = yoko[w] + tate[h] - 1
            ma = max(Map2[h][w], ma)
        else:
            Map2[h][w] = yoko[w] + tate[h]
            ma = max(Map2[h][w], ma)
print(ma)

Submission status: Runtime Error

Solution code:
import numpy as np

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

row = grid.sum(axis=0)
col = grid.sum(axis=1)

cm = col.max()
rm = row.max()
if grid[col==cm][:, row==rm].min() == 0:
    print(cm+rm)
else:
    print(cm+rm-1)

Submission status: Runtime Error

Solution code:
import sys
H, W, M = map(int, input().split())
hList = {i+1: 0 for i in range(H)}
wList = {i+1: 0 for i in range(W)}
pList = set()
#print(hList, wList, pList)
for _ in range(M):
  h, w = list(map(int, input().split()))
  #print(h, w)
  hList[h] += 1
  wList[w] += 1
  pList.add((h, w))
#print(hList, wList, pList)

maxHCount = max(hList.values())
maxHList = [h for h, count in hList.items() if count==maxHCount]


maxWCount = max(wList.values())
maxWList = [w for w, count in wList.items() if count==maxWCount]

#counterList = []
#for maxH in maxHList:
#  for maxW in maxWList:
#    counterList.append(hList[maxH] + wList[maxW] - (1 if ((maxH, maxW) in pList) else 0))
#print(max(counterList))
#print(max(((hList[maxH] + wList[maxW] - (1 if ((maxH, maxW) in pList) else 0)) for maxH in maxHList for maxW in maxWList)))
#print(max(((hList[maxH] + wList[maxW] - (1 if ((maxH, maxW) in pList) else 0)) for maxH in maxHList for maxW in maxWList)))

if M < len(maxHList)*len(maxWList):
	print(maxHCount + maxWList)
	sys.exit(0)

for maxH in maxHList:
  for maxW in maxWList:
    if (maxH, maxW) in pList:
      print(maxHCount + maxWList - 1)
      sys.exit(0)

print(maxHCount + maxWList)

Submission status: Runtime Error

Solution code:
h, w, m = map(int, input().split())
g = [[0] * w for i in range(h)]
hs = [0] * h
ws = [0] * w

for i in range(m):
  y, x = map(int, input().split())
  y -= 1
  x -= 1
  g[y][x] = 1
  hs[y] += 1
  ws[x] += 1

ans = 0
for i in range(h):
  for j in range(w):
    sum = hs[i] + ws[j]
    if g[i][j] == 1:
      sum -= 1
    ans = max(ans, sum)

print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
 
h,w,m = list(map(int,input().split()))

R = [0]*h
C = [0]*w
G = [[0]*w for _ in range(h)]

for _ in range(m):
  a,b = list(map(int,input().split()))
  a,b = a-1, b-1
  R[a] += 1
  C[b] += 1
  G[a][b] = 1
  
best = 0
for i in range(h):
  for j in range(w):
    best = max(best,R[i]+C[j] - G[i][j])
    
print(best)

Submission status: Runtime Error

Solution code:
import numpy as np

H, W, N = list(map(int, input().rstrip().split(' ')))

dist = np.zeros([H, W], dtype=int)

for _ in range(N):
    h, w = list(map(int, input().rstrip().split(' ')))
    dist[h - 1, w - 1] = 1
        
w_sum = dist.sum(axis=0)
h_sum = dist.sum(axis=1)

new_dist = np.tile(w_sum, H).reshape(H, W) + np.tile(h_sum, W).reshape(W, H).T - dist

print(int(new_dist.max()))

Submission status: Runtime Error

Solution code:
H, W, M = map(int, input().split())
S = [[0]*W for _ in range(H)]
lh = [0]*(H+1)
lw = [0]*(W+1)
l = []
for _ in range(M):
  h, w = map(int, input().split())
  lh[h] += 1
  lw[w] += 1
  l += [[h, w]]

maxh = max(lh)
maxw = max(lw)
cnt = 0

ch = lh.count(maxh)
cw = lw.count(maxw)
for y, x in l:
  if lh[y] == maxh and lw[x] == maxw:
    cnt += 1

ans = maxh+maxw
if cnt == ch*cw:
  ans -= 1

print(ans)

Submission status: Runtime Error

Solution code:
def main():

    h,w,m = tuple([int(t)-1 for t in input().split()])

    height = [0]*h
    width = [0]*w

    for _ in range(m):
        h_i,w_i = tuple([int(t)-1 for t in input().split()])
        height[h_i] += 1
        width[w_i] += 1

    print(max(height)+max(width))

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# 初期入力
import sys
from collections import defaultdict
import numpy as np
input = sys.stdin.readline  #文字列では使わない
H,W,M = map(int, input().split())
bomb =np.zeros((H,W))
posision =defaultdict(int)

for i in range(M):
    h,w = map(int, input().split())
    bomb[h-1,:] +=1
    bomb[:,w-1] +=1
    bomb[h-1,w-1] -=1
    posision[(h-1,w-1)] =1

#index =np.unravel_index(np.argmax(bomb), bomb.shape)
bom_max =np.max(bomb)
print(int(bom_max))