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:
import numpy as np

H, W, M = map(int, list(input().split()))
hw = [list(map(int, list(input().split()))) for i in range(M)]

sh = [0] * H
sw = [0] * W
bomb = np.zeros((H, W), dtype=int)
for i in range(M):
    sh[hw[i][0] - 1] += 1
    sw[hw[i][1] - 1] += 1
    bomb[hw[i][0] - 1, hw[i][1] - 1] = 1

h_list = [i for i, x in enumerate(sh) if x == max(sh)]
w_list = [i for i, x in enumerate(sw) if x == max(sw)]

print(6)

Submission status: Runtime Error

Solution code:
H, W, M = map(int, input().split())
hw_lst = []
board = [[0] * W for _ in range(H)]
h_cnt = [0] * H
w_cnt = [0] * W
for i in range(M):
    hw = list(map(int, input().split()))
    hw_lst.append(hw)
    board[hw[0]-1][hw[1]-1] = 1
    h_cnt[hw[0]-1] += 1
    w_cnt[hw[1]-1] += 1
# for i in board:
#     print(i)

h_max = []
max_num_h = 0
max_num = 0
for i, value in enumerate(h_cnt):
    if value >= max_num:
        if value != max_num:
            h_max = []
            max_num_h = value
        max_num = value
        h_max.append(i)
w_max = []
max_num_w = 0
max_num = 0
for i, value in enumerate(w_cnt):
    if value >= max_num:
        if value != max_num:
            w_max = []
            max_num_w = value
        max_num = value
        w_max.append(i)
# print(h_cnt, w_cnt)
# print(h_max, w_max)

flug = True     #交差点に爆破対象がある
for i, value1 in enumerate(h_max):
    for j, value2 in enumerate(w_max):
        if board[value1][value2] != 1:
            flug = False
            break
if flug:
    print(max_num_h + max_num_w - 1)
else:
    print(max_num_h + max_num_w)

Submission status: Runtime Error

Solution code:
import sys

r = list(map(int,input().split()))
h = r[0]
w = r[1]
m = r[2]
l = []

hl = [0 for i in range(m)]
wl = [0 for i in range(m)]

for i in range(m):
    a = list(map(int,input().split()))
    hl[a[0]-1] += 1
    wl[a[1]-1] += 1
    l.append(a) 

#print(l)
#print(hl)
#print(wl)

H = max(hl)
W = max(wl)

hlist = []
wlist = []

for i in range(m):
    if hl[i] == H:
        hlist.append(i+1)
    if wl[i] == W:
        wlist.append(i+1)

ans = H+W
##print(ans)
#print(hlist)
#print(wlist)

flag = 1

for i in range(len(hlist)):
    if flag == 0:
        break
    for j in range(len(wlist)):
        flag *= l.count([hlist[i],wlist[j]])
        if flag == 0:
            break

ans -= flag
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

H, W, M = [int(n) for n in input().split()]
grid = np.zeros((H, W))
for i in range(M):
    h, w = [int(n) for n in input().split()]
    grid[h-1, w-1] = 1

col_sums = grid.sum(axis=0)
row_sums = grid.sum(axis=1)

col_indexes = np.argwhere(col_sums==col_sums.max()).flatten().tolist()
row_indexes = np.argwhere(row_sums==row_sums.max()).flatten().tolist()

intersection_without_bomb = False
for row_idx in row_indexes:
    for col_idx in col_indexes:
        if grid[row_idx, col_idx] == 0:
            intersection_without_bomb = True
            break
    if intersection_without_bomb:
        break

if intersection_without_bomb:
    print(int(col_sums.max() + row_sums.max()))
else:
    print(int(col_sums.max() + row_sums.max()) - 1)

Submission status: Runtime Error

Solution code:
import time
t = time.time()
H,W,m = map(int,input().split())
h = [0]*(H+1)
w = [0]*(W+1)
c = [[-1] for i in range(H+1)]
for i in range(m):
    a,b = map(int,input().split())
    h[a] += 1
    w[b] += 1
    c[a].append(b)
mh = max(h)
mw = max(w)
hkouho = []
wkouho = []
for i in range(H+1):
    if h[i] == mh:
        hkouho.append(i)
for i in range(W+1):
    if w[i] == mw:
        wkouho.append(i)
for i in hkouho:
    for j in wkouho:
        if j not in c[i]:
            print(mh+mw)
            exit()
        if time.time-t > 2.7:
            print(mh+mw-1)
            exit()
print(mh+mw-1)

Submission status: Runtime Error

Solution code:
import numpy as np

H,W,M=map(int,input().split())
hw = np.zeros([H,W],dtype=int)
for i in range(M):
  h,w=map(int,input().split())
  hw[h-1,w-1] = 1

sum1 = hw.sum(1)
sum2 = hw.sum(0)

sum1 = sorted(enumerate(sum1),key=lambda x:x[1],reverse=True)
sum2 = sorted(enumerate(sum2),key=lambda x:x[1],reverse=True)

ans=0
for idx1,val1 in sum1[:min(3,len(sum1))]:
  for idx2,val2 in sum2:
    tmp = val1+val2-hw[idx1,idx2]
    ans = max(ans,tmp)
    
print(ans)

Submission status: Runtime Error

Solution code:
h, w, m = map(int, input().split())
hs = [0] * h
ws = [0] * w
pos = [[0] * w for i in range(h)]
for _ in range(m):
    hi, wi = map(int, input().split())
    pos[hi-1][wi-1] = 1
    hs[hi-1] += 1
    ws[wi-1] += 1
    
mh = max(hs)
mhs = []
for i in range(h):
    if hs[i] == mh:
        mhs.append(i+1)

mw = max(ws)
mws = []
for i in range(w):
    if ws[i] == mw:
        mws.append(i+1)

flg = False
for i in mhs:
    for j in mws:
        if pos[i-1][j-1]==0:
            flg = True
            print(max(hs) + max(ws))
            break
    if flg:
        break
else:
    print(max(hs) + max(ws) - 1)

Submission status: Runtime Error

Solution code:
import sys

H, W, M = map(int, sys.stdin.readline().split())
hlst = [0]*(H+1)
wlst = [0]*(M+1)
hwlst = []

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

hmax = max(hlst)
wmax = max(wlst)
ans = 0
c = True
i = 0
for i in range(H+1):
    if hlst[i] == hmax:
        for j in range(W+1):
            if wlst[j] == wmax:
                if [i, j] in hwlst:
                    ans = hmax + wmax -1
                else:
                    ans = hmax + wmax

print(ans)

Submission status: Runtime Error

Solution code:
import numpy
import itertools
H, W, M = map(int, input().split())
mine = numpy.zeros((H, W))

for _ in range(M):
    h, w = map(int, input().split())
    mine[h - 1, w - 1] = 1

wb = numpy.sum(mine, axis=0)
wcandidate = numpy.where(wb == numpy.max(wb))[0]

hb = numpy.sum(mine, axis=1)
hcandidate = numpy.where(hb == numpy.max(hb))[0]

ans = 0

for i,j in itertools.product(hcandidate, wcandidate):
    ans = max(ans, numpy.sum(mine[i, :]) + numpy.sum(mine[:, j]) - mine[i, j])

print(int(ans))

Submission status: Runtime Error

Solution code:
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    h, w, m = i_map()
    num_list = i_row_list(m)

    h_list = []
    w_list = []

    count = 0

    for num in num_list:
        h_list.append(num[0])
        w_list.append(num[1])

    co_h_list = Counter(h_list)
    co_w_list = Counter(w_list)

    # print(num_list)
    # print(co_h_list.most_common())
    # print(co_w_list.most_common())

    h_max_key = co_h_list.most_common()[0][0]
    h_max_value = co_h_list.most_common()[0][1]

    w_max_key = co_w_list.most_common()[0][0]
    w_max_value = co_h_list.most_common()[0][1]

    if h_max_value >= w_max_value:
        max_index = h_max_key
        count = h_max_value

        tmp_list = []
        for num in num_list:
            if num[0] == max_index:
                continue
            tmp_list.append(num[1])

        count += Counter(tmp_list).most_common()[0][1]
    else:
        max_index = w_max_key
        count = w_max_value

        tmp_list = []
        for num in num_list:
            if num[1] == max_index:
                continue
            tmp_list.append(num[0])

        count += Counter(tmp_list).most_common()[0][1]

    print(count)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys, bisect, math, itertools, heapq, collections
from operator import itemgetter
# a.sort(key=itemgetter(i)) # i番目要素でsort
from functools import lru_cache
# @lru_cache(maxsize=None)
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9 + 7
eps = 10**-7


def inp():
    '''
    一つの整数
    '''
    return int(input())


def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


def str_inp():
    '''
    文字列をリストとして読み込む
    '''
    return list(input()[:-1])


h, w, m = inpl()
dic1 = [0] * h
dic2 = [0] * w
mine = [[0] * w for _ in range(h)]
for _ in range(m):
    y, x = inpl()
    dic1[y - 1] += 1
    dic2[x - 1] += 1
    mine[y - 1][x - 1] = 1

sub1 = []
tmp = 0
for i in range(h):
    if tmp < dic1[i]:
        sub1 = [i]
        tmp = dic1[i]
    elif tmp == dic1[i]:
        sub1.append(i)
temp = 0
sub2 = []
for i in range(w):
    if temp < dic2[i]:
        sub2 = [i]
        temp = dic2[i]
    elif temp == dic2[i]:
        sub2.append(i)
ans = -1
FLAG = False
for i in sub1:
    for j in sub2:
        if mine[i][j] == 0:
            ans = 0
            FLAG = True
            break
    if FLAG:
        break
print(ans + tmp + temp)

Submission status: Runtime Error

Solution code:
import numpy as np

HWM = [int(a) for a in input().split()]
H = HWM[0]
W = HWM[1]
M = HWM[2]

Bombs = np.array([[int(b) for b in input().split()] for l in range(M)])

npBombs = np.zeros((W,H))
for x,y in Bombs:
  npBombs[x-1][y-1] = 1

Hs = []
Hmax = 0
for x in range(H):
  s = np.sum(npBombs[x,::])
  Hs.append(s)

Ws = []
Wmax = 0
for y in range(W):
  s = np.sum(npBombs[::, y])
  Ws.append(s)

Hmax = max(Hs)
Wmax = max(Ws)

answer = Hmax + Wmax - 1
for x in range(H):
  for y in range(W):
    if Hs[x] == Hmax and Ws[y] == Wmax and npBombs[x][y] == 0:
      answer += 1

print(int(answer))

Submission status: Runtime Error

Solution code:
import itertools
import numpy as np

# 別変数の整数複数個
H, W, M = [int(x) for x in input().strip().split(" ")]

G = np.zeros((H, W))
for _ in range(M):
    h, w = [int(x) for x in input().strip().split(" ")]
    G[h-1][w-1] = 1

h_sums = np.sum(G, axis=1)
w_sums = np.sum(G, axis=0)
h_sum_max = max(h_sums)
w_sum_max = max(w_sums)
h_maxs = [i for i, v in enumerate(h_sums) if v == h_sum_max]
w_maxs = [i for i, v in enumerate(w_sums) if v == w_sum_max]
cnt = int(h_sum_max + w_sum_max)

have_zero = any([G[h][w] == 0 for h, w in itertools.product(h_maxs, w_maxs)])
print(cnt if have_zero else cnt - 1)

Submission status: Runtime Error

Solution code:
import sys
H, W, M= map(int,input().split())
height = [0]*H
width = [0]*W
object = [width]*H
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
if !(len(cand_h)*len(cand_w) > M):
   for i in range(len(cand_h)):
      for j in range(len(cand_w)):
         if object[cand_h[i]][cand_w[j]] == 0:
            ans += 1
else:
   ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = map(int, input().split())
S = [[0]*W for _ in range(H)]
for _ in range(M):
  h, w = map(lambda x: int(x)-1, input().split())
  S[h][w] = 1

S = np.array(S)
T = np.array([[0]*W for _ in range(H)])
for i in range(H):
  T[i] += np.sum(S[i])

for i in range(W):
  T[:, i] += np.sum(S[:, i])

T -= S
print(np.max(T))

Submission status: Runtime Error

Solution code:
h,w,m=map(int,input().split())
hw=[list(map(int,input().split())) for _ in range(m)]
numh=[[i,0] for i in range(h)]
numw=[[i,0] for i in range(w)]
m=[[0]*w for _ in range(h)]
for hi,wi in hw:
  numh[hi-1][1]+=1
  numw[wi-1][1]+=1
  m[hi-1][wi-1]=1
numh.sort(key=lambda x:x[1])
numw.sort(key=lambda x:x[1])
ansh=numh[-1][1]
answ=numw[-1][1]
if ansh+answ==1:
  print(1)
  exit()
# 答えはansh+answまたはansh+answ-1
hk=set(())
while numh and numh[-1][1]==ansh:
  hk.add(numh.pop()[0])
wk=set(())
while numw and numw[-1][1]==answ:
  wk.add(numw.pop()[0])

for wi in wk:
  for hi in hk:
    if m[hi][wi]==0:
      print(ansh+answ)
      exit()
print(ansh+answ-1)

Submission status: Runtime Error

Solution code:
H, W, M = map(int, input().split())
lis =[]
Hlis = [0]*H
Mlis = [0]*M
for _ in range(M):
    Ch, Cm = map(int, input().split())
    Hlis[Ch-1] += 1
    Mlis[Cm-1] += 1
    lis.append([Ch, Cm])

Hmax = max(Hlis)
Hidx = Hlis.index(Hmax)+1
Mmax = max(Mlis)
Midx = Mlis.index(Mmax)+1

if [Hidx, Midx] in lis:
    print(Hmax + Mmax -1)
else:
    print(Hmax+Mmax)

Submission status: Runtime Error

Solution code:
import numpy as np
def resolve():
    H,W,M = map(int,input().split())
    
    Hnum = np.zeros(H+1)
    Wnum = np.zeros(W+1)
    Mdata = np.zeros((H+1, W+1), dtype = int)
    
    for i in range(M):
        h,w = map(int,input().split())
        Mdata[h,w] = 1
        Hnum[h] += 1
        Wnum[w] += 1
    
    ans = int(max(Hnum) + max(Wnum))
    HmaxIdx = np.where(Hnum == Hnum.max())[0]
    WmaxIdx = np.where(Wnum == Wnum.max())[0]
    
    for i in HmaxIdx:
        for j in WmaxIdx:
            if Mdata[i,i] == 1:
                continue
            else:
                return ans
    return ans - 1
    

ans = resolve()

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

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

#print(ans)
colm=np.sum(ans, axis=0)
line=np.sum(ans, axis=1)

ataimax=0
for ih in range(0,h):
  for iw in range(0,w):
     atai=colm[iw]+line[ih]-ans[ih,iw]
     if (atai >= ataimax): ataimax=atai

print (ataimax)

Submission status: Runtime Error

Solution code:
import numpy as np

H,W,M=input().split()
H=int(H)
W=int(W)
M=int(M)

MA=np.zeros((H,W))
MX=[0]*H
MY=[0]*W
for i in range(M):
  x,y=input().split()
  MA[int(x)-1,int(y)-1]=1

for x in range(H):
  MX[x]=sum(MA[x,:])

for y in range(W):
  MY[y]=sum(MA[:,y])

x,xmax=max(enumerate(MX),key=lambda x: x[1])
y,ymax=max(enumerate(MX),key=lambda x: x[1])

print(int(xmax+ymax-MA[x,y]))

Submission status: Runtime Error

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

htotal = [0] * h
wtotal = [0] * w


hmax = 0
wmax = 0

s = set()
hl = []
wl = []

for i in range(m):
    hi, wi = [int(e) for e in input().split()]
    hi -= 1
    wi -= 1

    s.add((hi, wi))
    htotal[hi] += 1
    wtotal[wi] += 1

    if htotal[hi] > hmax:
        hmax = htotal[hi]
        hl = [hi]
    elif htotal[hi] == hmax:
        hl.append(hi)

    if wtotal[hi] > wmax:
        wmax = wtotal[wi]
        wl = [wi]
    elif wtotal[wi] == wmax:
        wl.append(wi)

m = hmax + wmax - 1
for he in hl:
    if m == hmax + wmax:
        break
    for we in wl:
        if m == hmax + wmax:
            break
        if not (he, we) in s:
            m += 1

print(m)

Submission status: Runtime Error

Solution code:
H , W , M = map(int,input().split())
h = [0]*M
w = [0]*M
for i in range(M):
  h[i] , w[i] = map(int,input().split())
G1 = [0]*H
G2 = [0]*W
for i in range(M):
  temp = h[i] - 1
  G1[temp] += 1
for  i in range(M):
  temp = w[i] - 1
  G2[temp] += 1
G3 = [[h[i] - 1 , w[i] - 1] for i in range(M)]
M1 = max(G1)
M2 = max(G2)
M1L = []
M2L = []
for i in range(H):
  if h[i] == M1:
    M1L.append(i)
for j in range(W):
  if w[j] == M2:
    M2L.append(j)
c = 0
for i in M1L:
  for j in M2L:
    if [i,j] not in G3:
      c = 1
      break
  if c ==1:
    break
if c == 1:
  print(M1 + M2)
else:
  print(M1 + M2 - 1)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = map(int, input().split()) # H * Mのマス目, M個のターゲット

place_inf = []
for _ in range(H):
    place_inf.append([0 for _ in range(W)])

for _ in range(M):
    i, j = map(int, input().split())
    place_inf[i - 1][j - 1] = 1
    
A = np.array(place_inf)
# print(A)
a = list(np.sum(A, axis=0))
b = list(np.sum(A, axis=1))
a_i = a.index(max(a))
b_i = b.index(max(b))

if place_inf[a_i][b_i] == 1:
  print(max(a) + max(b) - 1)
else:
  print(max(a) + max(b))

Submission status: Runtime Error

Solution code:
h,w,m=map(int, input().split())
grid=[[0 for i in range(w)]for j in range(h)]
row=[0 for i in range(h)]
col=[0 for i in range(w)]
for i in range(m):
    a,b=map(int, input().split())
    grid[a-1][b-1]=1
    row[a-1]+=1
    col[b-1]+=1
val=0
index2=-1
m=max(row)
index1=row.index(m)
maxi=-1
for i in range(h):
    val=col[i]-grid[index1][i]
    if val>maxi:
        maxi=val
        index2=i
'''print(index1,index2)
for i in range(w):
    val+=grid[index1][i]
for j in range(1,h):
    val+=grid[j][index2]'''
print(maxi+m)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = map(int, input().split()) # H * Mのマス目, M個のターゲット

place_inf = []
for _ in range(H):
    place_inf.append([0 for _ in range(W)])

for _ in range(M):
    i, j = map(int, input().split())
    place_inf[i - 1][j - 1] = 1
    
A = np.array(place_inf)
# print(A)
a = list(np.sum(A, axis=0))
b = list(np.sum(A, axis=1))
a_i = a.index(max(a))
b_i = b.index(max(b))

if place_inf[a_i][b_i] == 1:
  print(max(a) + max(b) - 1)
else:
  print(max(a) + max(b) - 1)

Submission status: Runtime Error

Solution code:
import statistics
H,W,M = map(int, input().split())
hw = [map(int, input().split()) for _ in range(M)]
h, w = [list(i) for i in zip(*hw)]

h_1 = h
w_1 = w

#print(h)
#print(w)
#print(statistics.mode(h))
h_s = statistics.mode(h)
w_s = statistics.mode(w)
for i in range(M):
  if h[i] == h_s:
    w[i] = 0

while 0 in w:
  w.remove(0)
  
#print(w)
w_s_1 = statistics.mode(w)

cnt = 0
for i in range(M):
  if h[i] == h_s:
    cnt +=1
    
for i in range(len(w)):
  if w[i] == w_s_1:
    cnt += 1
    
print(cnt)

Submission status: Runtime Error

Solution code:
H,W,M=map(int,input().split())
hw=[]
xcount=dict()
xmax=0
ans=0
for i in range(M):
    hw.append(list(map(int,input().split())))
    xcount.setdefault(hw[i][0],0)
    xcount[hw[i][0]]+=1
    if xcount[hw[i][0]]>xmax:
        xmax=xcount[hw[i][0]]
        xmaxindex=hw[i][0]
ans+=xcount[xmaxindex]
ycount=dict()
ymax=0
for i in range(M):
    if hw[i][0]==xmaxindex:
        continue
    ycount.setdefault(hw[i][1],0)
    ycount[hw[i][1]]+=1
    if ycount[hw[i][1]]>ymax:
        ymax=ycount[hw[i][1]]
        ymaxindex=hw[i][1]
ans+=ycount[ymaxindex]
print(ans)

Submission status: Runtime Error

Solution code:
h,w,m = map(int, input().split(' '))
data = [list(map(int, input().split(' '))) for _ in range(m)]

def f(dic, i):
    if dic.get(i) == None:
        dic[i] = 1
    else:
        dic[i] += 1
    return dic

dic = {}
for d in data:
    dic = f(dic, d[0])

row = max(dic, key=dic.get)
cnt = 0
dic = {}
for d in data:
    if d[0] == row:
        cnt += 1
    else:
        dic = f(dic, d[1])
print(cnt + max(dic.values()))

Submission status: Runtime Error

Solution code:
# coding: utf-8

import numpy as np

def main():
    h, w, m = map(int, input().split())
    matrix = np.zeros((h, w))
    
    for i in range(m):
        x, y = map(int, input().split())
        matrix[x-1, y-1] = 1
    # print(matrix)
    
    sum_row = matrix.sum(axis=1).reshape(h, 1)
    sum_col = matrix.sum(axis=0)
    
    # print(sum_row)
    # print(sum_col)
    
    ans_matrix = np.tile(sum_row, (1, w))
    ans_matrix += sum_col
    
    ans_matrix -= matrix
    
    
    print(int(np.max(ans_matrix)))
 
main()

Submission status: Runtime Error

Solution code:
# abc176_e.py
H, W, M = map(int,input().split())
hcnt = [0]*H
wcnt = [0]*W
duplicate = [[0]*W for i in range(H)]

for i in range(M):
    h,w = map(int,input().split())
    hcnt[h-1]+=1
    wcnt[w-1]+=1
    duplicate[h-1][w-1] += 1
hmax = 0
hmaxidx = 0
for i,v in enumerate(hcnt):
    if hmax < v:
        hmax = v
        hmaxidx = i

wmax = 0
wmaxidx = 0
for i,v in enumerate(wcnt):
    if wmax < v:
        wmax = v
        wmaxidx = i
print(hmax+wmax - duplicate[hmaxidx][wmaxidx])

Submission status: Runtime Error

Solution code:
horigin,worigin,m = map(int,input().split())
dich = {}
dicw = {}

havew = {}
for _ in range(m):
    h,w = map(lambda x: int(x)-1,input().split())
    if h in dich:
        dich[h] += 1
    else:
        dich[h] = 1
        havew[h] = [False] * worigin
    havew[h][w] = True
    if w in dicw:
        dicw[w] += 1
    else:
        dicw[w] = 1

dichi = list(dich.items())
dichi.sort(reverse=True, key=lambda x:x[1])
dicwi = list(dicw.items())
dicwi.sort(reverse=True, key=lambda x:x[1])

# print(havew)
#
# print(dichi)
# print(dicwi)

hmax = dichi[0][1]
wmax = dicwi[0][1]

for hs in dichi:
    if hs[1] != hmax:
        break
    for ws in dicwi:
        # print(hs, ws)
        if ws[1] != wmax:
            break
        if not havew[hs[0]][ws[0]]:
            print(hmax+wmax)
            exit()
print(hmax+wmax-1)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
from collections import*
(h, w, m), *d = [[*map(int, o.split())] for o in open(0)]
x, y = zip(*d)
a, e = Counter(x).most_common()[0]
b, f = Counter(y).most_common()[0]
C = defaultdict(lambda: 0)
D = defaultdict(lambda: 0)
for h, w in d:
    if h != a: C[w] += 1
for h, w in d:
    if w != b: D[w] += 1
C, D = Counter(C), Counter(D)
print(max(e + C.most_common()[0][1], f + D.most_common()[0][1]))

Submission status: Runtime Error

Solution code:
h,w,m=map(int,input().split())
W=[0 for i in range(w)]
H=[0 for i in range(w)]
HW = [[0 for i in range(w)] for j in range(h)]
for _ in range(m):
    i,j=map(int,input().split())
    H[i-1] += 1
    W[j-1] += 1
    HW[i-1][j-1] = 1
h_index = []
w_index = []
for i in range(h):
    if max(H)==H[i]:
        h_index.append(i)
for i in range(w):
    if max(W)==W[i]:
        w_index.append(i)

for i in h_index:
    for j in w_index:
        if HW[i][j]==0:
            print(max(H)+max(W))
            exit()
print(max(H)+max(W)-1)
'''print(h_index, w_index)
if HW[h_index][w_index]==0: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=[0 for i in range(h)]
W=[0 for i in range(w)]
A=[[0 for i in range(h)]for i in range(w)]

for i in range(m):
  a,b=map(int,input().split(" "))
  H[a-1]+=1
  W[b-1]+=1
  A[b-1][a-1]=1
  

X=[]
Y=[]

for i in range(h):
  if H[i]==max(H):
    X.append(i)
for i in range(w):
  if W[i]==max(W):
    Y.append(i)
for i in range(int(len(X))):
  for j in range(len(Y)):
    if A[X[i]][Y[j]]==0:
      ans=max(H)+max(W)
      break
  if i==int(len(X))-1:
    ans=max(H)+max(W)-1
print(ans)

Submission status: Runtime Error

Solution code:
def resolve():
    h,w,m = map(int, input().split())
    mine = []
    mineh = [0]*h
    minew = [0]*w
    for i in range(m):
        a,b = map(int, input().split())
        a -= 1
        b -= 1
        mine.append([a,b])
        mineh[a] += 1
        minew[b] += 1
    maxh = max(mineh)
    hnum = []
    maxw = max(minew)
    wnum = []
    for i in range(h):
        if mineh[i] == maxh:
            hnum.append(i)
        if minew[i] == maxw:
            wnum.append(i)
    for i in hnum:
        for j in wnum:
            if [i,j] in mine:
                continue
            else:
                print(maxw+maxh)
                exit()
    print(maxw+maxh-1)
resolve()

Submission status: Runtime Error

Solution code:
H, W, M = map(int,input().split())
hcnt = [0]*H
wcnt = [0]*W
duplicate = [[0]*W for i in range(H)]

for i in range(M):
    h,w = map(int,input().split())
    hcnt[h-1]+=1
    wcnt[w-1]+=1
    duplicate[h-1][w-1] += 1
hmax = 0
hmaxidx = 0
for i,v in enumerate(hcnt):
    if hmax < v:
        hmax = v
        hmaxidx = i

wmax = 0
wmaxidx = 0
for i,v in enumerate(wcnt):
    if wmax < v:
        wmax = v
        wmaxidx = i
print(hcnt,wcnt)
print(hmax+wmax - duplicate[hmaxidx][wmaxidx])

Submission status: Runtime Error

Solution code:
import numpy as np

H,W,M=map(int,input().split())
hw = np.zeros([H,W],dtype=int)
for i in range(M):
  h,w=map(int,input().split())
  hw[h-1,w-1] = 1

sum1 = hw.sum(1)
sum2 = hw.sum(0)

sum1 = sorted(enumerate(sum1),key=lambda x:x[1],reverse=True)
sum2 = sorted(enumerate(sum2),key=lambda x:x[1],reverse=True)

ans=0
for idx1,val1 in sum1[:min(3,len(sum1))+1]:
  for idx2,val2 in sum2:
    tmp = val1+val2-hw[idx1,idx2]
    ans = max(ans,tmp)
    
print(ans)

Submission status: Runtime Error

Solution code:
h,w,m=map(int, input().split())
a = [[0 for j in range(w)] for i in range(h)]
for i in range(m):
	x,y=map(int, input().split())
	a[x-1][y-1]+=1
hc=[0 for i in range(w)]
wc=[0 for i in range(h)]
for i in range(w):
	for j in range(h):
		hc[i] += a[j][i]
for i in range(h):
	for j in range(w):
		wc[i] += a[i][j]
ans=0
for i in range(len(hc)):
	for j in range(len(wc)):
		if a[i][j]==1:
			if ans<hc[i]+wc[j]-1:
				ans=hc[i]+wc[j]-1
		else:
			if ans<hc[i]+wc[j]:
				ans=hc[i]+wc[i]
print(ans)

Submission status: Runtime Error

Solution code:
h, w, m = map(int, input().split())
mh = [None]*m
mw = [None]*m
bomb = {}
ans = 0

for i in range(m):
    mh[i], mw[i] = map(int, input().split())
    if mh[i] in bomb:
        bomb[mh[i]] += 1
    else:
        bomb[mh[i]] = 1
    
for key in bomb:
    dele = key
    num = bomb[key]
    bomb2 = {}
    for i in range(m):
        if mh[i] == dele:
            continue
        if mw[i] in bomb2:
            bomb2[mw[i]] += 1
        else:
            bomb2[mw[i]] = 1
    max_v = max(bomb2.values())
    total = num + max_v
    ans = max(ans, total)

print(ans)

Submission status: Runtime Error

Solution code:
'''
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College

'''
from os import path
import sys
from heapq import heappush,heappop
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd 
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input().rstrip()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
mod=1000000007
# mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]

def bo(i):
    return ord(i)-ord('a')

file = 1




    
def solve():

    
    # for _ in range(ii()):

        h,w,m=mi()
        row=[0]*h
        col=[0]*w
        vis=[[0]*w for i in range(h)]
        for i in range(m):
            x,y=mi()
            x -= 1
            y -= 1
            row[x] +=1
            col[y] +=1
            vis[x][y]=1
        ans = 0
        for i in range(h):
            for j in range(w):
                if vis[i][j]==0:
                    ans = max(ans,row[i]+col[j])
                else:
                    ans = max(ans,(row[i]+col[j]-1))
        print(ans)












        
if __name__ =="__main__":

    if(file):

        if path.exists('input.txt'):
            sys.stdin=open('input.txt', 'r')
            sys.stdout=open('output.txt','w')
        else:
            input=sys.stdin.readline
    solve()

Submission status: Runtime Error

Solution code:
h,w,m=map(int,input().split())
l=[list(map(int,input().split())) for _ in range(m)]

lh,lw=[],[]

for i in range(h):
    lh.append(0)
for i in range(w):
    lw.append(0)

for i in range(m):
    lh[l[i][0]-1]+=1
    lw[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])

ll=[]
for i in range(len(lhlm)):
    for j in range(len(lwlm)):
        k=[]
        k.append(lhlm[i])
        k.append(lwlm[j])
        ll.append(k)

l1_l2_and = set(l) & set(ll)
if len(ll)==len(l1_l2_and):print(lhm+lwm-1)
else:print(lhm+lwm)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = list(map(int,input().split()))
grid = np.zeros((H,W),int)
Hnum = np.zeros((H),int)
Wnum = np.zeros((W),int)
for i in range(M):
    bh, bw = list(map(int,input().split()))
    Hnum[bh-1] += 1
    Wnum[bw-1] += 1
    grid[bh-1, bw-1] = 1


maxb = 0
# for i in range(H):
#     for j in range(W):
#         bomb = Hnum[i]+Wnum[j]-grid[i,j]
#         if maxb < bomb:
#             maxb = bomb
print(maxb)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# モジュールのインポート
import numpy as np

# 標準入力を取得
H, W, M = list(map(int, input().split()))
B = np.zeros(shape=(H, W))
for m in range(M):
    h_m, w_m = list(map(int, input().split()))
    B[h_m - 1, w_m - 1] = 1

# 求解処理
h_bom = -1
max_bom_h = 0
for h in range(H):
    bom_h = sum(B[h])
    if max_bom_h < bom_h:
        max_bom_h = bom_h
        h_bom = h

B = np.delete(B, h_bom, 0)
B = B.T
w_bom = -1
max_bom_w = 0
for w in range(W):
    bom_w = sum(B[w])
    if max_bom_w < bom_w:
        max_bom_w = bom_w
        w_bom = w

ans = int(max_bom_h + max_bom_w)

# 結果出力
print(ans)

Submission status: Runtime Error

Solution code:
h,w,m=map(int,input().split())
h=[]
w=[]
for i in range(m):
    h1,w1=map(int,input().split())
    h.append(h1)
    w.append(w1)
h_counter=Counter(h)
w_counter=Counter(w)
for i in range(m):
    if h[i]==h_counter.most_common()[0][0] and w[i]==w_counter.most_common()[0][0]:
        print(h_counter.most_common()[0][1]+w_counter.most_common()[0][1]-1)
        exit()
else:
    print(h_counter.most_common()[0][1]+w_counter.most_common()[0][1])

Submission status: Runtime Error

Solution code:
from collections import Counter
h,w,m = map(int,input().split())
lir = []
lic = []
ans = 0
for _ in range(m):
    x,y = map(int,input().split())
    lir.append(x)
    lic.append(y)
maxr = Counter(lir).most_common()[0][0]
while maxr in lir:
    ans+=1
    ind = lir.index(maxr)
    lir.pop(ind)
    lic.pop(ind)
maxc = Counter(lic).most_common()[0][0]
while maxc in lic:
    ans+=1
    ind = lic.index(maxc)
    lic.pop(ind)
print(ans)