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:
from collections import Counter
from collections import defaultdict
from collections import deque
from heapq import heapify
from heapq import heappop
from heapq import heappush


h,w,m=[int(x) for x in input().split()]

#defaultdictにすることにより後に使うmax関数のコストを削減
hcnt=defaultdict(int)
wcnt=defaultdict(int)
bomb=[]
#O(M)
for _ in range(m):
    hi,wi=[int(x)-1 for x in input().split()]
    hcnt[hi]+=1
    wcnt[wi]+=1
    bomb.add([hi,wi])

#O(M)
hmax=max(hcnt.values())
wmax=max(wcnt.values())

#O(M)
hidx=[x for x,y in hcnt.items() if y==hmax]
widx=[x for x,y in wcnt.items() if y==wmax]

#O(M**2*)
ofst=-1
for i in hidx:
    for j in widx:
        if not [i,j] in bomb:
            ofst=0
            break
                
print(hmax+wmax+ofst)

Submission status: Runtime Error

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

HL, WL = [0]*H, [0]*W
B = []

for _ in range(M):
  h, w = map(lambda x:int(x)-1, input().split())
  B.append((h, w))
  HL[h] += 1
  WL[w] += 1

HM, WM = max(HL), max(WL)
K = HL.count(HM) * WM.count(WL)

cnt = 0
for h, w in B:
  if HL[h] == HM and WL[w] == WM:
    cnt += 1

if K > cnt:
  print(HM+WM)
else:
  print(HM+WM-1)

Submission status: Runtime Error

Solution code:
import numpy as np
h,w,m=map(int,input().split())
ls=[[0]*w for _ in range(h)]
 
lh,lw=[0]*h,[0]*w
for i in range(m):
    l1=list(map(int,input().split()))
    lh[l1[0]-1]+=1
    lw[l1[1]-1]+=1
    ls[l1[0]-1][l1[1]-1]+=1

lhm=max(lh)
lhml=[i for i, x in enumerate(lh) if x == lhm]
lwm=max(lw)
lwml=[i for i, x in enumerate(lw) if x == lwm]

ll1=[]
for i in range(len(lhml)):
    ll1.append(ls[lhml[i]])

lwh2=np.array(ll1).T.tolist()

for i in range(len(lwml)):
    if 0 in lwh2[lwml[i]]:
        print(lhm+lwm)
        exit()
print(lhm+lwm-1)

Submission status: Runtime Error

Solution code:
import numpy as np
h,w,m=map(int,input().split())
ls=[[0]*w for _ in range(h)]
 
lh,lw=[0]*h,[0]*w
for i in range(m):
    l1=list(map(int,input().split()))
    lh[l1[0]-1]+=1
    lw[l1[1]-1]+=1
    ls[l[i][0]-1][l[i][1]-1]+=1

lhm=max(lh)
lhml=[i for i, x in enumerate(lh) if x == lhm]
lwm=max(lw)
lwml=[i for i, x in enumerate(lw) if x == lwm]

ll1=[]
for i in range(len(lhml)):
    ll1.append(ls[lhml[i]])

lwh2=np.array(ll1).T.tolist()

for i in range(len(lwml)):
    if 0 in lwh2[lwml[i]]:
        print(lhm+lwm)
        exit()
print(lhm+lwm-1)

Submission status: Runtime Error

Solution code:
h,w,m = list(map(int,input().split()))
bomb = [[0]*w for _ in range(h)]
place = [0]*m
for i in range(m):
    x,y = list(map(int,input().split()))
    x -= 1; y -= 1
    bomb[x][y] = 1
    place[i] = [x,y]

hsum = [sum(i) for i in bomb]
wsum = [sum([bomb[j][i] for j in range(h)]) for i in range(w)]
hmax = max(hsum); wmax = max(wsum)

hsearch = set(); wsearch = set()
for i in range(m):
    x,y = place[i]
    if hsum[x] == hmax: hsearch.add(x)
    if wsum[y] == wmax: wsearch.add(y)

ans = hmax+wmax
flag = False
for i in hsearch:
    for j in wsearch:
        if bomb[i][j] == 0:
            flag = True
            break
    if flag:
        break    

print(ans if flag else ans-1)

Submission status: Runtime Error

Solution code:
H,W,M = map(int,input().split())
a = [[0]*W for i in range(H)]
hh = [0] *H
ww = [0] * W
for i in range(M):
    h,w = (map(int, input().split()))
    hh[h-1] += 1
    ww[w-1] += 1
    a[h-1][w-1] = 1
Maxh = 0
Maxw = 0
argh = 0
argw = 0
for h in range(H):
    if Maxh <= hh[h]:
        argh = h
        Maxh = hh[h]

for w in range(W):
    if Maxw <= ww[w]:
        argw = w
        Maxw = ww[w]
print(Maxw+Maxh-a[argh][argw])

Submission status: Runtime Error

Solution code:
H,W,M = map(int,input().split())
a = [[0]*W for i in range(H)]
hh = [0] *H
ww = [0] * W
for i in range(M):
    h,w = (map(lambda a: int(a)-1, input().split()))
    hh[h] += 1
    ww[w] += 1
    a[h][w] = 1
Maxh = 0
Maxw = 0
argh = 0
argw = 0
for h in range(H):
    if Maxh <= hh[h]:
        argh = h
        Maxh = hh[h]

for w in range(W):
    if Maxw <= ww[w]:
        argw = w
        Maxw = ww[w]
print(Maxw+Maxh-a[argh][argw])

Submission status: Runtime Error

Solution code:
H,W,M = map(int,input().split())
a = [[0]*W for i in range(H)]
hh = [0] *H
ww = [0] * W
for i in range(M):
    h,w = (map(lambda a: int(a)-1, input().split()))
    hh[h] += 1
    ww[w] += 1
    a[h][w] = 1
Maxh = 0
Maxw = 0
argh = 0
argw = 0
for h in range(H):
    if Maxh <= hh[h]:
        argh = h
        Maxh = hh[h]

for w in range(W):
    if Maxw <= ww[w]:
        argw = w
        Maxw = hh[w]
print(Maxw+Maxh-a[argh][argw])

Submission status: Runtime Error

Solution code:
h,w,m = (int(x) for x in input().split())

index = [0]*h*w

h_total = []
w_total = []
chk_flag = 1

for i in range(m):
    l = input()
    a = l.split(' ')

    index[(int(a[1])-1)*h+int(a[0])-1] = 1

for i in range(h):
    temp = 0
    for j in range(w):
        temp += index[h*j+i]
    h_total.append(temp)

for i in range(w):
    temp = 0
    for j in range(h):
        temp += index[h*i+j]
    w_total.append(temp)



h_max = [i for i, x in enumerate(h_total) if x == max(h_total)]
w_max = [i for i, x in enumerate(w_total) if x == max(w_total)]


for i in range(len(h_max)):
    for j in range(len(w_max)):
        if(index[w_max[j]*h + h_max[i]] == 0):
            chk_flag = 0
            break

print(max(h_total) + max(w_total) - chk_flag)

Submission status: Runtime Error

Solution code:
h,w,m = map(int, input().split())
lw= [0 for i in range(w+1)]
lh= [0 for i in range(h+1)]
lm= list()
for i in range(m):
  l =list(map(int, input().split()))
  lh[l[0]] += 1
  lw[l[1]] += 1
  lm.append(l)
hmax = max(lh)
wmax = max(lw)

flg = 0
for hh in range(1,h+1):
  if lh[hh] == hmax:
    for ww in range(1,h+1):
      if lw[ww] == wmax:
        for ii in range(len(lm)):
          if lm[ii][1] == ww:
            if lm[ii][0] == hh:
              flg = -1
print(hmax + wmax + flg)

Submission status: Runtime Error

Solution code:
h,w,m = map(int, input().split())
lw= [0 for i in range(w+1)]
lh= [0 for i in range(h+1)]
lm= list()
for i in range(m):
  l =list(map(int, input().split()))
  lh[l[0]] += 1
  lw[l[1]] += 1
  lm.append(l)
hmax = max(lh)
wmax = max(lw)

ttl = 0
for hh in range(1,h+1):
  if lh[hh] == hmax:
    cnth = hmax
    for ww in range(1,h+1):
      if lw[ww] == wmax:
        cntw = wmax
        for ii in range(len(lm)):
          if lm[ii][1] == ww:
            if lm[ii][0] == hh:
              cntw = wmax -1
        if ttl < hmax + cntw:
          ttl = hmax + cntw
print(ttl)

Submission status: Runtime Error

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

h,w,m = inpl()
yd = []
for i in range(h):
    yd.append([i,0])
xd = []
for i in range(w):
    xd.append([i,0])
s = [False] * (w*h)
def ind(i,j):
    return i*w + j

for _ in range(m):
    a,b = inpl()
    yd[a-1][1] += 1
    xd[b-1][1] += 1
    s[ind(a-1,b-1)] = True
yd.sort(key = lambda x:x[1], reverse = True)
xd.sort(key = lambda x:x[1], reverse = True)
ymax = yd[0][1]
yl = []
for i,x in yd:
    if x != ymax: break
    yl.append(i)
xmax = xd[0][1]
xl = []
for i,x in xd:
    if x != xmax: break
    xl.append(i)
res = ymax + xmax - 1
# print(yl,xl,res)
for i in yl:
    for j in xl:
        if not s[ind(i,j)]:
            res += 1
            break
print(res)

Submission status: Runtime Error

Solution code:
h,w,m = map(int, input().split())
lw= [0 for i in range(w+1)]
lh= [0 for i in range(h+1)]
lm= list()
for i in range(m):
  l =list(map(int, input().split()))
  lh[l[0]] += 1
  lw[l[1]] += 1
  lm.append(l)
hmax = max(lh)
wmax = max(lw)
#print('lh',lh,hmax)
#print('lw',lw,wmax)
#print(lm)

ttl = 0
for hh in range(1,h+1):
  if lh[hh] == hmax:
    cnth = hmax
    #print('hh',hh)
    for ww in range(1,h+1):
      if lw[ww] == wmax:
        #print('ww',ww)
        cntw = wmax
        for ii in range(len(lm)):
          if lm[ii][1] == ww:
            #print(' ',lm[ii][0],lm[ii][1])
            if lm[ii][0] == hh:
              #print('X',lm[ii][0],lm[ii][1],hh)
              cntw = wmax -1
        #print(cntw)
        if ttl < hmax + cntw:
          ttl = hmax + cntw
print(ttl)

Submission status: Runtime Error

Solution code:
h,w,m = (int(x) for x in input().split())

index = [0]*h*w

h_total = []
w_total = []
chk_flag = 0

for i in range(m):
    l = input()
    a = l.split(' ')

    index[(int(a[1])-1)*h+int(a[0])-1] = 1

for i in range(h):
    temp = 0
    for j in range(w):
        temp += index[h*j+i]
    h_total.append(temp)

for i in range(w):
    temp = 0
    for j in range(h):
        temp += index[h*i+j]
    w_total.append(temp)



h_max = [i for i, x in enumerate(h_total) if x == max(h_total)]
w_max = [i for i, x in enumerate(w_total) if x == max(w_total)]


for i in range(len(h_max)):
    for j in range(len(w_max)):
        if(index[h_max[i]*h + w_max[j]] == 1):
            chk_flag = 1
            break

print(max(h_total) + max(w_total) - chk_flag)

Submission status: Runtime Error

Solution code:
from collections import deque
H, W = map(int, input().split())
CY, CX = map(lambda x: int(x)-1, input().split())
DY, DX = map(lambda x: int(x)-1, input().split())

gridgraph = [input() for _ in range(H)]
q = deque()
q.append([CY, CX])
# 更新されるなら追加
adjacent = [[1, 0], [0, 1], [-1, 0], [0, -1]]
INF = float("inf")
distant = [[INF]*W for _ in range(H)]
distant[CY][CX] = 0
visited = set()
while len(q):
    cy, cx = q.popleft()
    if (cy, cx) in visited:
        continue
    visited.add((cy, cx))
    for dy, dx in adjacent:
        ny, nx = cy+dy, cx+dx
        if ny < 0 or ny >= H or nx < 0 or nx >= W:
            continue
        if gridgraph[ny][nx] == "." and distant[ny][nx] > distant[cy][cx]:
            distant[ny][nx] = distant[cy][cx]
            q.appendleft([ny, nx])
    for i in range(-2, 3, 1):
        for j in range(-2, 3, 1):
            target_y, target_x = cy+i, cx+j
            if target_y < 0 or target_y >= H or target_x < 0 or target_x >= W:
                continue
            if gridgraph[target_y][target_x] == "." and distant[target_y][target_x] > distant[cy][cx]+1:
                distant[target_y][target_x] = distant[cy][cx]+1
                q.append([target_y, target_x])
if distant[DY][DX] == INF:
    print(-1)
else:
    print(distant[DY][DX])

Submission status: Runtime Error

Solution code:
H,W,M=map(int,input().split())
dicti={}
dictj={}
bomb=[]
for i in range(M):
  h,w=map(int,input().split())
  bomb.append((h,w))
  if h in dicti:
    dicti[h]+=1
  if h not in dicti:
    dicti[h]=1
  if w in dictj:
    dictj[w]+=1
  if w not in dictj:
    dictj[w]=1
LI=sorted(dicti.items(),key=lambda x:x[1],reverse=True)
LJ=sorted(dictj.items(),key=lambda x:x[1],reverse=True)
Imax=LI[0][1]
Jmax=LJ[0][1]
I=0
J=0
IL=[]
for i in range(len(LI)):
  if LI[i][1]==Imax:
    I+=1
for j in range(len(LJ)):
  if LJ[i][1]==Jmax:
    J+=1
pos=0
for i in range(M):
  if dicti[bomb[i][0]]==Imax and dictj[bomb[i][0]]==Jmax:
    pos+=1
if pos==I*J:
  print(Imax+Jmax)
else:
  print(Imax+Jmax-1)

Submission status: Runtime Error

Solution code:
#E
import numpy as np

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

num = np.zeros((h,w))

for i in range(m):
    a,b = map(int,input().split())
    num[a-1,b-1] = 1

Sum_row = np.sum(num , axis = 1)
point1  = np.argmax(Sum_row)
Max_row = Sum_row[point1]

num[point1] = 0

Sum_line = np.sum(num , axis = 0)
Max_line = np.max(Sum_line)

print(int(Max_row+Max_line))

Submission status: Runtime Error

Solution code:
h,w,m = (int(x) for x in input().split())

index = [0]*h*w

h_total = []
w_total = []
chk_sum = []

for i in range(m):
    l = input()
    a = l.split(' ')

    index[(int(a[1])-1)*h+int(a[0])-1] = 1

for i in range(h):
    temp = 0
    for j in range(w):
        temp += index[h*j+i]
    h_total.append(temp)

for i in range(w):
    temp = 0
    for j in range(h):
        temp += index[h*i+j]
    w_total.append(temp)



h_max = [i for i, x in enumerate(h_total) if x == max(h_total)]
w_max = [i for i, x in enumerate(w_total) if x == max(w_total)]


for i in range(len(h_max)):
    for j in range(len(w_max)):
        chk_sum.append(h_total[h_max[i]] + w_total[w_max[j]] - index[h_max[i]*h + w_max[j]])

print(max(chk_sum))

Submission status: Runtime Error

Solution code:
import numpy as np
H,W,M = map(int,input().split())
R = {}
C = {}
RC = []
for i in range(M):
  h,w = map(int,input().split())
  RC.append([h,w])
  if h in R:
    R[h] += 1
  else:
    R[h] = 1
  if w in C:
    C[w] += 1
  else:
    C[w] = 1
MaxR = max(R.values())
rlist = [k for k, v in R.items() if v == MaxR]
MaxC = max(C.values())
clist = [k for k, v in C.items() if v == MaxC]
for i in rlist:
  for j in clist:
    if [i,j] not in RC:
      print(MaxR+MaxC)
      break
  else:
    print(MaxR+MaxC-1)
    break

Submission status: Runtime Error

Solution code:
#E
import numpy as np

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

num = np.zeros((h,w))

for i in range(m):
    a,b = map(int,input().split())
    num[a-1,b-1] = 1

Sum_row = (np.sum(num , axis = 1))
point1  = np.argmax(Sum_row ,axis = 1 )
Max_row = max(Sum_row)

num[point1] = 0

Sum_line = (np.sum(num , axis = 0))
Max_line = max(Sum_line)

print(int(Max_row+Max_line))

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 s in sys.stdin.readlines():
    h, w = map(int, s.split())
    height[h-1] += 1
    width[w-1] += 1
    object.append([h-1, w-1])
object = set(object)
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 not [cand_h[i], cand_w[j]] in object:
            ans = h_max + w_max
            break
            break

else:
   ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
#E
import numpy as np

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

num = np.zeros((h,w))

for i in range(m):
    a,b = map(int,input().split())
    num[a-1,b-1] = 1

Sum_row = (np.sum(num , axis = 1))
point1  = np.argmax(Sum_row)
Max_row = max(Sum_row)

num[point1] = 0

Sum_line = (np.sum(num , axis = 0))
Max_line = max(Sum_line)

print(int(Max_row+Max_line))

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 s in sys.stdin.readlines():
    h, w = map(int, s.split())
    height[h-1] += 1
    width[w-1] += 1
    object.append([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
c = 0
p = len(cand_h)*len(cand_w)
if M >= p
for i in range(len(object)):
   if object[i][0] in cand_h and object[i][1] in cand_w:
      c += 1
print(ans - (c == p))

Submission status: Runtime Error

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
h,w,m=map(int,input().split())
l={}
u={}
hw=[list(map(int,input().split())) for _ in range(m)]
s=set()

for i in range(m):
    hr,wr=hw[i]
    if hr-1 in u:
        u[hr-1]+=1
    else:
        u[hr-1]=1
    if wr-1 in l:
        l[wr-1]+=1
    else:
        l[wr-1]=1
    s.add((hr-1)*w+wr-1)
resl=0
resu=0
cl=[]
cu=[]
for i in l:
    if resl<l[i]:
        resl=l[i]
        cl=[i]
    elif resl==l[i]:
        cl.append(i)
for i in u:
    if resu<u[i]:
        resu=u[i]
        cu=[i]
    elif resu==u[i]:
        cu.append(i)
ans=0

if len(cu)*len(cl)>=m:
    ans=resl+resu
else:
    for i in cu:
        for j in cl:
            if i*w+j in s:
                res=l[j]+u[i]-1
            else:
                res=l[i]+u[j]
            ans=max(res,ans)
print(ans)

Submission status: Runtime Error

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
h,w,m=map(int,input().split())
l={}
u={}
hw=[list(map(int,input().split())) for _ in range(m)]
s=set()

for i in range(m):
    hr,wr=hw[i]
    if hr-1 in u:
        u[hr-1]+=1
    else:
        u[hr-1]=1
    if wr-1 in l:
        l[wr-1]+=1
    else:
        l[wr-1]=1
    s.add((hr-1)*w+wr-1)
resl=0
resu=0
cl=[]
cu=[]
for i in l:
    if resl<l[i]:
        resl=l[i]
        cl=[i]
    elif resl==l[i]:
        cl.append(i)
for i in u:
    if resu<u[i]:
        resu=u[i]
        cu=[i]
    elif resu==u[i]:
        cu.append(i)
ans=0

if len(cu)*len(cl)>=m:
    ans=resl+resu
else:
    for i in cu:
        for j in cl:
            print(i,j)
            if i*w+j in s:
                res=l[j]+u[i]-1
            else:
                res=l[i]+u[j]
            print(res)
            ans=max(res,ans)
print(ans)

Submission status: Runtime Error

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

hBom = [0]*H
wBom = [0]*W

# 各行、各列の爆破対象をカウント
for h, w in bom:
    hBom[h-1] += 1
    wBom[w-1] += 1

# 爆破対象の最大数と最大になる行と列の番号を調べる
maxHBom = max(hBom)
maxWBom = max(wBom)
maxHBomIndex = [index for index, h in enumerate(hBom) if h == maxHBom]
maxWBomIndex = [index for index, w in enumerate(wBom) if w == maxWBom]

# 爆破対象が最大になる行と列の組み合わせについて、
# 爆弾を配置する場所が爆破対象であるかないかで場合分け
for h in maxHBomIndex:
    for w in maxWBomIndex:
        if [h+1, w+1] not in set(bom):
            print(hBom[h] + wBom[w])
            exit()
else:
    print(hBom[h] + wBom[w] - 1)

Submission status: Runtime Error

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
h,w,m=map(int,input().split())
l={}
u={}
hw=[list(map(int,input().split())) for _ in range(m)]
sl=set()
su=set()

for i in range(m):
    hr,wr=hw[i]
    if hr-1 in u:
        u[hr-1]+=1
    else:
        u[hr-1]=1
    if wr-1 in l:
        l[wr-1]+=1
    else:
        l[wr-1]=1
    sl.add(wr-1)
    su.add(hr-1)
resl=0
resu=0
cl=[]
cu=[]
for i in l:
    if resl<l[i]:
        resl=l[i]
        cl=[i]
    elif resl==l[i]:
        cl.append(i)
for i in u:
    if resu<u[i]:
        resu=u[i]
        cu=[i]
    elif resl==l[i]:
        cu.append(i)
ans=0
if len(cu)*len(cl)>=m:
    ans=resl+resu
else:
    for i in cu:
        for j in cl:
            if i in su and j in sl:
                res=l[j]+u[i]-1
            else:
                res=l[i]+u[j]
            ans=max(res,ans)
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

H,W,M = map(int,input().split())
target = np.zeros(shape=(M,2))

for i in range(M):
    target[i] = list(map(int,input().split()))

target -= 1

def count(array):
    ans = []
    array_sort = np.sort(array)
    tmp = array_sort[0]
    c = 1
    for i in range(1,len(array_sort)):
        if array_sort[i] != tmp:
            ans.append([int(tmp),c])
            c = 1
        else:
            c += 1
        tmp = array_sort[i]
        if i ==len(array_sort)-1:
            ans.append([int(tmp),c])
    return np.array(ans)

def judge(i,j):
    for t in target:
        if t[0]==i and t[1]==j:
            return True
    return False

h_count = count(target[:,0])
w_count = count(target[:,1])

i = h_count[np.argmax(h_count[:,1]), 0]
col_idx = w_count[w_count[:,1]==w_count[:,1].max() , 0]


j = col_idx[0]
for j_ in col_idx:
    if judge(i,j_)==False:
        j = j_
        break
    
ans = h_count[:,1].max()+ w_count[:,1].max()

if judge(i,j):
    ans -=1

print(int(ans))

Submission status: Runtime Error

Solution code:
H,W,M = map(int,input().split())
l = [[0]*W for _ in range(H)]
tate,yoko = [0]*W,[0]*H
ans = 0
memo = set([])
for _ in range(M):
  h,w = map(int,input().split())
  h -= 1
  w -= 1
  tate[w] += 1
  yoko[h] += 1
  l[h][w] = 1
  memo.add((h,w))
tateArr,yokoArr = [],[]
ans = 0
t,y = max(tate),max(yoko)
for i in range(W):
  if tate[i] == t:
    tateArr.append(i)
for i in range(H):
  if yoko[i] == y:
    yokoArr.append(i)
for i in yokoArr:
  for j in tateArr:
    if (i,j) not in memo:
      print(t+y)
      exit()

print(t+y-1)

Submission status: Runtime Error

Solution code:
H,W,M = map(int,input().split())
l = [[0]*W for _ in range(H)]
tate,yoko = [0]*W,[0]*H
ans = 0
memo = []
for _ in range(M):
  h,w = map(int,input().split())
  h -= 1
  w -= 1
  tate[w] += 1
  yoko[h] += 1
  l[h][w] = 1
  memo.append([h,w])
tateArr,yokoArr = [],[]
ans = 0
t,y = max(tate),max(yoko)
for i in range(W):
  if tate[i] == t:
    tateArr.append(i)
for i in range(H):
  if yoko[i] == y:
    yokoArr.append(i)
for i in yokoArr:
  for j in tateArr:
    if [i,j] in memo:
      pass
    else:
      print(t+y)
      exit()
print(t+y-1)

Submission status: Runtime Error

Solution code:

def main():
	h,w,m=map(int,input().split())
	lst=[[0]*w for i in range(h)]
	by_row=[0 for i in range(h)]
	by_col=[0 for i in range(w)]
	for i in range(m):
		p,q,=map(int,input().split())
		lst[p-1][q-1]=1
		by_row[p-1]+=1
		by_col[q-1]+=1
	row_max=max(by_row)
	col_max=max(by_col)

	rc=[x for x,count in enumerate(by_row) if count==row_max]
	cc=[y for y,count in enumerate(by_col) if count==row_col]
	for x in  rc:
		for y in cc:
			if lst[x][y]:
				continue
			else:
				print(row_max+col_max)
				return
	print(row_max+col_max-1)
	return

if __name__=='__main__':
	main()

Submission status: Runtime Error

Solution code:
H, W, M = map(int, input().split())
h_list = [0 for _ in range(H + 1)]
w_list = [0 for _ in range(W + 1)]
bomb = set()
for _ in range(M):
    h, w = map(int, input().split())
    h_list[h] += 1
    w_list[w] += 1
    bomb.add([h, w])
h_max = max(h_list)
w_max = max(w_list)
h_index = []
w_index = []
for i in range(H + 1):
    if h_list[i] == h_max:
        h_index.append(i)
for i in range(W + 1):
    if w_list[i] == w_max:
        w_index.append(i)
tmp = -1        
for h in h_index:
    for w in w_index:
        if [h, w] not in bomb:
            tmp = 0
            break
print(h_max + w_max + tmp)

Submission status: Runtime Error

Solution code:
H,W,M = map(int,input().split())
l = [[0]*W for _ in range(H)]
tate,yoko = [0]*W,[0]*H
ans = 0
for _ in range(M):
  h,w = map(int,input().split())
  h -= 1
  w -= 1
  tate[w] += 1
  yoko[h] += 1
  l[h][w] = 1
tateArr,yokoArr = [],[]
ans = 0
t,y = max(tate),max(yoko)
for i in range(W):
  if tate[i] == t:
    tateArr.append(i)
for i in range(H):
  if yoko[i] == y:
    yokoArr.append(i)
for i in yokoArr:
  for j in tateArr:
    if l[i][j] == 0:
      print(t+y)
      exit()
print(t+y-1)

Submission status: Runtime Error

Solution code:
H,W,M = map(int,input().split())
l = [[0]*W for _ in range(H)]
tate,yoko = [0]*W,[0]*H
memo = []
for _ in range(M):
  h,w = map(int,input().split())
  h -= 1
  w -= 1
  tate[w] += 1
  yoko[h] += 1
  l[h][w] = 1
  memo.append([h,w])
tateArr,yokoArr = [],[]

t,y = max(tate),max(yoko)
for i in range(W):
  if tate[i] == t:
    tateArr.append(i)
for i in range(H):
  if yoko[i] == y:
    yokoArr.append(i)
for i,j in memo:
  if j not in tateArr and i not in yokoArr:
    print(t+y)
    exit()
print(t+y-1)

Submission status: Runtime Error

Solution code:
h,w,m=map(int,input().split())
lst=[[0]*w for i in range(h)]
by_row=[0 for i in range(h)]
by_col=[0 for i in range(w)]
for i in range(m):
	p,q,=map(int,input().split())
	lst[p-1][q-1]=1
	by_row[p-1]+=1
	by_col[q-1]+=1

ans=0
for i in range(h):
	for j in range(w):
		ans=max(ans,by_row[i]+by_col[j]-lst[i][j])
print(ans)

Submission status: Runtime Error

Solution code:
h, w, m = map(int, input().split())
h_cnt = [0 for i in range(h)]
w_cnt = [0 for i in range(w)]
bomb = set()
for _ in range(m):
    c_h, c_w = map(int, input().split())
    h_cnt[c_h-1] +=1
    w_cnt[c_w-1]+=1
    bomb.add(((c_h-1),(c_w-1)))
    
h_max = max(h_cnt)
w_max = max(w_cnt)
    
max_h = [i for i in range(h) if h_cnt[i] == h_max]
max_w = [i for i in range(w) if w_cnt[i] == w_max]

flg = 0
for i in max_h:
    for j in max_w:
        if (i,j) not in bomb:
            flg = 1
            break

if flg:
    print(max_h + max_w)
else:
    print(max_h + max_w -1)

Submission status: Runtime Error

Solution code:
h, w, m = map(int, input().split())
h_cnt = [0 for i in range(h)]
w_cnt = [0 for i in range(w)]
bomb = set()
for _ in range(m):
    c_h, c_w = map(int, input().split())
    h_cnt[c_h-1] +=1
    w_cnt[c_w-1]+=1
    bomb.add(((c_h-1),(c_w-1)))
    
h_max = max(h_cnt)
w_max = max(w_cnt)
    
max_h = [i for i in range(len(h_cnt)) if h_cnt[i] == h_max]
max_w = [i for i in range(len(w_cnt)) if h_cnt[i] == w_max]

flg = 0
for i in max_h:
    for j in max_w:
        if (i,j) not in bomb:
            flg = 1
            break

if flg:
    print(max_h + max_w)
else:
    print(max_h + max_w -1)

Submission status: Runtime Error

Solution code:
h, w, m = map(int, input().split())
h_cnt = [0 for i in range(h)]
w_cnt = [0 for i in range(w)]
bomb = set()
for _ in range(m):
    c_h, c_w = map(int, input().split())
    h_cnt[c_h-1] +=1
    w_cnt[c_w-1]+=1
    bomb.add(((c_h-1),(c_w-1)))

max_h = [i for i in range(len(h_cnt)) if h_cnt[i] == max(h_cnt)]
max_w = [i for i in range(len(w_cnt)) if h_cnt[i] == max(w_cnt)]

ans = []
for i in max_h:
    for j in max_w:
        if (i,j) in bomb:
            ans.append(h_cnt[i] + w_cnt[j] -1)
        else:
            ans.append(h_cnt[i] + w_cnt[j])

print(max(ans))

Submission status: Runtime Error

Solution code:
h, w, m = map(int, input().split())
h_cnt = [0 for i in range(h)]
w_cnt = [0 for i in range(w)]
bomb = set()
for _ in range(m):
    c_h, c_w = map(int, input().split())
    h_cnt[c_h-1] +=1
    w_cnt[c_w-1]+=1
    bomb.add(((c_h-1),(c_w-1)))

max_h = [i for i in range(len(h_cnt)) if h_cnt[i] == max(h_cnt)]
max_w = [i for i in range(len(w_cnt)) if h_cnt[i] == max(w_cnt)]

ans = []
for i in max_h:
    for j in max_w
    if (i,j) in bomb:
        ans.append(h_cnt[i] + w_cnt[j] -1)
    else:
        ans.append(h_cnt[i] + w_cnt[j])

print(max(ans))

Submission status: Runtime Error

Solution code:
import numpy as np
import sys

h,w,m = map(int, input().split())
hw = [list(map(int, input().split())) for i in range(m)]
map = np.zeros((h, w))

for i in hw:
  map[i[0]-1, i[1]-1] = 1
  

hcount = np.count_nonzero(map, axis=0)
wcount = np.count_nonzero(map, axis=1)

hmax = max(hcount)
wmax = max(wcount)

hindex = [i for i, x in enumerate(hcount) if x == hmax]
windex = [i for i, x in enumerate(wcount) if x == wmax]


for i in hindex:
  for j in windex:
    if [j+1, i+1] not in hw:
      print(hmax + wmax)
      sys.exit()

print(hmax + wmax - 1)

Submission status: Runtime Error

Solution code:
import numpy as np
import sys

h,w,m = map(int, input().split())
hw = [list(map(int, input().split())) for i in range(m)]
map = np.zeros((h, w))

for i in hw:
  map[i[0]-1, i[1]-1] = 1

hcount = np.count_nonzero(map, axis=0)
wcount = np.count_nonzero(map, axis=1)

hmax = max(hcount)
wmax = max(wcount)

hindex = [i for i, x in enumerate(hcount) if x == hmax]
windex = [i for i, x in enumerate(wcount) if x == wmax]

for i in hindex:
  for j in windex:
    if [i-1, j-1] not in hw:
      print(hmax + wmax)
      sys.exit()

print(hmax + wmax - 1)

Submission status: Runtime Error

Solution code:
import numpy as np
h, w, m = map(int, input().split())
arr = np.zeros([h, w], dtype=np.int8)
for _ in range(m):
    c_h, c_w = map(int, input().split())
    arr[c_h-1, c_w-1] = 1
    
row_sum = np.sum(arr, axis=1)
col_sum = np.sum(arr, axis=0)
row_max_pos = np.argwhere(row_sum == np.max(row_sum))
col_max_pos = np.argwhere(col_sum == np.max(col_sum))
    
row_max = int(max(row_sum))
col_max = int(max(col_sum))

ans = []
for i in row_max_pos:
    for j in col_max_pos:
        if arr[i,j] == 1:
            ans.append(row_max + col_max-1)
        else:
            ans.append(row_max + col_max-1)

Submission status: Runtime Error

Solution code:
import numpy as np
h, w, m = map(int, input().split())
arr = np.zeros([h, w], dtype=np.int8)
for _ in range(m):
    c_h, c_w = map(int, input().split())
    arr[c_h-1, c_w-1] = 1
    
row_sum = np.sum(arr, axis=1)
col_sum = np.sum(arr, axis=0)
row_max_pos = np.argwhere(row_sum == np.max(row_sum))
col_max_pos = np.argwhere(col_sum == np.max(col_sum))
    
row_max = int(max(row_sum))
col_max = int(max(col_sum))

ans = []
for i in row_max_pos:
    for j in col_max_pos:
        if arr[i,j] == 1:
            ans.append(row_max + col_max-1)
        else
            ans.append(row_max + col_max-1)

Submission status: Runtime Error

Solution code:
h,w,m = map(int, input().split())
column = [0 for i in range(h+1)]
row = [0 for i in range(w+1)]
column_max = 0
row_max = 0
column_max_index = []
row_max_index = []
matrix = [[0] * (w+1) for i in range(h+1)]

for i in range(m):
  a,b = map(int, input().split())
  column[a] += 1
  row[b] += 1
  if column[a] == column_max:
    column_max_index.append(a)
    
  if column[a] > column_max:
    column_max = column[a]
    column_max_index = [a]
  
  if row[b] == row_max:
    row_max_index.append(b)
    
  if row[b] > row_max:
    row_max = row[b]
    row_max_index = [b]
  matrix[a][b] = 1
  
count = 0
for i in column_max_index:
  for j in row_max_index:
    if matrix[i][j] == 0:
      count += 1
      break
if count != 0:
  print(column_max + row_max)
else:
  print(column_max + row_max - 1)