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: Time Limit Exceeded

Solution code:
import sys
h,w,m=map(int,input().split())
hw = []
H=[0 for _ in range(h)]
W=[0 for _ in range(w)]
for _ in range(m):
    i,j = [int(i) for i in input().split()]
    hw.append((i,j))
    H[i-1] += 1
    W[j-1] += 1


hm = max(H)
wm = max(W)
hmi = [i for i, _ in enumerate(H) if _ == hm]
wmi = [i for i, _ in enumerate(W) if _ == wm]

for h in hmi:
    for w in wmi:
        if not (h+1,w+1) in hw:
            print(hm+wm)
            sys.exit()
else:
    print(hm+wm-1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
H, W, M = list(map(int, input().split()))
row = np.zeros(H)
col = np.zeros(W)
maps = np.zeros((M, 2), dtype=np.int64)
for i in range(M):
    h, w =list(map(int, input().split()))
    row[h-1] += 1
    col[w-1] += 1
    maps[i] = [h - 1, w - 1]

rowMax = row.max()
colMax = col.max()
rowMaxs = np.where(row == rowMax)[0]
colMaxs = np.where(col == colMax)[0]
c = 0
d = 0
dc = rowMaxs.size * colMaxs.size
for i in maps:
    if np.any(rowMaxs==i[0]) and np.any(colMaxs==i[1]):
        d += 1
        if d == dc:
            c = 1
            break
    

print(int(rowMax + colMax - c))

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
boms = [[0,0] for i in range(m)]
Hs = [0] * h
Ws = [0] * w
for i in range(m):
  _h, _w = map(int, input().split())
  boms[i][0] = _h - 1
  boms[i][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
if m < len(Hindexs) * len(Windexs):
  print(Hmax + Wmax)
  exit()
for hindex in Hindexs:
  for windex in Windexs:
    if [hindex,windex] not in boms: 
      print(Hmax + Wmax)
      exit()
print(Hmax + Wmax -1)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=list(map(int, input("").split()))
out=0
o=0
hb=[]
wb=[]
for i in range(h):
    hb.append([0,i+1])
for i in range(w):
    wb.append([0,i+1])
bo=[]
for i in range(m):
    a,b=list(map(int, input("").split()))
    hb[a-1][0]+=1
    wb[b-1][0]+=1
    bo.append([a,b])
list.sort(hb, reverse=True)
list.sort(wb, reverse=True)
for i in hb:
    if i[0] ==hb[0][0]:
        for j in wb:
            if j[0] ==wb[0][0]:
                o=i[0]+j[0]
                if [i[1],j[1]] in bo:
                    o-=1
                if o>out:
                    out=o
            else:
                break
    else:
        break
print(out)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

H,W,N = [int(i) for i in input().split()]
varriers = [[int(i)-1 for i in input().split()] for i in range(N)]
board = np.array([[0 for _ in range(W)] for i in range(H)], dtype=int)

for varrier in varriers:
    for x in range(W):
        board[varrier[0]][x] += 1
    for y in range(H):
        if y == varrier[0]:
            continue
        board[y][varrier[1]] += 1


print(board.max())

Submission status: Time Limit Exceeded

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 _ in range(M):
    P.append(list(map(int,input().split())))
for m in range(M):
    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 range(M):
    if P[k][0] in max_X and P[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: Time Limit Exceeded

Solution code:
h, w, m =  map(int, input().split())
bom_list = []
h_list = [0] * h
w_list = [0] * w

for i in range(m):
    a, b =  map(int, input().split())
    bom_list.append([a - 1, b - 1])
    h_list[a - 1] += 1
    w_list[b - 1] += 1

max_h = max(h_list)
max_h_list = [i for i, x in enumerate(h_list) if x == max_h]
max_w = max(w_list)
max_w_list = [i for i, x in enumerate(w_list) if x == max_w]
ans = max_h + max_w
count = 0

for i in range(len(max_h_list)):
    for j in range(len(max_w_list)):
        if [max_h_list[i], max_w_list[j]] in bom_list:
            count += 1

if len(max_h_list) * len(max_w_list) == count:
    ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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)
ci = [i for i, x in enumerate(c) if x == cm]
ri = [i for i, x in enumerate(r) if x == rm]
ans = cm + rm

for i in ri:
    for j in ci:
        if [i+1,j+1] not in l:
            print(int(ans))
            exit()

print(int(ans)-1)

Submission status: Time Limit Exceeded

Solution code:
H, W, M = map(int, input().split())
if H==1 and W == 1:
  print(1)
  exit()
hwlist = [list(map(int, input().split())) for i in range(M)]
a = []
b = []
for h in range(H):
    a.append(0)
for w in range(W):
    b.append(0)
for m in range(M):
    a[hwlist[m][0]-1] += 1
    b[hwlist[m][1]-1] += 1
max_a = max(a)
a_max_id = [i for i, v in enumerate(a) if v == max_a]
max_b = max(b)
b_max_id = [i for i, v in enumerate(b) if v == max_b]
for i in range(len(a_max_id)):
    for j in range(len(b_max_id)):
        if not [a_max_id[i]+1, b_max_id[j]+1] in hwlist:
            print(max_a + max_b)
            exit()
print(max_a + max_b - 1)

Submission status: Time Limit Exceeded

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

targets = []
n_target_h = [0] * H
n_target_w = [0] * W

h_max = 0
w_max = 0
for i in range(M):
    h, w = map(int, input().split())
    targets.append((h, w))
    n_target_h[h - 1] += 1
    n_target_w[w - 1] += 1
    h_max = max(h_max, n_target_h[h - 1])
    w_max = max(w_max, n_target_w[w - 1])

bomb_h = [i + 1 for i, h in enumerate(n_target_h) if h == h_max]
bomb_w = [i + 1 for i, w in enumerate(n_target_w) if w == w_max]

ans = h_max + w_max - 1
for h in bomb_h:
    for w in bomb_w:
        if not (h, w) in targets:
            ans = h_max + w_max
            break
    else:
        continue
    break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
import numpy as np
import sys
h,w,m = (int(x) for x in input().split())
hw = []
for _ in range(m):
    hw.append([int(x) for x in input().split()])

np_hw = np.array(hw)

c_h = collections.Counter(np_hw[:,0])
c_w = collections.Counter(np_hw[:,1])


max_h_list = [h_key for h_key in c_h.keys() if c_h[h_key] == c_h.most_common()[0][1]]
max_w_list = [w_key for w_key in c_w.keys() if c_w[w_key] == c_w.most_common()[0][1]]

ans = c_h.most_common()[0][1]+c_w.most_common()[0][1]
for i in max_h_list:
    for j in max_w_list:
        if [i,j] not in hw:
            print(ans)
            sys.exit()

ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
mapa = {}
strings = [0]*h
rows = [0]*w
for i in range(m):
    h1, w1 = map(int, input().split())
    if not mapa.get(h1-1, 0):
        mapa[h1-1] = []
    mapa[h1-1].append(w1-1)
    strings[h1-1] += 1
    rows[w1-1] += 1
maxs = max(strings)
maxr = max(rows)
summa = maxs + maxr - 1
fl = True
for i in range(h):
    if fl and strings[i] == maxs:
        for j in range(w):
            if rows[j] == maxr:
                if j not in mapa.get(i, []):
                    summa = maxs + maxr
                    fl = False
                    break
print(summa)

Submission status: Time Limit Exceeded

Solution code:
import collections
import numpy as np

h, w, m = map(int, input().split())
lst_ = []
for i in range(m):
    h, m = map(int, input().split())
    lst_.append([h, m])

lst = np.array(lst_)
h_lst = collections.Counter(lst[:,0])
w_lst = collections.Counter(lst[:,1])

def get_cand(sub_lst):
    sub_lst = sub_lst.most_common()
    cand = []
    for sub in sub_lst:
        if sub[1] != sub_lst[0][1]:
            break
        cand.append(sub[0])
    return cand, sub_lst[0][1]

h_cand, h_count = get_cand(h_lst)
w_cand, w_count = get_cand(w_lst)

count = 0
for bomb in lst_:
    if bomb[0] in h_cand and bomb[1] in w_cand:
        count += 1

ans = h_count + w_count
if count < len(h_cand)*len(w_cand):
    print(ans)
else:
    print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
[h, w, m] = map(int, input().split())
pos_x = {}
pos_y = {}
dup = {}

for y in range(1, h+1):
  for x in range(1, w+1):
    pos_x[x] = 0
    pos_y[y] = 0

for y in range(1, h+1):
  for x in range(1, w+1):
    dup[str(x) + "_" + str(y)] = False

for i in range(m):
  [y, x] = map(int, input().split())
  dup[str(x) + "_" + str(y)] = True
  if x not in pos_x.keys():
    pos_x[x] = 1
  else:
    pos_x[x] += 1
    
  if y not in pos_y.keys():
    pos_y[y] = 1
  else:
    pos_y[y] += 1
    
max_val = 0
    
for i in range(1, h+1):
  for j in range(1, w+1):
    if dup[str(j) + "_" + str(i)]:
      if max_val < (pos_x[j] + pos_y[i] - 1):
        max_val = pos_x[j] + pos_y[i] - 1
    else:
      if max_val < (pos_x[j] + pos_y[i]):
        max_val = pos_x[j] + pos_y[i]

print(max_val)

Submission status: Time Limit Exceeded

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= [i for i, x in enumerate(hl) if x ==a]
# wmax=[i for i, x in enumerate(wl) if x ==b]
hmax=np.where(hl == hl.max())[0]
wmax=np.where(wl == wl.max())[0]
a=max(hl)
b=max(wl)
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: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
boms = set()
# boms = [[0,0] for i in range(m)]
Hs = [0] * h
Ws = [0] * w
for i in range(m):
  _h, _w = map(int, input().split())
  boms.add((_h - 1,_w - 1))
  # boms[i][0] = _h - 1
  # boms[i][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)
# if m < len(Hindexs) * len(Windexs):
#   print(Hmax + Wmax)
#   exit()
# for hindex in Hindexs:
#   for windex in windexs:
#     if hindex in  and windex):
#       print(Hmax + Wmax)
#       exit()
# print(Hmax + Wmax -1)

Submission status: Time Limit Exceeded

Solution code:
#coding: utf-8
from collections import defaultdict
import sys

H, W, M = (int(x) for x in input().split())

NH = [[0, i] for i in range(H)]
NW = [[0, i] for i in range(W)]

B = []
for i in range(M):
    h, w = (int(x)-1 for x in input().split())
    B.append((h, w))
    NH[h][0] += 1
    NW[w][0] += 1

NH.sort(reverse=True)
NW.sort(reverse=True)
maxH = NH[0][0]
maxW = NW[0][0]

for i in range(H):
    countH, idxH = NH[i] 
    if countH != maxH:
        break

    for j in range(W):
        countW, idxW = NW[j] 
        if countW!= maxW:
            break

        if (idxH, idxW) not in B:
            print(maxH + maxW)
            sys.exit(0)

print(maxH + maxW - 1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

h, w, m= map(int, input().split())
#ans=np.zeros((h, w), dtype='int8')
line=np.zeros(h, dtype='int8')
colm=np.zeros(w, dtype='int8')
locline=np.zeros(m,dtype='int8')
loccolm=np.zeros(m,dtype='int8')

for i in range(0,m):
  hi, wi = map(int, input().split())
  line[hi-1]=line[hi-1]+1 
  colm[wi-1]=colm[wi-1]+1 
  locline[i]=hi-1
  loccolm[i]=wi-1
    
#colm=np.sum(ans, axis=0)
#line=np.sum(ans, axis=1)

colmarg=np.argsort(-colm)
linearg=np.argsort(-line)

hhh=int(h/2)
www=int(w/2)
if (h < 1000): hhh=h
if (w < 1000): www=w

ataimax=0
for ih in linearg[0:hhh]:
  for iw in colmarg[0:www]:
    cho=0
    for i in range(0,m):
      if ((locline[i] == ih) and (loccolm[i] == iw)): cho=1
    atai=colm[iw]+line[ih]-cho
    if (atai >= ataimax): ataimax=atai

print (ataimax)

Submission status: Time Limit Exceeded

Solution code:
def main():
    height, width, target_count = [int(x) for x in input().split()]
    count_by_height = [0] * height
    count_by_width = [0] * width
    bomb_is = [[False] * width for _ in range(height)]
    for _ in range(target_count):
        bom_h, bom_w = [int(x) - 1 for x in input().split()]
        count_by_height[bom_h] += 1
        count_by_width[bom_w] += 1
        bomb_is[bom_h][bom_w] = True
    max_h, max_w = max(count_by_height), max(count_by_width)
    max_h_is = [i for i, x in enumerate(count_by_height) if x == max_h]
    max_w_is = [i for i, x in enumerate(count_by_width) if x == max_w]
    no_cross = any(not bomb_is[hh][ww] for hh in max_h_is for ww in max_w_is)
    return max_h + max_w - (not no_cross)


if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int,input().split())
tate=[0]*(h+1)
yoko=[0]*(w+1)
box=[[] for i in range(h+1)]

for i in range(m):
    a, b = map(int, input().split())
    tate[a] += 1
    yoko[b] += 1
    box[a].append(b)
tmax = max(tate)
ymax = max(yoko)
tbox = [i for i, v in enumerate(tate) if v == tmax]
ybox = [i for i, v in enumerate(yoko) if v == ymax]

flag = True
for i in tbox:
    for j in ybox:
        if not(j in box[i]):
           flag = False
#print(tmax,ymax,tbox,ybox,flag,box)
if flag:
    print(tmax + ymax - 1)
else:
    print(tmax + ymax)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

def argmax_poss(a):
    if len(a) == 0:
        return []
    all_ = [0]
    max_ = a[0]
    for i in range(1, len(a)):
        if a[i] > max_:
            all_ = [i]
            max_ = a[i]
        elif a[i] == max_:
            all_.append(i)
    return all_

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

h_scores = [0 for i in range(h)]
w_scores = [0 for i in range(w)]

record = set()

for i in range(m):
  h,w = map(int,input().split())
  record.add((h-1,w-1))
  h_scores[h-1] += 1
  w_scores[w-1] += 1

max_h_poss = argmax_poss(h_scores)
max_w_poss = argmax_poss(w_scores)

out_pos_count = 0 

is_ok = False

for h in max_h_poss:
    for w in max_w_poss:
        if (h,w) not in record:
            is_ok = True
            # break
        
if is_ok:
    print(max(h_scores) + max(w_scores))
else:
    print(max(h_scores) + max(w_scores)-1)

Submission status: Time Limit Exceeded

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 _ in range(M):
    P.append(list(map(int,input().split())))
for m in range(M):
    P_X[P[m][0]]+=1
for m in range(M):
    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 range(M):
    if P[k][0] in max_X and P[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: Time Limit Exceeded

Solution code:
H,W,M = map(int,input().split())
P = [0]*(H+1)
Q = [0]*(W+1)
A = []
for k in range(M):
  a,b = map(int,input().split())
  P[a] += 1
  Q[b] += 1
  A.append((a,b))
Pindex = [i for i, v in enumerate(P) if v == max(P)]
Qindex = [i for i, v in enumerate(Q) if v == max(Q)]
Pmax = P[Pindex[0]]
Qmax = Q[Qindex[0]]
ans = Pmax+Qmax-1
key = 0
for x in Pindex:
  for y in Qindex:
    if (x,y) not in A:
      ans += 1
      key = 1
      break
  if key == 1:
    break
print(ans)

Submission status: Time Limit Exceeded

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

area = []
w_list = []

bh_list = []
bw_list = []

max = 0


for i in range(h):
    w_list = []
    for j in range(w):
        w_list.append(".")

    area.append(w_list)

for num in range(m):
    t_h, t_w = map(int, input().split())

    area[t_h-1][t_w-1] = "#"
    bh_list.append(t_h-1)
    bw_list.append(t_w-1)





for i in range(h):
    for j in range(w):
        if i in bh_list and j in bw_list:
            count = 0
            for width in range(w):
                if area[i][width] == "#":
                    count += 1
            for height in range(h):
                if area[height][j] == "#":
                    count += 1
            

            if area[i][j] == "#":
                count -= 1

            if count > max:
                max = count


print(max)

Submission status: Time Limit Exceeded

Solution code:
H, W, M = map(int, input().split())
h = [0] * (H+1)
w = [0] * (W+1)
l = set()
for i in range(M):
  x, y = map(int, input().split())
  h[x] += 1
  w[y] += 1
  l.add((x, y))
  
a = [i for i, x in enumerate(h) if x == max(h)]
b = [i for i, x in enumerate(w) if x == max(w)]

flag = False
for s in a:
  for t in b:
    if (s, t) not in l:
      flag = True
      break
  if flag:
    break
ans = max(h)+max(w)
if not flag:
  ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

bom=[[0 for i in range(w+1)] for i in range(h+1)]
 
for i in range(m):
  a,b=list(map(int,input().split()))
  bom[a][b]=1
 
 
from itertools import accumulate
for i in range(h+1):
  bom[i]=list(accumulate(bom[i]))
 
 
for i in range(1,h+1):
  for j in range(1,w+1):
    bom[i][j]=bom[i][j]+bom[i-1][j]

ans=0  
for i in range(1,h+1):
  for j in range(1,w+1):

    ans=max(ans,bom[h][w]-(bom[i-1][j-1]+bom[i-1][w]-bom[i-1][j]+bom[h][j-1]-bom[i][j-1]+bom[h][w]-bom[i][w]-bom[h][j]+bom[i][j]))
    
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
hw = []
h_cnt = [0] * h
w_cnt = [0] * w
hw = []
hw_set = set()
for _ in range(m):
    r, c = map(int, input().split())
    r -= 1
    c -= 1
    h_cnt[r] += 1
    w_cnt[c] += 1
    hw.append((r, c))
    hw_set.add((r, c))
mh = max(h_cnt)
mw = max(w_cnt)
ans = mh + mw
is_exist = False
for i in range(h):
    if h_cnt[i] != mh:
        continue
    for j in range(w):
        if w_cnt[j] == mw:
            if not (i, j) in hw_set:
                is_exist = True
                print(ans)
                exit()
print(ans - 1)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

# from typing import Optional
from typing import List

# from typing import Dict
from typing import Tuple

# from typing import Sequence

Cod = Tuple[int, int]


def main():
    args = input().split()
    H = int(args[0])
    W = int(args[1])
    M = int(args[2])

    codList: List[Cod] = []
    for i in range(M):
        args = input().split()
        codList.append((int(args[0]), int(args[1])))

    columnSumList: List[int] = [0] * W
    rowSumList: List[int] = [0] * H

    for item in codList:
        columnSumList[item[1] - 1] += 1
        rowSumList[item[0] - 1] += 1

    maxColumnSum = max(columnSumList)
    maxColumIndexList = [i for i, x in enumerate(columnSumList) if x == maxColumnSum]
    maxRowSum = max(rowSumList)
    maxRowIndexList = [i for i, x in enumerate(rowSumList) if x == maxRowSum]

    result = maxColumnSum + maxRowSum
    for row in maxRowIndexList:
        for column in maxColumIndexList:
            if (row + 1, column + 1) in codList:
                continue
            else:
                print(result)
                return

    print(result - 1)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
H, W, M = map(int, input().split())
h = [0] * (H+1)
w = [0] * (W+1)
l = set()
for i in range(M):
  x, y = map(int, input().split())
  h[x] += 1
  w[y] += 1
  l.add((x, y))
  
a = [i for i, x in enumerate(h) if x == max(h)]
b = [i for i, x in enumerate(w) if x == max(w)]

flag = False
for s in a:
  for t in b:
    if (s, t) not in l:
      flag = True
      break
  if flag:
    break
if flag:
  ans = max(h)+max(w)
else:
  ans = max(h)+max(w)-1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

def argmax_poss(a):
    if len(a) == 0:
        return []
    all_ = [0]
    max_ = a[0]
    for i in range(1, len(a)):
        if a[i] > max_:
            all_ = [i]
            max_ = a[i]
        elif a[i] == max_:
            all_.append(i)
    return all_

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

h_scores = [0 for i in range(h)]
w_scores = [0 for i in range(w)]

record = deque([])

for i in range(m):
  h,w = map(int,input().split())
  record.append((h-1,w-1))
  h_scores[h-1] += 1
  w_scores[w-1] += 1

max_h_poss = argmax_poss(h_scores)
max_w_poss = argmax_poss(w_scores)

out_pos_count = 0 

is_ok = False

for h in max_h_poss:
    for w in max_w_poss:
        if (h,w) not in record:
            is_ok = True
            break
        
if is_ok:
    print(max(h_scores) + max(w_scores))
else:
    print(max(h_scores) + max(w_scores)-1)

Submission status: Time Limit Exceeded

Solution code:
hi, wi, m = map(int, input().split())
h = [] * m
w = [] * m
for i in range(m):
    a, b = input().split()
    h.append(a)
    w.append(b)
mh = []
mw = []
ha = 0
for i in range(1, hi+1):
    c = h.count(str(i))
    if c > ha:
        ha = c
        mh = [str(i)]
    elif c == ha:
        mh.append(str(i))
wa = 0
for i in range(1, wi+1):
    c = w.count(str(i))
    if c > wa:
        wa = c
        mw = [str(i)]
    elif c == wa:
        mw.append(str(i))
a = 0
for i in range(len(mh)):
    for j in range(len(mw)):
        for k in range(m):
            if mh[i]==h[k] and mw[j]==w[k]:
              a = a + 1
              break
if (a < len(mh*len(mw))):
    print(ha + wa)
else:
    print(ha + wa - 1)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split(' '))
grid = {}
h_b = set()

h_max = 0
w_max = 0
h_max_grid = 0
w_max_grid = 0

h_b = set()
w_b = set()

for i in range(m):
    hi, wi = map(int, input().split(' '))
    grid[(hi, wi)] = 1
    grid[(hi, 0)] = grid.get((hi, 0), 0) + 1
    grid[(0, wi)] = grid.get((0, wi), 0) + 1

    if h_max < grid[(hi,0)]:
        h_max = grid[(hi,0)]

    if w_max < grid[(0,wi)]:
        w_max = grid[(0,wi)]

    t = h_max + w_max
    grid[(0,0)] = t if grid.get((0,0), 0) < t else grid.get((0,0), 0)
    h_b.add(hi)
    w_b.add(wi)

flg = False
for hb in list(h_b):
    for wb in list(w_b):
        if (grid[(hb, 0)] + grid[(0, wb)]) != grid[(0,0)]:
            continue
        
        if grid.get((hb, wb), 0) != 1:
            flg = True
            break
    else:
        continue
    break

if flg:
    print(grid[(0,0)])
else:
    print(grid[(0,0)]-1)

Submission status: Time Limit Exceeded

Solution code:
def E():
    h,w,m = list(map(int, input().split()))
    h_l = []
    w_l = []
    bomb = []
    for _ in range(m):
        h_t,w_t = list(map(int, input().split()))
        h_l.append(h_t)
        w_l.append(w_t)
        bomb.append(str(h_t)+":"+str(w_t))
    import collections
    c_h = collections.Counter(h_l)
    c_w = collections.Counter(w_l)
    ret = 0
    for i in range(1,h+1):
        for j in range(1,w+1):
            flg = 0
            if str(i)+":"+str(j) in bomb:
                flg = -1
            if c_h[i] + c_w[j] + flg > ret:
                ret = c_h[i] + c_w[j] + flg
    print(ret)

E()

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
h_l = [[] for i in range(h + 1)]
w_l = [[] for i in range(w + 1)]
max_h = 0
max_h_l = []
max_w = 0
max_w_l = []
for _ in range(m):
    h, w = map(int, input().split())
    if h_l[h] :
        h_l[h][1].append(w)
        h_l[h][0] += 1
    else:
        h_l[h] = [1, [w]]
        
    if h_l[h][0] > max_h:
        max_h_l = [h]
        max_h = h_l[h][0]
    if h_l[h][0] == max_h:
        max_h_l.append(h)
            

        
    if w_l[w]:
        w_l[w][1].append(h)
        w_l[w][0] += 1
    else:
        w_l[w] = [1, [h]]
    if w_l[w][0] > max_w:
        max_w_l = [w]
        max_w = w_l[w][0]
    if w_l[w][0] == max_w:
        max_w_l.append(w)


flag = 0
ans = 0
for i in max_h_l:
    if flag == 1:
        break
    for j in max_w_l:
        if flag == 1:
            break
        if j in h_l[i][1]:
            ans = max_h + max_w - 1
        else:
            ans = max_h + max_w
            flag = 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W,M = map(int,input().split())
h = [0]*M
w = [0]*M

from collections import defaultdict
counter_h = defaultdict(int)
counter_w = defaultdict(int)

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

MAX_h = max(counter_h.values())
h_list = [k for k, v in counter_h.items() if v == MAX_h]
MAX_w = max(counter_w.values())
w_list = [k for k, v in counter_w.items() if v == MAX_w]


ans = MAX_h+MAX_w
check = 0

for i in range(M):
    if h[i] in h_list and w[i] in w_list:
        check += 1

if len(h_list)*len(w_list)==check:
    ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

h,w,m = na()
hh = [0]*h
ww = [0]*w
d = set()

for i in range(m):
  a,b = na()
  a,b = a-1,b-1
  hh[a]+=1
  ww[b]+=1
  d.add((a,b))

ans = max(hh)+max(ww)-1

nn = [i for i, v in enumerate(hh) if v == max(hh)]
mm = [i for i, v in enumerate(ww) if v == max(ww)]

for p in nn:
  for q in mm:
    if (p,q) not in d:
      ans = max(hh)+max(ww)
      print(ans)
      exit()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
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()
print(mh+mw-1)

Submission status: Time Limit Exceeded

Solution code:
h,w,m = map(int,input().split())
hli = [0]*h
wli = [0]*w
li = []
for i in range(m):
    a,b = map(int,input().split())
    li.append([a,b])
    hli[a-1] += 1
    wli[b-1] += 1
a = max(hli)
b = max(wli)
hli2 = []
wli2 = []
for i in range(h):
    if hli[i] == a:
        hli2.append(i+1)
for i in range(w):
    if wli[i] == b:
        wli2.append(i+1)
for i in range(len(hli2)):
    for j in range(len(wli2)):
        if [hli2[i],wli2[j]] not in li:
            print(a + b)
            exit()
print(a + b - 1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

H, W, M = [int(n) for n in input().split()]
h = []
w = []
table = {}
for m in range(M):
    tmp = [int(n) for n in input().split()]
    h.append(tmp[0])
    w.append(tmp[1])
    if tmp[0] not in table.keys():
        table[tmp[0]] = [tmp[1]]
    else:
        table[tmp[0]].append(tmp[1])

h_counter = Counter(h)
w_counter = Counter(w)

most_common_h_list = []
most_common_w_list = []

most_common_h_count = 0
for k, v in h_counter.most_common():
    if v < most_common_h_count:
        break
    most_common_h_list.append(k)
    most_common_h_count = v

most_common_w_count = 0
for k, v in w_counter.most_common():
    if v < most_common_w_count:
        break
    most_common_w_list.append(k)
    most_common_w_count = v

ans = 0
finished = False
for hh in most_common_h_list:
    if finished:
        break
    for ww in most_common_w_list:
        if ww in table[hh]:
            tt = most_common_h_count + most_common_w_count - 1
            if tt > ans:
                ans = tt
        else:
            tt = most_common_h_count + most_common_w_count
            if tt > ans:
                ans = tt
            finished = True
            break

print(ans)

Submission status: Time Limit Exceeded

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



hl = [[i,0] for i in range(h)]
wl = [[i,0] for i in range(w)]
g = [[0 for i in range(w)] for j in range(h)]


for i in range(len(b)):
    # print(i)
    hl[b[i][0]-1][1] += 1
    wl[b[i][1]-1][1]  += 1
    g[b[i][0]-1][b[i][1]-1] = 1
    



shl = sorted(hl,reverse=True,key=lambda x:x[1])
whl = sorted(wl,reverse=True,key=lambda x:x[1])
mh = max(hl,key=lambda x:x[1])
mw = max(wl, key=lambda x:x[1])



flg = False

for i in shl:
    if i[1] < mh[1]:
        break
    for j in whl:
        if j[1] < mw[1]:
            break       
        if g[i[0]][j[0]] == 0:
            flg = True

ans = mh[1] + mw[1]

if flg:
    print(ans)
else:
    print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

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


print(ans)