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
input = sys.stdin.buffer.readline

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

bomb_h = [0] * H
bomb_w = [0] * W
grid = [[False] * W for _ in range(H)]

max_h = 0
max_w = 0
h_indices = []
w_indices = []

for i in range(M):
    h, w = map(int, input().split())
    bomb_h[h-1] += 1
    bomb_w[w-1] += 1
    if bomb_h[h-1] == max_h:
        h_indices.append(h-1)
    elif bomb_h[h-1] > max_h:
        h_indices = [h-1]
        max_h = bomb_h[h-1]

    if bomb_w[w-1] == max_w:
        w_indices.append(w-1)
    elif bomb_w[w-1] > max_w:
        w_indices = [w-1]
        max_w = bomb_w[w-1]

    grid[h-1][w-1] = True

ans = max_w + max_h - 1
for h in h_indices:
    for w in w_indices:
        if not grid[h][w]:
            ans += 1
            print(ans)
            exit()

if ans == -1:
    ans = 0

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline

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

    H_list = [[0, i] for i in range(H + 1)] 
    W_list = [[0, i] for i in range(W + 1)] 


    S = set()
    S_add = S.add
    INF = 10 ** 6

    for _ in range(M):
        a, b = map(int, input().split())
        tmp = INF * a + b
        S_add(tmp)
        H_list[a][0] += 1
        W_list[b][0] += 1

    ans = 0
    H_list.sort(reverse=True)
    W_list.sort(reverse=True)

    tmp_H = H_list[0][0]
    tmp_W = W_list[0][0]
    tmp_ans = tmp_H + tmp_W

    for h in range(H):
        if H_list[h][0] + 1 < tmp_H:
            continue
        h_ = H_list[h][1]
        H_ = H_list[h][0]
        for w in range(W):
            if W_list[w][0] + 1 < tmp_W:
                break
            w_ = W_list[w][1]
            tmp = H_ + W_list[w][0]
            if INF * h_ + w_ in S:
                tmp -= 1
            ans = max(ans, tmp)
            if ans == tmp_ans:
                print (ans)
                return

    print (ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
H,W,N = list(map(int,input().split()))
A = [list(map(int,input().split())) for _ in range(N)]
from collections import Counter
B,C = Counter(),Counter()
maxB,maxC = 0,0
for a in A:
    B[a[0]] += 1
    C[a[1]] += 1
for b in B:
    if maxB<B[b]:maxB = B[b]
for c in C:
    if maxC<C[c]:maxC = C[c]
def ifmax(array,maxV):
    maxArray = []
    for v in array:
        if array[v] == maxV:maxArray.append(v)
    return maxArray

maxBs = ifmax(B,maxB)
maxCs = ifmax(C,maxC)
# print(B,C,maxBs,maxCs)

sum = B[maxBs[0]] + C[maxCs[0]]
def aaa():
    for b in maxBs:
        for c in maxCs:
            if [b,c] not in A:return 0
    return -1

print(sum+aaa())

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
def main():
    import sys
    input = sys.stdin.readline
    h, w, m = map(int, input().split())
    cnt_h = [0] * h
    cnt_w = [0] * w
    bomb = [[False] * w for _ in range(h)]
    for _ in range(m):
        y, x = map(lambda x:int(x) - 1, input().split())
        cnt_h[y] += 1
        cnt_w[x] += 1
        bomb[y][x] = True
        
    kouho_h = []
    max_h = max(cnt_h)
    max_w = max(cnt_w)
    for i in range(h):
        if cnt_h[i] == max_h:
            kouho_h.append(i)
    kouho_w = []
    for i in range(w):
        if cnt_w[i] == max_w:
            kouho_w.append(i)
    
    flag = False
    for i in kouho_h:
        for j in kouho_w:
            if bomb[i][j] == False:
                flag = True
                break
        else:
            continue
        break
    
    if flag:
        print(max_h + max_w)
    else:
        print(max_h + max_w - 1)



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
HWM = list(map(int, input().split()))
hw = [input().split() for i in range(HWM[2])]

c = [0] * HWM[0]
r = [0] * HWM[1]

for i in range(HWM[2]):
    c[int(hw[i][0]) - 1] += 1
    r[int(hw[i][1]) - 1] += 1

cIndex = [i for i, v in enumerate(c) if v == max(c)]
rIndex = [i for i, v in enumerate(r) if v == max(r)]

def isDouplicate(x, y):
    doupleFlag = False
    for i in range(HWM[2]):
        if (int(hw[i][0]) == (x + 1) and int(hw[i][1]) == (y + 1)):
            return True
    return False

sum = 0
for i in range(len(cIndex)):
    for j in range(len(rIndex)):
        if (isDouplicate(cIndex[i], rIndex[j])):
            if (sum < c[cIndex[i]] + r[rIndex[j]] - 1):
                sum = c[cIndex[i]] + r[rIndex[j]] - 1
        else:
            if (sum < c[cIndex[i]] + r[rIndex[j]]):
                sum = c[cIndex[i]] + r[rIndex[j]]

print(sum)

Submission status: Time Limit Exceeded

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

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

h = {}
w = {}
m = {}
for i in range(M):
    hi, wi = map(int, readline().split())
    if hi not in h:
        h[hi] = 1
        m[hi] = [wi]
    else:
        h[hi] += 1
        m[hi].append(wi)
    if wi not in w:
        w[wi] = 1
    else:
        w[wi] += 1

max_h = max(h.values())
max_w = max(w.values())
max_h_list = [kv[0] for kv in h.items() if kv[1] == max_h]
max_w_list = [kv[0] for kv in w.items() if kv[1] == max_w]

for i in max_h_list:
    for j in max_w_list:
        if j not in m[i]:
            print(max_h + max_w)
            exit()
print(max_h + max_w - 1)

Submission status: Time Limit Exceeded

Solution code:
H, W, N = map(int, input().split())
h_array = {}
w_array = {}
array = []
for _ in range(N):
    h, w = map(int, input().split())
    array.append([h,w])
    if h not in h_array.keys():
      h_array[h] = 1
    else:
    	h_array[h] +=1
    if w not in w_array.keys():
      w_array[w] = 1
    else:
    	w_array[w] +=1
max_h_list = [kv[0] for kv in h_array.items() if kv[1] == max(h_array.values())]
max_w_list = [kv[0] for kv in w_array.items() if kv[1] == max(w_array.values())]
# max_kv_list = [kv for kv in h_array.items() if kv[1] == max(h_array.values())]
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 array:
      count = h_array[max_h_list[i]] + w_array[max_w_list[j]] -1
    else:
      count =h_array[max_h_list[i]] + w_array[max_w_list[j]]
      break
print(count)

Submission status: Time Limit Exceeded

Solution code:
import collections
h, w, m = map(int, input().split())
L_g = []
L_r = []
L = []
for i in range(m):
    a, b = map(int, input().split())
    L_g.append(a)
    L_r.append(b)
    L.append((a, b))
c1 = collections.Counter(L_g)
c2 = collections.Counter(L_r)

L1 = c1.most_common()
L2 = c2.most_common()

s = L1[0][1]
bL1 = []
for i in range(len(L1)):
    if L1[i][1] == s:
        bL1.append(L1[i])
    else:
        break
s = L2[0][1]
bL2 = []
for i in range(len(L2)):
    if L2[i][1] == s:
        bL2.append(L2[i])
    else:
        break
bL = []
for i in range(len(bL1)):
    for j in range(len(bL2)):
        bL.append((bL1[i][0], bL2[j][0]))
flag = 0
for e in bL:
    if e not in L:
        bom = e
        flag = 1
if flag == 0:
    bom = bL[0]

t = L1[0][1]+L2[0][1]

for e in L:
    if e == bom:
        t -= 1
print(t)

Submission status: Time Limit Exceeded

Solution code:
def main():
    h, w, m = map(int, input().split())
    bomb = []
    H = []
    W = []
    dH = {}
    dW = {}
    for i in range(m):
        a, b = map(int, input().split())
        H.append(a)
        if a in dH:
            dH[a] += 1
        else:
            dH[a] = 1
        W.append(b)
        if b in dW:
            dW[b] += 1
        else:
            dW[b] = 1
        bomb.append((a, b))
    maxH = max(dH.values())
    maxH_list = []
    for i in dH:
        if dH[i] == maxH:
            maxH_list.append(i)
    maxW = max(dW.values())
    maxW_list = []
    for i in dW:
        if dW[i] == maxW:
            maxW_list.append(i)
    for i in maxH_list:
        for j in maxW_list:
            if (i, j) not in bomb:
                print(dH[i] + dW[j])
                break
        else:
            continue
        break
    else:
        print(dH[maxH_list[0]] + dW[maxW_list[0]] - 1)

main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from itertools import groupby


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def ilen(ll):
    return sum(1 for _ in ll)


def solve():
    H, W, M = read_ints()
    hw = []
    for _ in range(M):
        hw.append(read_ints())
    hw.sort()

    max_ws = {}
    max_count_ws = 0
    for h, ws in groupby(hw, key=lambda pair: pair[0]):
        ws = list(ws)
        max_count_ws = max(max_count_ws, len(ws))
        max_ws[h] = [w for _, w in ws]
    max_ws = {k: max_ws[k] for k in max_ws if len(max_ws[k]) == max_count_ws}

    hw.sort(key=lambda pair: pair[1])
    max_hs = {}
    max_count_hs = 0
    for w, hs in groupby(hw, key=lambda pair: pair[1]):
        hs = list(hs)
        max_count_hs = max(max_count_hs, len(hs))
        max_hs[w] = [h for h, _ in hs]
    max_hs = {k: max_hs[k] for k in max_hs if len(max_hs[k]) == max_count_hs}

    counter = Counter()
    for v in max_hs.values():
        counter += Counter(v)
    for k in max_ws.keys():
        if counter[k] != len(max_hs):
            return max_count_ws+max_count_hs
    return max_count_ws+max_count_hs-1


if __name__ == '__main__':
    print(solve())

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
H , W , M = map(int,input().split())
G1 = [0]*H
G2 = [0]*W
h = [0]*M
w = [0]*M
for i in range(M):
  h[i] , w[i] = map(int,input().split())
  temp = h[i] - 1
  G1[temp] += 1
  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 = deque()
M2L = deque()
for i in range(H):
  if G1[i] == M1:
    M1L.appendleft(i)
for j in range(W):
  if G2[j] == M2:
    M2L.appendleft(j)
c = 0
M1L = list(M1L)
M2L = list(M2L)
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: Time Limit Exceeded

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())
  
h_cnt=[]
w_cnt=[]
h_set=list(set(h))
w_set=list(set(w))
for i in h_set:
  h_cnt.append(h.count(i))
for i in w_set:
  w_cnt.append(w.count(i))
  
max_h_cnt=max(h_cnt)
max_w_cnt=max(w_cnt)

h_indices=[i for i, x in enumerate(h_cnt) if x ==max_h_cnt]
w_indices=[i for i, x in enumerate(w_cnt) if x ==max_w_cnt]

h_max=[h_set[i] for i in h_indices]
w_max=[w_set[i] for i in w_indices]

flag=True
for i in h_max:
  for j in w_max:
    if (i,j) not in list(zip(h,w)):
      print(max_h_cnt+max_w_cnt)
      flag=False
      break

if flag:
  print(max_h_cnt+max_w_cnt-1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

H, W, M = [int(n) for n in input().split()]
h = []
w = []
for m in range(M):
    tmp = [int(n) for n in input().split()]
    h.append(tmp[0])
    w.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:
        for m in range(M):
            if h[m] == hh and w[m] == ww:
                tt = most_common_h_count + most_common_w_count - 1
                if tt > ans:
                    ans = tt
                break
        else:
            tt = most_common_h_count + most_common_w_count
            if tt > ans:
                ans = tt
            break
            finished = True
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
h,w,m = map(int, input().split())
h_t = []
w_t = []
s = []
for i1 in range(h):
  h_t.append(0)
for i2 in range(w):
  w_t.append(0)
for i in range(h):
  s.append([])
  for j in range(w):
    s[i].append(0)
for i3 in range(m):
  hi,wi = map(int, input().split())
  h_t[hi-1] += 1
  w_t[wi-1] += 1
  s[hi-1][wi-1] = 1
hmax = max(h_t)
wmax = max(w_t)
hmaxi = [i for i, v in enumerate(h_t) if v == max(h_t)]
wmaxi = [i for i, v in enumerate(w_t) if v == max(w_t)]
for ht in hmaxi:
  for wt in wmaxi:
    if s[ht][wt] == 0:
      print(hmax + wmax)
      sys.exit()
print(hmax + wmax-1)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int, input().split())
from collections import defaultdict
hd=defaultdict(int)
wd=defaultdict(int)
hd2=defaultdict(list)
wd2=defaultdict(list)
for i in range(m):
    h0,w0=map(int, input().split())
    hd[h0]+=1
    hd2[h0].append(w0)
    wd[w0]+=1
    wd2[w0].append(h0)

max_h_list = [kv[0] for kv in hd.items() if kv[1] == max(hd.values())]
max_w_list = [kv[0] for kv in wd.items() if kv[1] == max(wd.values())]

ans=float('inf')
for x in max_h_list:
    hlist=hd2[x]
    for y in max_w_list:
        if y in hlist:
            ans=min(ans,1)
        else:
            ans=min(ans,0)

print(hd[max_h_list[0]]+wd[max_w_list[0]]-ans)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
row_list = [0 for _ in range(h)]
col_list = [0 for _ in range(w)]
log = []

for i in range(m):
  hb, wb = map(int, input().split())
  log.append([hb-1, wb-1])
  row_list[hb-1] += 1
  col_list[wb-1] += 1

r_max_val = max(row_list)
c_max_val = max(col_list)
r_max_idx = [i for i, x in enumerate(row_list) if x == max(row_list)]
c_max_idx = [i for i, x in enumerate(col_list) if x == max(col_list)]

flag = False
for ri in r_max_idx:
  for ci in c_max_idx:
    tmp_flag = True
    for i in range(m):
      hb, wb = log[i]
      if ci == wb and ri == hb:
        tmp_flag = False
    if tmp_flag:
      flag = True
if flag:
  print(r_max_val + c_max_val)
else:
  print(r_max_val + c_max_val - 1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
h,w,m=map(int,input().split())
l=[]
 
lh,lw=[],[]
 
for i in range(h):
    lh.append(0)
for i in range(w):
    lw.append(0)

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

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]

for i in range(len(lhml)):
    for j in range(len(lwml)):
        k=[]
        k.append(lhml[i]+1)
        k.append(lwml[j]+1)
        l.append(k)

l_arr = np.unique(l, axis=0)
if len(l_arr)==m:print(lhm+lwm-1)
else:print(lhm+lwm)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
targets = []
strings = [[0, i+1] for i in range(h)]
rows = [[0, i+1] for i in range(w)]
for i in range(m):
    h1, w1 = map(int, input().split())
    targets.append([h1, w1])
    strings[h1-1][0] += 1
    rows[w1-1][0] += 1
rows.sort(reverse=True)
strings.sort(reverse=True)
maxs = strings[0][0]
maxr = rows[0][0]
summa = maxs + maxr - 1
for i in range(h):
    if strings[i][0] == maxs:
        for j in range(w):
            if rows[j][0] == maxr:
                x = maxs + maxr
                if [strings[i][1], rows[j][1]] in targets:
                    x -= 1
                if x > summa:
                    summa = x
                    print(summa)
                    exit()
            else:
                break
    else:
        break
print(summa)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import itertools

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


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

rows = [0]*(H+1)
cols = [0]*(W+1)

def get_total(row, col):
    total = 0
    for m in ms:
        if (m[0] == row) or (m[1] == col):
            total += 1
            pd(f"o:{m[0]=} {m[1]=}")
        else:
            pd(f"x:{m[0]=} {m[1]=}")
    return total

for m in ms:
    pd(f"{m=}")
    rows[m[0]] += 1
    cols[m[1]] += 1

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

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

max_total = 0
p = itertools.product(max_rows, max_cols)
for v in p:
    max_total = max(get_total(v[0],v[1]), max_total)

print(max_total)

Submission status: Time Limit Exceeded

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

m_s = []
num_h = [0 for _ in range(h)]
num_w = [0 for _ in range(w)]

for _ in range(m):
    tmp = list(map(int, input().split()))
    m_s.append(tmp)
    num_h[tmp[0] - 1] += 1
    num_w[tmp[1] - 1] += 1

max_h = [i for i, v in enumerate(num_h) if v == max(num_h)]
max_w = [i for i, v in enumerate(num_w) if v == max(num_w)]

candidate = []

for H in max_h:
    for W in max_w:
        candidate.append([H + 1, W + 1])

ans = max(num_h) + max(num_w) - 1

for can in candidate:
    if can in m_s:
        continue
    else:
        ans += 1
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

h,w,m=map(int,input().split())
N=[[0]*w for i in range(h)]
for i in range(m):
    a,b=map(int,input().split())
    N[a-1][b-1]=1
m_0=[0]*h
m_1=[0]*w
for i in range(h):
    for j in range(w):
        m_0[i]+=N[i][j]
        m_1[j]+=N[i][j]
l_0=[i for i,x in enumerate(m_0) if x==max(m_0)]
l_1=[i for i,x in enumerate(m_1) if x==max(m_1)]
ans=[]

for i in l_0:
    for j in l_1:
        if N[i][j]==1:
            ans.append(max(m_1)+max(m_0)-1)
        else:
            ans.append(max(m_0)+max(m_1))
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
H,W,M = map(int,input().split())
Hinx = [0 for _ in range(H+1)]
Winx = [0 for _ in range(W+1)]
Hexist = [0 for _ in range(M)]
Wexist = [0 for _ in range(M)]
for i in range(M):
    h,w = map(int,input().split())
    Hinx[h] += 1
    Winx[w] += 1
    Hexist[i] = h
    Wexist[i] = w

HMinx = []
WMinx = []

Hmax = max(Hinx)
Wmax = max(Winx)
ans = Hmax+Wmax-1
for i in range(1,H+1):
    if Hinx[i] == Hmax:
        HMinx.append(i)
for i in range(1,W+1):
    if Winx[i] == Wmax:
        WMinx.append(i)

flag = False
for i in HMinx:
    for j in WMinx:
        flag2 = False
        for k in range(M):
            if Hexist[k] == i and Wexist[k] == j:
                flag2 = True
                break
        if flag2 == False:
            flag = True
            ans += 1
            break
    if flag == True:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
h, w, m = map(int, input().split())
hw =[list(map(int, input().split())) for i in range(m)]
hh = [0]*m
ww = [0]*m
for i in range(m):
  hh[i] = hw[i][0]
  ww[i] = hw[i][1]


hcnt = collections.Counter(hh).most_common()[0][1]
wcnt = collections.Counter(ww).most_common()[0][1]
ans = hcnt +wcnt

hhh = []
www = []
for i in range(len(collections.Counter(hh))):
  if hcnt == collections.Counter(hh).most_common()[i][1]:
    hhh.append(collections.Counter(hh).most_common()[i][0])
for i in range(len(collections.Counter(ww))):
  if wcnt == collections.Counter(ww).most_common()[i][1]:
    www.append(collections.Counter(ww).most_common()[i][0])

    
newhw = [[i,j] for i in hhh for j in www]
if all(i in hw for i in newhw):
  ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import heapq

def main():
    H, W, M = map(int, readline().split())
    m = map(int,read().split())
    hw = list(zip(m,m))

    bR = [0] * (H+1)
    bC = [0] * (W+1)

    maxes = []
    x = [[False] * (W+1) for i in range(H+1)]

    for (h, w) in hw:
        bR[h] += 1
        bC[w] += 1
        x[h][w] = True

    rmax = max(bR)
    cmax = max(bC)
    rmis = [i for i in range(H+1) if bR[i] == rmax]
    cmis = [i for i in range(W+1) if bC[i] == cmax]

    ans = rmax + cmax
    minus_flag = True
    for r in rmis:
        for c in cmis:
            if not x[r][c]:
                minus_flag = False
                break
    if minus_flag:
        ans -= 1

    
    # print(hw)
    # print(bR, bC)
    # print(rmax, cmax, rmis, cmis)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections
import sys

*data, = map(int, sys.stdin.read().split()[::-1])

def inp():
    return data.pop()

h, w, m = inp(), inp(), inp()


targets = set()
xs = []
ys = []
for _ in range(m):
    y, x = inp(), inp()
    # y -= 1
    # x -= 1
    ys.append(y)
    xs.append(x)
    targets.add((y, x))

x_stat = collections.Counter(xs)
y_stat = collections.Counter(ys)

xs = sorted(set(xs), key=lambda obj: x_stat[obj], reverse=True)
ys = sorted(set(ys), key=lambda obj: y_stat[obj], reverse=True)

mx_x = x_stat[xs[0]]
mx_y = y_stat[ys[0]]
while xs and x_stat[xs[-1]] < x_stat[xs[0]]:
    xs.pop()
while ys and y_stat[ys[-1]] < y_stat[ys[0]]:
    ys.pop()
best_res = 0
# print(xs, x_stat)
# print(ys, y_stat)
for x in xs:
    for y in ys:
        cur = x_stat[x] + y_stat[y] - ((y, x) in targets)
        best_res = max(cur, best_res)
print(best_res)

Submission status: Time Limit Exceeded

Solution code:
H,W,M = map(int,input().split())
T = [0] * M
H_cnt = {}
W_cnt = {}
HW = {}
for i in range(M):
    h,w = map(int,input().split())
    if not h in H_cnt:
        H_cnt[h] = 0
    if not w in W_cnt:
        W_cnt[w] = 0
    H_cnt[h] += 1
    W_cnt[w] += 1
    HW[(h,w)] = 1

HL = [[h,cnt] for h,cnt in H_cnt.items()]
WL = [[w,cnt] for w,cnt in W_cnt.items()]

HL.sort(key=lambda x: x[1],reverse=True)
WL.sort(key=lambda x: x[1],reverse=True)

HS = []
max_cnt = 0
for h,cnt in HL:
    if max_cnt<=cnt:
        HS.append([h,cnt])
        max_cnt = max(max_cnt,cnt)
        continue
    break

WS = []
max_cnt = 0
for w,cnt in WL:
    if max_cnt<=cnt:
        WS.append([w,cnt])
        max_cnt = max(max_cnt,cnt)
        continue
    break

cnt = 0
ans = 0

# ここの判定を高速にする
#cross_cnt = 0
#for hw in HW.keys():
#    h,w = hw
#    if h in HS and w in WS:
#        cross_cnt+=1


for h,hcnt in HS:
    for w,wcnt in WS:
        if (h,w) in HW:
            cnt = hcnt+wcnt-1
            ans = max(ans,cnt)
            continue
        cnt = hcnt+wcnt
        ans = max(ans,cnt)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int,input().split())
nh=[[_] for _ in range(w)] #各wに対するhの値
nw=[[_] for _ in range(h)]
num=[]
for i in range(m):
    h2,w2=map(int,input().split())
    nh[w2-1].append(i)
    nw[h2-1].append(i)
    num.append([h2,w2])
for i in range(w):
    nh[i].append(len(nh[i])-1)
for i in range(h):
    nw[i].append(len(nw[i])-1)
nh.sort(reverse=True,key=lambda x: x[-1])
nw.sort(reverse=True,key=lambda x: x[-1])
ans=0
for i in range(w):
    if nh[i][-1]+1<nh[0][-1]:
        break
    for j in range(h):
        if nw[j][-1]+1<nw[0][-1]:
            break
        num10=nh[i][-1]+nw[j][-1]
        if [nw[j][0]+1,nh[i][0]+1] in num:
            num10-=1
        ans=max(ans,num10)
print(ans)

Submission status: Time Limit Exceeded

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

bomb_list = []

for i in range(m):
    h_, w_ = map(int, input().split())
    h_ -= 1
    w_ -= 1
    h_cnt[h_] += 1
    w_cnt[w_] += 1
    bomb_list.append((h_, w_))

max_h = max(h_cnt)
max_h_idx = []
for i in range(h):
    if h_cnt[i] == max_h:
        max_h_idx.append(i)

max_w = max(w_cnt)
max_w_idx = []
for i in range(w):
    if w_cnt[i] == max_w:
        max_w_idx.append(i)

minus = -1
for i in max_h_idx:
    for j in max_w_idx:
        if (i, j) not in bomb_list:
            print(max_h + max_w)
            exit()
print(max_h + max_w - 1)

Submission status: Time Limit Exceeded

Solution code:
#from functools import lru_cache
import sys
from itertools import product,combinations
from functools import reduce,lru_cache
import heapq

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def list_t(l): return vfunc(lambda *args:args)(*l)

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

s=rvfunc(int)(reader())
h,w,m=s[0]
hl,wl = [0]*h,[0]*w
hw=s[1:]
hh,ww = list_t(hw)
for i in range(len(hw)):
    hl[hh[i]-1]+=1
    wl[ww[i]-1]+=1
hlmax,wlmax = vfunc(max)([hl,wl])
hlidx = tuple(filter(lambda a:hl[a]==hlmax,range(len(hl))))
wlidx = tuple(filter(lambda a:wl[a]==wlmax,range(len(wl))))
minus = 1
if len(hlidx)*len(wlidx)>len(hw): minus=0
else:
    fltd = tuple(filter(lambda a:a[0]-1 in hlidx and a[1]-1 in wlidx,hw))
    if len(fltd)!=len(hlidx)*len(wlidx): minus=0
#print(fltd,hw)
# for i in hlidx:
#     for j in wlidx:
# #        print([i+1,j+1],fltd)
#         if not [i+1,j+1] in fltd:
#             minus=0
#             break
print(max(hl)+max(wl)-minus)

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

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(n_target_h)
w_max = max(n_target_w)

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:
from collections import Counter
H,W,M = list(map(int,input().split()))

counterH = Counter()
counterW = Counter()
targets = set()
for i in range(M):
    h,w = list(map(int,input().split()))
    counterH[h] += 1
    counterW[w] += 1
    targets.add((h,w))

max_w = 0
max_h = 0
if M == 1:
    print(1)
else:
    ans = 2
    flg = False
    for count_H in counterH.most_common():
        max_h = max(max_h,count_H[1])
        if max_h != count_H[1]:
            break
        for count_W in counterW.most_common():
            max_w = max(max_w,count_W[1])
            if max_w != count_W[1]:
                break
            # print(count_H,count_W)
            if (count_H[0],count_W[0]) in targets:
                ans = max(ans,count_H[1]+count_W[1]-1)
            else:
                ans = max(ans,count_H[1]+count_W[1])
                flg = True
            if flg:
                break
        if flg:
            break
    print(ans)

Submission status: Time Limit Exceeded

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

Hmax = max(Hlis)
Hidx = [i+1 for i, v in enumerate(Hlis) if v == Hmax]
Wmax = max(Wlis)
Widx = [i+1 for i, v in enumerate(Wlis) if v == Wmax]

for i in range(len(Hidx)):
    for j in range(len(Widx)):
        if [Hidx[i], Widx[j]] not in lis:
            print(Hmax+Wmax)
            sys.exit()
else:
    print(Hmax + Wmax -1)

Submission status: Time Limit Exceeded

Solution code:
H, W, M = map(int, input().split())
Hdst = [0]*H
Wdst = [0]*W
DST = [[0] * 2 for i in range(M)]
for i in range(M):
    hi, wi = map(int, input().split())
    Hdst[hi-1] += 1
    Wdst[wi-1] += 1
    DST[i] = [hi-1, wi-1]

Hdstmax = max(Hdst)
Wdstmax = max(Wdst)
Hbomb = [i for i, x in enumerate(Hdst) if x == Hdstmax]
Wbomb = [i for i, x in enumerate(Wdst) if x == Wdstmax]

ans = Hdstmax + Wdstmax - 1
for i in Hbomb:
    for j in Wbomb:
        if [i, j] not in DST:
            ans = Hdstmax + Wdstmax
            break
    if ans == Hdstmax + Wdstmax:
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np
import collections


H,W,M = map(int, input().split())
bomb = np.array(list(list(map(int, input().split())) for num in range(M)))
h = [0] *H
w = [0] * W
for num in range(M):
  h[bomb[num][0] - 1] += 1
  w[bomb[num][1] - 1] += 1
chy = 0
chx = 0

for num in range(H):
  if h[num]> chy:
    chy =h[num]
for num in range(W):
  if w[num]> chx:
    chx =w[num]
    
x = []
y = []
for num in range(H):
  if h[num] == chy:
    y += [num + 1]
    
for num in range(W):
  if w[num] == chx:
    x += [num + 1]  


maxn = 0
test = 0
for i in range(len(x)):
  for j in range (len(y)):
    test = 0
    for num in range(M):
      if (y[j] == bomb[num][0] or x[i] == bomb[num][1]):
        test+=1;
    if (test > maxn):
      maxn = test
      
print(maxn)

Submission status: Time Limit Exceeded

Solution code:
H,W,M=map(int, input().split())
hh=[0 for i in range(H)]
ww=[0 for j in range(W)]
cord=[]
for i in range(M):
    h,w=map(int,input().split())
    cord.append([h-1,w-1])
    hh[h-1]+=1
    ww[w-1]+=1
mxh=max(hh)
mxw=max(ww)
mh=[i for i,x in enumerate(hh)if x==mxh]
mw=[i for i,x in enumerate(ww)if x==mxw]
for mhs in mh:
    for mws in mw:
        if not([mhs,mws] in cord):
            print(mxh+mxw)
            exit()
print(mxh+mxw-1)

Submission status: Time Limit Exceeded

Solution code:
import collections
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)]

nyuuryoku = [[0] * 2 for i in range(M)]
for i in range(M):
    nyuuryoku[i] = (h[i],w[i])
nyuuryoku_set = set(nyuuryoku)

ch = collections.Counter(h)
cw = collections.Counter(w)
ch_zyunn = ch.most_common()
cw_zyunn = cw.most_common()


result = ch_zyunn[0][1] + cw_zyunn[0][1]
#重複チェックをする
ch_max = [i[0] for i in ch.items() if i[1] >= ch_zyunn[0][1]]
cw_max = [i[0] for i in cw.items() if i[1] >= cw_zyunn[0][1]]

flg = True
for i in range(len(ch_max)):
    for j in range(len(cw_max)):
        if ((ch_max[i],cw_max[j]) in nyuuryoku_set):
            continue
        else:
          flg = False  
if flg:
    print(result-1)
else:
    print(result)