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:
from collections import defaultdict

H, W, M = map(int, input().split())
l = [tuple(map(int, input().split())) for _ in range(M)]

dh, dw = defaultdict(int), defaultdict(int)
used = set()

for h, w in l:
    dh[h] += 1
    dw[w] += 1
    used.add((h, w))

ih = sorted(dh.items(), key=lambda x:-x[1])
iw = sorted(dw.items(), key=lambda x:-x[1])

def get(x, y):
    h, counth = ih[x]
    w, countw = iw[y]
    ans = counth + countw
    if (h, w) in used:
        ans -= 1
    return ans

ans = get(0, 0)
for h in range(len(ih)):
    for w in range(len(iw)):
        b = get(h, w)
        if abs(b-ans)>1:
            break
        ans = max(ans, b)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int,input().split())
s=[0]*m
t=[0]*m
temp=[0]*m
for i in range(m):
  sm,tm=map(int,input().split())
  s[i]=sm
  t[i]=tm
  temp[i]=[s[i],t[i]]
st=[0]*h
tt=[0]*w
for i in range(m):
  st[s[i]-1]=st[s[i]-1]+1
  tt[t[i]-1]=tt[t[i]-1]+1
smax=max(st)
tmax=max(tt)
ans=smax+tmax
maxs=[]
maxt=[]
for i in range(h):
  if st[i]==smax:
    maxs.append(i)
for i in range(w):
  if tt[i]==tmax:
    maxt.append(i)

if len(maxs)*len(maxt)>m:
  print(ans)
else:
  for i in range(len(maxs)):
    for j in range(len(maxt)):
      if [maxs[i]+1,maxt[j]+1] not in temp:
        print(ans)
        exit()
  else:
    print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int,input().split())
s=[0]*m
t=[0]*m
temp=[]*m
for i in range(m):
  sm,tm=map(int,input().split())
  s[i]=sm
  t[i]=tm
  temp.append(tuple([s[i],t[i]]))
st=[0]*h
tt=[0]*w
for i in range(m):
  st[s[i]-1]=st[s[i]-1]+1
  tt[t[i]-1]=tt[t[i]-1]+1
smax=max(st)
tmax=max(tt)
ans=smax+tmax
maxs=[]
maxt=[]
for i in range(h):
  if st[i]==smax:
    maxs.append(i)
for i in range(w):
  if tt[i]==tmax:
    maxt.append(i)
c=0
for i in range(len(maxs)):
  for j in range(len(maxt)):
    if (maxs[i]+1,maxt[j]+1) not in temp:
      print(ans)
      exit()
else:
  print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int,input().split())
s=[0]*m
t=[0]*m
temp=[0]*m
for i in range(m):
  sm,tm=map(int,input().split())
  s[i]=sm
  t[i]=tm
  temp[i]=[s[i],t[i]]
st=[0]*h
tt=[0]*w
for i in range(m):
  st[s[i]-1]=st[s[i]-1]+1
  tt[t[i]-1]=tt[t[i]-1]+1
smax=max(st)
tmax=max(tt)
ans=smax+tmax
maxs=[]
maxt=[]
for i in range(h):
  if st[i]==smax:
    maxs.append(i)
for i in range(w):
  if tt[i]==tmax:
    maxt.append(i)
c=0
for i in range(len(maxs)):
  for j in range(len(maxt)):
    if ([maxs[i]+1,maxt[j]+1]) not in temp:
      print(ans)
      exit()
else:
  print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
h,w,m  = list(map(int, input().strip().split()))
a=[0]*m
A=[0]*h
b=[0]*m
B=[0]*w
grid = []
for i in range(m):
    a[i],b[i]= list(map(int, input().strip().split()))
for i in range(m):
    A[a[i]-1]=a.count(a[i])
    B[b[i]-1]=b.count(b[i])
#print(A,B)
p=max(A)
q=max(B)
#print(p,q)
s=[]
S=[]
t=[]
T=[]
for i in range(m):
    if A[a[i]-1]==p:
        s.append(i)
        S.append(a[i])
    if B[b[i]-1]==q:
        t.append(i)
        T.append(b[i])
S=list(set(S))
T=list(set(T))
f=0
for i in range(m):
    if A[a[i]-1]+B[b[i]-1]==p+q:
        f=f+1
if len(S)*len(T)>f:
    ans=p+q
else:
    ans=p+q-1
#print(S,T)
#print(s,t)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w,m  = list(map(int, input().strip().split()))
a=[0]*m
A=[0]*h
b=[0]*m
B=[0]*w
grid = []
for i in range(m):
    array = list(map(int, input().strip().split()))
    grid.append(array)
for i in range(m):
    a[i]=grid[i][0]
    A[a[i]-1]+=1
    b[i]=grid[i][1]
    B[b[i]-1]+=1
masu=[[0 for i in range(w)] for j in range(h)]
for i in range(m):
    masu[a[i]-1][b[i]-1]=-1
for i in range(h):
    for j in range(w):
        masu[i][j]+=A[i]+B[j]
ans=[]
for i in range(h):
    for j in range(w):
        ans.append(masu[i][j])
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
H,W,M = map(int,input().split())
L=[]
for i in range(M):
    h,w = map(int,input().split())
    L.append([h-1,w-1])

h = dict()
w = dict()

for i in range(M):
    val = L[i][0]
    if val in h:
        h[val] += 1
    else:
        h[val] = 1

for i in range(M):
    val = L[i][1]
    if val in w:
        w[val] += 1
    else:
        w[val] = 1

maxh = []
maxw = []
m=-1
for val in h:
    if h[val]>m:
        m = h[val]

for val in h:
    if m==h[val]:
        maxh.append(val)

ans = m
m=-1
for val in w:
    if w[val]>m:
        m=w[val]

for val in w:
    if m==w[val]:
        maxw.append(val)

ans += m
#print(maxh)
#print(maxw)
for mh in maxh:
    for mw in maxw:
        if [mh,mw] not in L:
            print(ans)
            exit()

print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
(h,w,m),*e=[[*map(int,i.split())]for i in open(0)];a,b=[0]*h,[0]*w
for y,x in e:a[y-1]+=1;b[x-1]+=1
print(max(a)+max(b)-(a.count(max(a))*b.count(max(b))-sum(a[y-1]+b[x-1]==max(a)+max(b)for y,x in e)<=0))

Submission status: Time Limit Exceeded

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

cnt_x = [0] * h
cnt_y = [0] * w
bomb_cnt = 0

for i in bomb:
    cnt_x[i[0]-1] += 1
    cnt_y[i[1]-1] += 1

max_x = max(cnt_x)
max_y = max(cnt_y)
ans = max_x + max_y
ans_index_x = [i for i, v in enumerate(cnt_x) if v == max_x]
ans_index_y = [i for i, v in enumerate(cnt_y) if v == max_y]

for i in bomb:
    if i[0]-1 in ans_index_x and i[1]-1 in ans_index_y:
        bomb_cnt += 1

if bomb_cnt >= len(ans_index_x) * len(ans_index_y):
    print(ans - 1)
else:
    print(ans)

Submission status: Time Limit Exceeded

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

cnt_x = [0] * h
cnt_y = [0] * w
bomb_cnt = 0

for i in bomb:
    cnt_x[i[0]-1] += 1
    cnt_y[i[1]-1] += 1

ans = max(cnt_x) + max(cnt_y)
ans_index_x = [i for i, v in enumerate(cnt_x) if v == max(cnt_x)]
ans_index_y = [i for i, v in enumerate(cnt_y) if v == max(cnt_y)]

exist_x = dict(zip(ans_index_x, [False] * len(ans_index_x)))
exist_y = dict(zip(ans_index_y, [False] * len(ans_index_y)))

for i in bomb:
    if i[0]-1 in ans_index_x and i[1]-1 in ans_index_y:
        bomb_cnt += 1

if bomb_cnt >= len(ans_index_x) * len(ans_index_y):
    print(ans - 1)
else:
    print(ans)

Submission status: Time Limit Exceeded

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

H_list = [0]*H
W_list = [0]*W

bom_list = list()

for i in range(M):
    h,w = map(int,input().split())
    bom_list.append([h,w])
    H_list[h-1] += 1
    W_list[w-1] += 1

max_H = max(H_list)
max_W = max(W_list)

# print(max_H+max_W)

H_num = [i for i, x in enumerate(H_list) if x == max_H]
W_num = [i for i, x in enumerate(W_list) if x == max_W]



cnt = 0
for i in bom_list:
    if i[0]-1 in H_num and i[1]-1 in W_num:
        cnt += 1

if len(H_num)*len(W_num) == cnt:
    ans = max_H+max_W-1
else:
    ans = max_H+max_W

print(ans)

Submission status: Time Limit Exceeded

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

H_list = [0]*H
W_list = [0]*W

bom_list = list()

for i in range(M):
    h,w = map(int,input().split())
    bom_list.append([h,w])
    H_list[h-1] += 1
    W_list[w-1] += 1

max_H = max(H_list)
max_W = max(W_list)

max_H_num = [i for i, x in enumerate(H_list) if x == max_H]
max_W_num = [i for i, x in enumerate(W_list) if x == max_W]

cnt = 0
for i in bom_list:
    if i[0]-1 in max_H_num and i[1]-1 in max_W_num:
        cnt += 1

if len(max_H_num)*len(max_W_num) == cnt:
    ans = max_H+max_W-1
else:
    ans = max_H+max_W

print(ans)

Submission status: Time Limit Exceeded

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

H_list = [0]*H
W_list = [0]*W

bom_list = list()

for i in range(M):
    h,w = map(int,input().split())
    bom_list.append([h,w])
    H_list[h-1] += 1
    W_list[w-1] += 1

max_H = max(H_list)
max_W = max(W_list)

max_H_num = [i for i, x in enumerate(H_list) if x == max_H]
max_W_num = [i for i, x in enumerate(W_list) if x == max_W]


for i in max_H_num:
    for j in max_W_num:
        if [i+1,j+1] in bom_list:
            ans = max_H + max_W -1
        else:
            ans = max_H + max_W
            print(ans)
            exit()
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
h, w, m = map(int, input().split())
blist = [list(map(int, input().split())) for _ in range(m)]

rowdict = Counter([blist[i][0] for i in range(m)])
coldict = Counter([blist[i][1] for i in range(m)])

maxr = rowdict.most_common()[0][1]
maxr_keys = []
for i in rowdict.most_common():
    if i[1] == maxr:
        maxr_keys.append(i[0])
    else:
        break
maxc = coldict.most_common()[0][1]
maxc_keys = []
for i in coldict.most_common():
    if i[1] == maxc:
        maxc_keys.append(i[0])
    else:
        break

for i in maxr_keys:
    for j in maxc_keys:
        if [i, j] not in blist:
            print(maxr + maxc)
            break
    else:
        continue
    break
else:
    print(maxr + maxc - 1)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
grid = [[0 for _ in range(w)] for _ in range(h)]
for _ in range(m):
    hi, wi = map(int, input().split())
    hi -= 1
    wi -= 1
    grid[hi][wi] = 1

rowsum = [0] * h
colsum = [0] * w
for i in range(h):
    for j in range(w):
        rowsum[i] += grid[i][j]
        colsum[j] += grid[i][j]

rowmax = max(rowsum)
rowindices = []
for i, v in enumerate(rowsum):
    if v == rowmax:
        rowindices.append(i)

colmax = max(colsum)
colindices = []
for i, v in enumerate(colsum):
    if v == colmax:
        colindices.append(i)

ans = 0
for ri in rowindices:
    for ci in colindices:
        ans = max(ans, rowmax + colmax - grid[ri][ci])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W,M=map(int,input().split())
L=[]
bh=[0]*(H+1)
bw=[0]*(W+1)
mh,mw=[0,[]],[0,[]]
for _ in range(M):
    h,w=map(int,input().split())
    L.append((h,w))
    bh[h]+=1
    bw[w]+=1
    if bh[h]>mh[0]:
        mh=[bh[h],[h]]
        nh=1
    elif bh[h]==mh[0]:
        mh[1].append(h)
        nh+=1
    if bw[w]>mw[0]:
        mw=[bw[w],[w]]
        nw=1
    elif bw[w]==mw[0]:
        mw[1].append(w)
        nw+=1
ans=mh[0]+mw[0]
n=0
if nh*nw<=M:
    for h,w in L:
        if h in mh[1] and w in mw[1]:
            n+=1
if n==nh*nw:
    ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W,M=map(int,input().split())
L=[]
bh=[0]*(H+1)
bw=[0]*(W+1)
mh,mw=[0,[]],[0,[]]
for _ in range(M):
    h,w=map(int,input().split())
    L.append((h,w))
    bh[h]+=1
    bw[w]+=1
    if bh[h]>mh[0]:
        mh=[bh[h],[h]]
    elif bh[h]==mh[0]:
        mh[1].append(h)
    if bw[w]>mw[0]:
        mw=[bw[w],[w]]
    elif bw[w]==mw[0]:
        mw[1].append(w)
ans=mh[0]+mw[0]
n=0
for h,w in L:
    if h in mh[1] and w in mw[1]:
        n+=1
if n==len(mh[1])*len(mw[1]):
    ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def solve(H, M, W, bombs):
    count_h = [0 for _ in range(H)]
    count_w = [0 for _ in range(W)]
    for b in bombs:
        count_h[b[0] - 1] += 1
        count_w[b[1] - 1] += 1
    max_h = max(count_h)
    max_w = max(count_w)
    points_h = [i for i, c in enumerate(count_h) if c == max_h]
    points_w = [i for i, c in enumerate(count_w) if c == max_w]
    ans = max_h + max_w
    if _find_cross_bomb(points_h, points_w, bombs):
        ans -= 1
    print(ans)


def _find_cross_bomb(points_h, points_w, bombs):
    c = 0
    for b in bombs:
        if (b[0] - 1) in points_h:
            if (b[1] - 1) in points_w:
                c += 1
    return c == len(points_h) * len(points_w)


if __name__ == "__main__":
    H, W, M = map(int, input().split())
    bombs = [list(map(int, input().split())) for i in range(M)]
    solve(H, M, W, bombs)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

h = [0] * H
w = [0] * W

g = [[False] * W for _ in range(H)]
S = set()
for _ in range(M):
    Th, Tw = map(int, sys.stdin.readline().strip().split())

    h[Th - 1] += 1
    w[Tw - 1] += 1

    # g[Th - 1][Tw - 1] = True
    S.add((Th - 1, Tw - 1))

max_h = max(h)
max_w = max(w)
ans = max_h + max_w

index_h = [n for n, v in enumerate(h) if v == max_h]
index_w = [n for n, v in enumerate(w) if v == max_w]

sub = 1
# for ih in index_h:
#     for iv in index_w:
#         if g[ih][iv] is False:
#             sub = 0
#             break
#     else:
#         continue
#     break
# ans -= sub
# print(ans)


for ih in index_h:
    for iv in index_w:
        if not (ih, iv) in S:
            print(ans)
            exit()
print(ans - 1)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

h = [0] * H
w = [0] * W

g = [[False] * W for _ in range(H)]
S = set()
for _ in range(M):
    Th, Tw = map(int, sys.stdin.readline().strip().split())

    h[Th - 1] += 1
    w[Tw - 1] += 1

    # g[Th - 1][Tw - 1] = True
    S.add((Th - 1, Tw - 1))

ans = max(h) + max(w)

index_h = [n for n, v in enumerate(h) if v == max(h)]
index_w = [n for n, v in enumerate(w) if v == max(w)]

sub = 1
# for ih in index_h:
#     for iv in index_w:
#         if g[ih][iv] is False:
#             sub = 0
#             break
#     else:
#         continue
#     break
# ans -= sub
# print(ans)


for ih in index_h:
    for iv in index_w:
        if not (ih, iv) in S:
            print(ans)
            exit()
print(ans - 1)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    H, W, M = map(int, input().split())
    bombs = []
    hs = [0]*(H)
    ws = [0]*(W)
    for _ in range(M):
        h, w = map(int, input().split())
        bombs.append([h-1, w-1])
        hs[h-1] += 1
        ws[w-1] += 1
    maxh = max(hs)
    maxw = max(ws)
    ans = maxh + maxw

    maxhindex = [i for i, x in enumerate(hs) if x == maxh]
    maxwindex = [i for i, x in enumerate(ws) if x == maxw]

    isfound = False

    for i in maxhindex:
        for j in maxwindex:
            if [i, j] not in bombs:
                print(ans)
                exit()

    print(ans-1)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def solve(H, M, W, bombs):
    count_h = [0 for _ in range(H)]
    count_w = [0 for _ in range(W)]
    for b in bombs:
        count_h[b[0] - 1] += 1
        count_w[b[1] - 1] += 1
    max_h = max(count_h)
    max_w = max(count_w)
    points_h = [i for i, c in enumerate(count_h) if c == max_h]
    points_w = [i for i, c in enumerate(count_w) if c == max_w]
    ans = max_h + max_w
    if _find_cross_bomb(points_h, points_w, bombs):
        ans -= 1
    print(ans)


def _find_cross_bomb(points_h, points_w, bombs):
    for ph in points_h:
        for pw in points_w:
            if [ph + 1, pw + 1] in bombs:
                pass
            else:
                return False
    return True


if __name__ == "__main__":
    H, W, M = map(int, input().split())
    bombs = [list(map(int, input().split())) for i in range(M)]
    solve(H, M, W, bombs)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

h = [0] * H
w = [0] * W

g = [[False] * W for _ in range(H)]

for _ in range(M):
    Th, Tw = map(int, sys.stdin.readline().strip().split())

    h[Th - 1] += 1
    w[Tw - 1] += 1

    g[Th - 1][Tw - 1] = True

ans = max(h) + max(w)

index_h = [n for n, v in enumerate(h) if v == max(h)]
index_w = [n for n, v in enumerate(w) if v == max(w)]

sub = 1
for ih in index_h:
    for iv in index_w:
        if g[ih][iv] is False:
            sub = 0
            break
    else:
        continue
    break

ans -= sub
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np

input = sys.stdin.readline


def S():
    return input().rstrip()


def I():
    return int(input())


def MI():
    return map(int, input().split())


H, W, M = MI()

R = np.zeros(H + 1)
C = np.zeros(W + 1)
bombs = []
for _ in range(M):
    h, w = MI()
    bombs.append((h, w))
    R[h] += 1
    C[w] += 1

R_max = np.where(R == max(R))
C_max = np.where(C == max(C))

ans = max(R) + max(C)
for r in R_max[0]:
    for c in C_max[0]:
        if (r, c) not in bombs:
            print(int(ans))
            exit()

print(int(ans - 1))

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    H, W, M = map(int, input().split())
    bombs = []
    hs = [0]*(H)
    ws = [0]*(W)
    for _ in range(M):
        h, w = map(int, input().split())
        bombs.append([h-1, w-1])
        hs[h-1] += 1
        ws[w-1] += 1
    maxh = max(hs)
    maxw = max(ws)
    ans = maxh + maxw

    maxwindex = [i for i, x in enumerate(ws) if x == maxw]

    isfound = False

    for i in range(H):
        if hs[i] == maxh:
            for j in maxwindex:
                if [i, j] not in bombs:
                    print(ans)
                    exit()

    print(ans-1)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    H, W, M = map(int, input().split())
    bombs = []
    hs = [0]*(H+1)
    ws = [0]*(W+1)
    for _ in range(M):
        h, w = map(int, input().split())
        bombs.append([h, w])
        hs[h] += 1
        ws[w] += 1
    maxh = max(hs)
    maxw = max(ws)
    ans = maxh + maxw

    maxwindex = [i for i, x in enumerate(ws) if x == maxw]

    isfound = False

    for i in range(H):
        if isfound:
            break
        if hs[i+1] == maxh:
            for j in maxwindex:
                if [i+1, j] not in bombs:
                    isfound = True
                    break
    if isfound == False:
        ans -= 1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    H, W, M = map(int, input().split())
    bombs = []
    hs = [0]*(H+1)
    ws = [0]*(W+1)
    for _ in range(M):
        h, w = map(int, input().split())
        bombs.append([h, w])
        hs[h] += 1
        ws[w] += 1
    maxh = max(hs)
    maxw = max(ws)
    ans = maxh + maxw

    isfound = False

    for i in range(H):
        if isfound == True:
            break
        if hs[i+1] == maxh:
            for j in range(W):
                if ws[j+1] == maxw:
                    if [i+1, j+1] not in bombs:
                        isfound = True
                        break
    if isfound == False:
        ans -= 1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
h,w,m = map(int,input().split())
l = np.array([[0] * w] * h)
for _ in range(m):
    i,j = map(int,input().split())
    l[i-1,:] += 1
    l[:,j-1] += 1
    l[i-1,j-1] -= 1
print(np.amax(l))

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    H, W, M = map(int, input().split())
    bombs = []
    hs = [0]*(H+1)
    ws = [0]*(W+1)
    for _ in range(M):
        h, w = map(int, input().split())
        bombs.append([h, w])
        hs[h] += 1
        ws[w] += 1
    maxh = max(hs)
    maxw = max(ws)
    ans = maxh + maxw

    maxhindex = [i for i, x in enumerate(hs) if x == maxh]
    maxwindex = [i for i, x in enumerate(ws) if x == maxw]

    isfound = False

    for h in maxhindex:
        if isfound == True:
            break
        for w in maxwindex:
            if [h, w] not in bombs:
                isfound = True
                break
    if isfound == False:
        ans -= 1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    H,W,M = map(int,input().split())
    list = []
    dict_H = {key:0 for key in range(1,H+1,1)}
    dict_W = {key:0 for key in range(1,W+1,1)}
    for i in range(M):
        h,w = map(int,input().split())
        dict_H[h] += 1
        dict_W[w] += 1
        list.append((h,w))
    #print(list)
    maxH = max(dict_H.values())
    maxW = max(dict_W.values())
    wi = [i for i in range(1,W+1,1) if dict_W[i] == maxW]
    #print(retuH,gyoW,wi)
    for h in [i for i in range(1,H+1,1) if dict_H[i] == maxH]:
        for w in wi:
            if ((h,w) not in list):
                return maxH + maxW
    return maxH + maxW -1

print(main())

Submission status: Time Limit Exceeded

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())


H, W, M = MI()
h = [0] * H
w = [0] * W
item_list = [[0, 0] for _ in range(M)]

for i in range(M):
    item_h, item_w = MI()
    item_h -= 1
    item_w -= 1
    h[item_h] += 1
    w[item_w] += 1
    item_list[i][0] = item_h
    item_list[i][1] = item_w

h_max = max(h)
w_max = max(w)
ans = h_max + w_max
index_num_h = [n for n, v in enumerate(h) if v == h_max]
index_num_w = [n for n, v in enumerate(w) if v == w_max]

cnt = -1
for h_i in index_num_h:
    for w_i in index_num_w:
        d = [h_i, w_i]
        if d not in item_list:
            cnt = 0
            print(ans)
            exit()

if cnt == -1:
    ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    H,W,M = map(int,input().split())
    list = []
    retuH = [0]*(H+1)
    gyoW = [0]*(W+1)
    for i in range(M):
        h,w = map(int,input().split())
        retuH[h] += 1
        gyoW[w] += 1
        list.append((h,w))
    #print(list)
    maxH = max(retuH)
    maxW = max(gyoW)
    wi = [i for i in range(1,W+1,1) if gyoW[i] == maxW]

    for h in [i for i in range(1,H+1,1) if retuH[i] == maxH]:
        for w in wi:
            if ((h,w) not in list):
                return maxH + maxW
    return maxH + maxW -1

print(main())

Submission status: Time Limit Exceeded

Solution code:
def main():
    H,W,M = map(int,input().split())
    list = []
    retuH = [0]*(H+1)
    gyoW = [0]*(W+1)
    for i in range(M):
        h,w = map(int,input().split())
        retuH[h] += 1
        gyoW[w] += 1
        list.append((h,w))
    #print(list)
    maxH = max(retuH)
    maxW = max(gyoW)

    for h in [i for i in range(1,H+1,1) if retuH[i] == maxH]:
        for w in [i for i in range(1,W+1,1) if gyoW[i] == maxW]:
            if ((h,w) not in list):
                return maxH + maxW
    return maxH + maxW -1

print(main())

Submission status: Time Limit Exceeded

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())


H, W, M = MI()
h = [0] * H
w = [0] * W
item_list = [[0, 0] for _ in range(M)]

for i in range(M):
    item_h, item_w = MI()
    item_h -= 1
    item_w -= 1
    h[item_h] += 1
    w[item_w] += 1
    item_list[i][0] = item_h
    item_list[i][1] = item_w
h_max = max(h)
w_max = max(w)

ans = h_max + w_max
index_num_h = [n for n, v in enumerate(h) if v == h_max]
index_num_w = [n for n, v in enumerate(w) if v == w_max]

cnt = 0
for item in item_list:
    if item[0] in index_num_h and item[1] in index_num_w:
        cnt += 1

x = len(index_num_h) * len(index_num_w)
if cnt == x:
    ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
H,W,M = map(int,input().split())
#h = np.empty(M,dtype=np.int)
#w = np.empty(M,dtype=np.int)
bomb = set()
hh = np.zeros(H+1,dtype=np.int)
ww = np.zeros(W+1,dtype=np.int)

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

#print(bomb)
h_max = np.max(hh)
w_max = np.max(ww)

h_max_ids = list()
w_max_ids = list()
for i in range(1,H+1):
    if hh[i] == h_max:
      h_max_ids.append(i)
for j in range(1,W+1):
    if ww[j] == w_max:
      w_max_ids.append(j)
b=1
for i in h_max_ids:
  for j in w_max_ids:
    if not (i,j) in bomb:
      b = 0

#print("hmax:{} wmax:{}".format(h_max_id, w_max_id))
#print("hmax:{} wmax:{}".format(hh[h_max_id], ww[w_max_id]))
print(h_max + w_max - b)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
H,W,M = map(int,input().split())
#h = np.empty(M,dtype=np.int)
#w = np.empty(M,dtype=np.int)
bomb = set()
hh = np.zeros(H+1,dtype=np.int32)
ww = np.zeros(W+1,dtype=np.int32)

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

#print(bomb)
h_max = np.max(hh)
w_max = np.max(ww)

b = 1
for i in range(1,H+1):
  for j in range(1,W+1):
    if hh[i] == h_max and ww[j] == w_max:
      if not (i,j) in bomb:
        b = 0
      #else:
        #print("[{}, {}] exists".format(i,j))
#print("hmax:{} wmax:{}".format(h_max_id, w_max_id))
#print("hmax:{} wmax:{}".format(hh[h_max_id], ww[w_max_id]))
print(h_max + w_max - b)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
H,W,M = map(int,input().split())
#h = np.empty(M,dtype=np.int)
#w = np.empty(M,dtype=np.int)
bomb = [0,0]*M
hh = np.zeros(H+1,dtype=np.int32)
ww = np.zeros(W+1,dtype=np.int32)

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

#print(bomb)
h_max = np.max(hh)
w_max = np.max(ww)

b = 1
for i in range(1,H+1):
  for j in range(1,W+1):
    if hh[i] == h_max and ww[j] == w_max:
      if not [i,j] in bomb[:]:
        b = 0
      #else:
        #print("[{}, {}] exists".format(i,j))
#print("hmax:{} wmax:{}".format(h_max_id, w_max_id))
#print("hmax:{} wmax:{}".format(hh[h_max_id], ww[w_max_id]))
print(h_max + w_max - b)

Submission status: Time Limit Exceeded

Solution code:
H, W, M = [int(x) for x in input().split()]

width = [0] * W
height = [0] * H

obj = set()
for i in range(M):
    h, w = [int(x) - 1 for x in input().split()]

    width[w] += 1
    height[h] += 1
    obj.add((h, w))

for idx_h, h in [(idx_h, h) for idx_h, h in enumerate(height) if h == max(height)]:
    for idx_w, w in [(idx_w, w) for idx_w, w in enumerate(width) if w == max(width)]:
        if not (idx_h, idx_w) in obj:
            print(h + w)
            exit()

print(max(height) + max(width) - 1)

Submission status: Time Limit Exceeded

Solution code:
H, W, M = map(int, input().split())
targets = [list(map(int, input().split())) for _ in range(M)]
x_counter = [0 for _ in range(W + 1)]
y_counter = [0 for _ in range(H + 1)]
y_index = []
x_index = []
check = 0

for i in range(M):
    x, y = targets[i]
    y_counter[x] += 1
    x_counter[y] += 1

y_max, x_max = max(y_counter), max(x_counter)

for i in range(H + 1):
    if y_counter[i] == y_max:
        y_index.append(i)

for j in range(W + 1):
    if x_counter[j] == x_max:
        x_index.append(j)
check = len(y_index) * len(x_index)
for k in range(M):
    r, c = targets[k]
    if r in y_index and c in x_index:
        check -= 1

print(y_max + x_max if check > 0 else y_max + x_max -1)

Submission status: Time Limit Exceeded

Solution code:
H, W, M = map(int, input().split())
targets = [list(map(int, input().split())) for _ in range(M)]
x_counter = [0 for _ in range(W + 1)]
y_counter = [0 for _ in range(H + 1)]
y_index = []
x_index = []
check = 0

for target in targets:
    y_counter[target[0]] += 1
    x_counter[target[1]] += 1

y_max, x_max = max(y_counter), max(x_counter)

for i in range(H + 1):
    if y_counter[i] == y_max:
        y_index.append(i)

for j in range(W + 1):
    if x_counter[j] == x_max:
        x_index.append(j)
check = len(y_index) * len(x_index)
for k in range(M):
    r, c = targets[k]
    if r in y_index and c in x_index:
        check -= 1

print(y_max + x_max if check > 0 else y_max + x_max -1)

Submission status: Time Limit Exceeded

Solution code:
H, W, M = map(int, input().split())
targets = [list(map(int, input().split())) for _ in range(M)]
x_counter = [0 for _ in range(W + 1)]
y_counter = [0 for _ in range(H + 1)]
y_index = []
x_index = []
dup = True

for target in targets:
    y_counter[target[0]] += 1
    x_counter[target[1]] += 1

y_max, x_max = max(y_counter), max(x_counter)

for i in range(H + 1):
    if y_counter[i] == y_max:
        y_index.append(i)

for j in range(W + 1):
    if x_counter[j] == x_max:
        x_index.append(j)

for k in y_index:
    for l in x_index:
        if [k, l] not in targets:
            print(y_max + x_max)
            exit()

print(y_max + x_max - 1)

Submission status: Time Limit Exceeded

Solution code:
H, W, M = map(int, input().split())
targets = [list(map(int, input().split())) for _ in range(M)]
x_counter = [0 for _ in range(W + 1)]
y_counter = [0 for _ in range(H + 1)]
y_index = []
x_index = []
dup = True

for target in targets:
    y_counter[target[0]] += 1
    x_counter[target[1]] += 1

y_max, x_max = max(y_counter), max(x_counter)

for i in range(H + 1):
    if y_counter[i] == y_max:
        y_index.append(i)

for j in range(W + 1):
    if x_counter[j] == x_max:
        x_index.append(j)

for k in y_index:
    for l in x_index:
        if [k, l] not in targets:
            dup = False

if dup:
    print(y_max + x_max - 1)
else:
    print(y_max + x_max)