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: Wrong Answer

Solution code:
h, w, m = map(int, input().split(' '))
row = dict()
col = dict()
for i in range(m):
    r, c = map(int, input().split(' '))
    if r in row.keys():
        row[r] += 1
    else:
        row[r] = 1
    if c in col.keys():
        col[c] += 1
    else:
        col[c] = 1

res = 0
for i in range(1, h+1):
    for j in range(1, w+1):
        t = 0
        #print([i, j])
        if i in row.keys():
            t += row[i]
        if j in col.keys():
            t += col[j]
        if i in row.keys() and j in row.keys():
            t -= 1
        #print(t)
        res = max(res, t)
print(res)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, Counter


def main():
    H, W, M = map(int, input().split())
    LH = []
    LM = []
    s = set()
    for _ in range(M):
        h, w = map(int, input().split())
        LH.append(h)
        LM.append(w)
        s.add((h,w))
    
    ch = Counter(LH)
    cm = Counter(LM)

    ah, ahc = ch.most_common(1)[0]
    aw, awc = cm.most_common(1)[0]
    if (ah, aw) in s:
        print(ahc+awc-1)
    else:
        print(ahc + awc)
    


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

L = [0]*H
R = [0]*W

li = []

for i in range(M):
    h, w = map(int, input().split())
    L[h-1] += 1
    R[w-1] += 1
    li.append([h-1, w-1])
ind_L = L.index(max(L))
ind_R = R.index(max(R))

if [ind_L, ind_R] in li:
    print(max(L)+max(R)-1)
else:
    print(max(L)+max(R))
print(ind_R, ind_L)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    h, w, m = map(int, readline().split())
    row = [0] * h
    col = [0] * w
    s = set()

    for _ in range(m):
        h, w = map(int, readline().split())
        h, w = h - 1, w - 1
        row[h] += 1
        col[w] += 1
        s.add((h, w))

    ans = 0
    r_cnt = max(row)
    mr = row.index(max(row))
    for c in range(w):
        num = r_cnt + col[c]
        if (mr, c) in s:
            num -= 1
        ans = max(ans, num)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

H,W,M=map(int,input().split())
height=[0]*H
width=[0]*W
bomb=[0]*M
for k in range(M):
    bomb[k]=[0]*2
for j in range(M):
    h,w=map(int,input().split())
    bomb[j][0]=h-1
    bomb[j][1]=w-1
    height[h-1]+=1
    width[w-1]+=1
ans=0
#print(bomb)
#print(height)
#print(width)
for i in range(M):
    
    ans=max(ans,height[bomb[i][0]]+width[bomb[i][1]]-1)
    #print(i)

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations

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

bomb_h = [0] * H
bomb_w = [0] * W
grid = [[False] * W for _ in range(H)]
for i in range(M):
    h, w = map(int, input().split())
    bomb_h[h-1] += 1
    bomb_w[w-1] += 1
    grid[h-1][w-1] = True

max_h = max(bomb_h)
max_w = max(bomb_w)

h_indices = []
for i in range(H):
    if bomb_h[i] == max_h:
        h_indices.append(i)

w_indices = []
for i in range(W):
    if bomb_w[i] == max_w:
        w_indices.append(i)

ans = max_w + max_h - 1
for h in h_indices:
    for w in w_indices:
        if not grid[h][w]:
            ans += 1
            break

print(ans)

Submission status: Wrong Answer

Solution code:
h,w,m = map(int,input().split())
gyou=[]
retsu=[]
goukei=[]
for i in range(m):
    a,b = map(int,input().split())
    gyou.append(a)
    retsu.append(b)
    goukei.append([a,b])
import collections
gyou = collections.Counter(gyou)
gzahyou=gyou.most_common()[0][0]
gkosuu=gyou.most_common()[0][1]
retsu = collections.Counter(retsu)
rzahyou=retsu.most_common()[0][0]
rkosuu=retsu.most_common()[0][1]
ans=gkosuu+rkosuu
if [gzahyou,rzahyou] in goukei:
    ans-=1
print(ans)

Submission status: Wrong Answer

Solution code:
h,w,m=map(int,input().split())
a=[0]*h
b=[0]*w
for i in range (m):
    x,y=map(int,input().split())
    a[x-1]+=1
    b[y-1]+=1
r1=max(a)
r2=max(b)
r=min(m,r1+r2)
print(r)

Submission status: Wrong Answer

Solution code:
import numpy as np
H,W,M=map(int,input().split())

h=[0]*(6*10**5)
w=[0]*(6*10**5)
l=[]
for i in range(M):
  a,b=map(int,input().split())
  h[a]+=1
  w[b]+=1
  l.append([a,b])
h=np.array(h)
w=np.array(w)
hm=np.argmax(h)
wm=np.argmax(w)
dup=0
for a,b in l:
  if a==hm and b==wm:
    dup+=1
print(max(h)+max(w)-dup)

Submission status: Wrong Answer

Solution code:
h,w,m = map(int,input().split())
hw = []
hl = []
wl = []
for i in range(m):
  h,w = map(int,input().split())
  hl.append(h)
  wl.append(w)
hs = list(set(hl))
ws = list(set(wl))
ans = -100
for i in range(len(hs)):
  for j in range(len(ws)):
    ans = max(ans,hl.count(hs[i])+wl.count(ws[j]))
      
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

h,w,m=nii()

h_b=[[0,i] for i in range(h)]
w_b=[[0,i] for i in range(w)]

l=[]
for i in range(m):
  h,w=nii()
  h-=1
  w-=1
  h_b[h][0]+=1
  w_b[w][0]+=1
  l.append([h,w])

h_s=sorted(h_b,key=lambda x:x[0],reverse=True)
w_s=sorted(w_b,key=lambda x:x[0],reverse=True)

ans=0
for i in range(min(1000,len(h_s))):
  for j in range(min(1000,len(w_s))):
    t_ans=h_b[i][0]+w_b[j][0]
    num=l.count([i,j])
    t_ans-=num
    ans=max(ans,t_ans)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

h,w,m= map(int, input().split())
a = [list(map(int, input().split())) for i in range(m)]
b=[a[i][0] for i in range(m)]
c=[a[i][1] for i in range(m)]
v=Counter(b).most_common()[0][1]+Counter(c).most_common()[0][1]
if(Counter(b).most_common()[0][0]==Counter(c).most_common()[0][0]):
  v-=1
print(v)

Submission status: Wrong Answer

Solution code:
h,w,m=map(int,input().split())
a=[0]*h
b=[0]*w
for i in range (m):
    x,y=map(int,input().split())
    a[x-1]+=1
    b[y-1]+=1
r1=max(a)
r2=max(b)
r=min(m,r1+r2)

Submission status: Wrong Answer

Solution code:
h,w,m=map(int,input().split())
objs=[tuple(map(int,input().split())) for i in range(m)]
setobj=set(objs)
xs,ys=zip(*objs)

from collections import Counter as co
ws=co(ys).most_common(2)
if len(ws)==1:ws.append((0,0))
#key value
hs=co(xs)
ans=1
for i in range(1,h+1):
    preans=0 if i not in hs else hs[i]
    ans=max(ans,preans+max(ws[0][1]-int((i,ws[0][0]) in setobj), ws[1][1]-int((i,ws[1][0]) in setobj)))
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))
import numpy as np

h,w,m=nii()

h_b=[0 for i in range(h)]
w_b=[0 for i in range(w)]

l=[]
for i in range(m):
  h,w=nii()
  h-=1
  w-=1
  h_b[h]+=1
  w_b[w]+=1
  l.append([h,w])

h_s=np.argsort(h_b)
w_s=np.argsort(w_b)

ans=0
for i in range(min(1000,len(h_s))):
  for j in range(min(1000,len(w_s))):
    t_ans=h_b[i]+w_b[j]
    num=l.count([i,j])
    t_ans-=num
    ans=max(ans,t_ans)

print(ans)

Submission status: Wrong Answer

Solution code:
h,w,m=map(int,input().split())
p=[]
for i in range(m):
  p.append(tuple(map(lambda x:int(x)-1,input().split())))

hc=[0 for i in range(h)]
wc=[0 for i in range(w)]
hm=0
wm=0
hp=[]
wp=[]
for i in p:
  hc[i[0]]+=1
  if hc[i[0]]>hm:
    hm=hc[i[0]]
    hp=[i[0]]
  elif hc[i[0]]==hm:
    hp.append(i[0])
   
  wc[i[1]]+=1
  if wc[i[1]]>wm:
    wm=wc[i[1]]
    wp=[i[1]]
  elif wc[i[1]]==wm:
    hp.append(i[1])

f=0
for i in hp:
  for j in wp:
    if not ((i,j) in p):
      f=1
      break
  if f:
    break

print(hm+wm-1+f)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter


def main():
    input = sys.stdin.buffer.readline
    h, w, m = map(int, input().split())
    h = [0] * m
    w = [0] * m
    object_set = set()
    for i in range(m):
        h[i], w[i] = map(int, input().split())
        object_set.add((h[i], w[i]))
    h_counter = Counter(h)
    w_counter = Counter(w)
    max_h = h_counter.most_common()[0]
    max_w = w_counter.most_common()[0]
    ans = max_h[1] + max_w[1]
    if (max_h[0], max_w[0]) in object_set:
        ans -= 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
H, W, M=map(int, input().split())
x=[0]*(H+1)
y=[0]*(W+1)
hw=[list(map(int, input().split())) for _ in range(M)]
for i in range(M):
  x[hw[i][0]]+=1
  y[hw[i][1]]+=1
  
a=np.argmax(x)
b=np.argmax(y)
z=0
for i in range(M):
  if hw[i][0]==a and hw[i][1]==b:
    z+=1
    
print(max(x)+max(y)-z)

Submission status: Wrong Answer

Solution code:
I = lambda : list(map(int,input().split()))
h,w,m=I()
x={};y={}
l=[]
for i in range(m):
  a,b=I()
  l.append([a,b])
  x[a]=x.get(a,0)+1
  y[b]=y.get(b,0)+1
a,b=max(x.keys(),key = lambda i:x[i]),max(y.keys(),key = lambda i:y[i])
an=x[a]+y[b]
if [a,b] in l:
  an-=1
print(an)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    h,w,m = LI()
    hw = LIR(m)
    # h, w, m = [3 * 10 ** 5] * 3
    # hw = [(i, i) for i in range(3 * 10 ** 5)]
    dh = defaultdict(int)
    dw = defaultdict(int)
    ma = defaultdict(int)
    for hi, wi in hw:
        dh[hi] += 1
        dw[wi] += 1
        ma[(hi, wi)] = 1
    maxh = sorted(dh.items(), key=lambda x: -x[1])
    maxw = sorted(dw.items(), key=lambda x: -x[1])
    ans = 0
    for h, ch in maxh[:5]:
        for w, cw in maxw[:5]:
            tmp = ch + cw - ma[(h, w)]
            ans = max(ans, tmp)
    print(ans)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

h,w,m=mp()
l=[]
x=[]
y=[]
for i in range(m):
    a,b=mp()
    l.append([a,b])
    x.append(a)
    y.append(b)
x=Counter(x)
y=Counter(y)
p=x.most_common()[0]
q=y.most_common()[0]
r=0
if [p[0],q[0]] in l:
    r=1
print(p[1]+q[1]-r)

Submission status: Time Limit Exceeded

Solution code:
import sys

H, W, M = map(int, sys.stdin.readline().rstrip().split())#行数H、列数W、爆弾数M
hcnt = [0] * H#何行目に何個あるか
wcnt = [0] * W#何列目に何個あるか
bomb = []

for i in range(M):
    bh,bw=map(int,sys.stdin.readline().rstrip().split())
    hcnt[bh - 1] += 1#行方向
    wcnt[bw - 1] += 1#列方向
    bomb.append((bh - 1,bw - 1))
#hcnt [2,1]
#wcnt[1,1,1]
#bomb[[1,1],[0,0],[0,2]]
hmax = max(hcnt)#2
wmax = max(wcnt)#1
hmaxindex = [i for i in range(H) if hcnt[i] == hmax]#[0]
wmaxindex = [i for i in range(W) if wcnt[i] == wmax]#[0,1,2]
#crosspoint has not bomb

for i in hmaxindex:
    for j in wmaxindex:
        if (i,j) not in bomb:
            print(hmax + wmax)
            sys.exit()
        else:
            continue
#all crosspoint has bomb
print(hmax + wmax - 1)

Submission status: Time Limit Exceeded

Solution code:
import sys
h,w,m = map(int,input().split())
h_lst = [0]*h
w_lst = [0]*w
memo = []

for i in range(m):
  x,y = map(int,input().split())
  h_lst[x-1] += 1
  w_lst[y-1] += 1
  memo.append((x-1,y-1))

sh = 0
for i in range(h):
  if h_lst[i] > sh:
    sh = h_lst[i]
sw = 0
for i in range(w):
  if w_lst[i] > sw:
    sw = w_lst[i]



memo = set(memo)
              

for i in range(h):
  if h_lst[i] == sh:
    for j in range(w):
      if (i,j) not in memo and w_lst[j] == sw:
        print(sh+sw)
        sys.exit()
print((sh+sw)-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
h,w,m = map(int,input().split())
h_lst = [[0,i] for i in range(h)]
w_lst = [[0,i] for i in range(w)]
memo = []

for i in range(m):
  x,y = map(int,input().split())
  h_lst[x-1][0] += 1
  w_lst[y-1][0] += 1
  memo.append((x-1,y-1))

h_lst.sort(reverse = True)
w_lst.sort(reverse = True)

Max_h = h_lst[0][0]
memo = set(memo)
              

for [y,j] in h_lst:
  if y == Max_h:
    for [z,k] in w_lst:
      if (j,k) not in memo and w_lst[0][0] == z:
        print(Max_h+w_lst[0][0])
        sys.exit()
print((Max_h+w_lst[0][0])-1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from itertools import product
H, W, M = list(map(int, input().split()))
h = [0]*M
w = [0]*M
for i in range(M):
    h[i], w[i] = list(map(int, input().split()))
hc = Counter(h)
wc = Counter(w)
hmax = max(hc.values())
wmax = max(wc.values())
lh = [k[0] for k in hc.items() if k[1] == hmax]
lw = [k[0] for k in wc.items() if k[1] == wmax]
# print(lh,lw)
S = set(product(lh, lw))
# print(S)

for i in range(M):
    if (h[i], w[i]) in S:
        S -= {(h[i], w[i])}
if S:
    print(hmax+wmax)
else:
    print(hmax+wmax-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
H,W,M=map(int,input().split())#slow,行数H、列数W、爆弾数M
hcnt=[0]*H#何行目に何個あるか
wcnt=[0]*W#何列目に何個あるか
bomb=[]

for i in range(M):
    bh,bw=map(int,input().split())
    hcnt[bh-1]+=1#行方向
    wcnt[bw-1]+=1#列方向
    bomb.append([bh-1,bw-1])
#hcnt [2,1]
#wcnt[1,1,1]
#bomb[[1,1],[0,0],[0,2]]
hmax=max(set(hcnt))#2
wmax=max(set(wcnt))#1
hmaxindex=[i for i in range(H) if hcnt[i]==hmax]#[0]
wmaxindex=[i for i in range(W) if wcnt[i]==wmax]#[0,1,2]
#crosspoint has not bomb

for i in hmaxindex:
    for j in wmaxindex:
        if [i,j] not in bomb:
            print(hmax+wmax)
            sys.exit()
        else:
            continue
#all crosspoint has bomb
print(hmax+wmax-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
H,W,M=map(int,input().split())#slow,行数H、列数W、爆弾数M
hcnt=[0]*H#何行目に何個あるか
wcnt=[0]*W#何列目に何個あるか
bomb=[]

for i in range(M):
    bh,bw=map(int,input().split())
    hcnt[bh-1]+=1#行方向
    wcnt[bw-1]+=1#列方向
    bomb.append([bh-1,bw-1])
#hcnt [2,1]
#wcnt[1,1,1]
#bomb[[1,1],[0,0],[0,2]]
hmax=max(hcnt)#2
wmax=max(wcnt)#1
hmaxindex=[i for i in range(H) if hcnt[i]==hmax]#[0]
wmaxindex=[i for i in range(W) if wcnt[i]==wmax]#[0,1,2]
#crosspoint has not bomb

for i in hmaxindex:
    for j in wmaxindex:
        if [i,j] not in bomb:
            print(hmax+wmax)
            sys.exit()
        else:
            continue
#all crosspoint has bomb
print(hmax+wmax-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
H,W,M=map(int, input().split())#slow,行数H、列数W、爆弾数M
hcnt=[0]*H#何行目に何個あるか
wcnt=[0]*W#何列目に何個あるか
bomb=[]

for i in range(M):
    bh,bw=map(int,input().split())
    hcnt[bh-1]+=1#行方向
    wcnt[bw-1]+=1#列方向
    bomb.append([bh-1,bw-1])
#hcnt [2,1]
#wcnt[1,1,1]
#bomb[[2,2],[1,1],[1,3]]
hmax = max(set(hcnt))#2
wmax = max(set(wcnt))#1
hmaxindex = [i for i in range(H) if hcnt[i]==hmax]#[0]
wmaxindex = [i for i in range(W) if wcnt[i]==wmax]#[0,1,2]
#crosspoint has not bomb

for i in hmaxindex:
    for j in wmaxindex:
        if bomb.count([i,j])==0:
            print(hmax + wmax)
            sys.exit()
        else:
            continue
#all crosspoint has bomb
print(hmax+wmax-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
H, W, M = map(int, input().split())#slow,行数H、列数W、爆弾数M
hcnt = [0] * H#何行目に何個あるか
wcnt = [0] * W#何列目に何個あるか
bomb = []

for i in range(M):
    bh, bw = map(int, input().split())
    hcnt[bh - 1] += 1#行方向
    wcnt[bw - 1] += 1#列方向
    bomb.append([bh - 1,bw - 1])
#hcnt [2,1]
#wcnt[1,1,1]
#bomb[[2,2],[1,1],[1,3]]
hmax = max(set(hcnt))#2
wmax = max(set(wcnt))#1
hmaxindex = [ i for i in range(H) if hcnt[i] == hmax]#[0]
wmaxindex = [ i for i in range(W) if wcnt[i] == wmax]#[0,1,2]
#crosspoint has not bomb

for i in hmaxindex:
    for j in wmaxindex:
        if bomb.count([i, j]) == 0:
            print(hmax + wmax)
            sys.exit()
        else:
            continue
#all crosspoint has bomb
print(hmax + wmax - 1)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
H, W, M = map(int, input().split())#slow,行数H、列数W、爆弾数M
hcnt = [0] * H#何行目に何個あるか
wcnt = [0] * W#何列目に何個あるか
bomb = []

for i in range(M):
    bh, bw = map(int, input().split())
    hcnt[bh - 1] += 1#行方向
    wcnt[bw - 1] += 1#列方向
    if bomb.count([bh - 1,bw - 1]) == 0:
        bomb.append([bh - 1,bw - 1])
#hcnt [2,1]
#wcnt[1,1,1]
#bomb[[2,2],[1,1],[1,3]]
hmax = max(set(hcnt))#2
wmax = max(set(wcnt))#1
hmaxindex = [ i for i in range(H) if hcnt[i] == hmax]#[0]
wmaxindex = [ i for i in range(W) if wcnt[i] == wmax]#[0,1,2]
#crosspoint has not bomb
for i in hmaxindex:
    for j in wmaxindex:
        if bomb.count([i, j]) == 0:
            print(hmax + wmax)
            sys.exit()
        else:
            continue
#all crosspoint has bomb
print(hmax + wmax - 1)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
a_list = []
b_list = []
h_list = [0 for _ in range(h)]
w_list = [0 for _ in range(w)]
for i in range(m):
    a, b = map(int, input().split())
    a_list.append([a,b])
    h_list[a - 1] += 1
    w_list[b - 1] += 1
w_flag = [0 for _ in range(w)]
for i in range(w):
    if w_list[i] == max(w_list):
        w_flag[i] = 1
h_flag = [0 for _ in range(h)]
for i in range(h):
    if h_list[i] == max(h_list):
        h_flag[i] = 1
flag = 0   
for i in range(m):
    if h_flag[a_list[i][0] - 1] == 1 and w_flag[a_list[i][1] - 1] == 1:
        flag += 1
s = sum(h_flag) * sum(w_flag)
print(max(h_list) + max(w_list) - 1 if flag == s else max(h_list) + max(w_list))

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
a_list = []
b_list = []
h_list = [0 for _ in range(h)]
w_list = [0 for _ in range(w)]
for i in range(m):
    a, b = map(int, input().split())
    a_list.append([a,b])
    h_list[a - 1] += 1
    w_list[b - 1] += 1
w_flag = []
for i in range(w):
    if w_list[i] == max(w_list):
        w_flag.append(i)
h_flag = []
for i in range(h):
    if h_list[i] == max(h_list):
        h_flag.append(i)
flag = 0   
for i in range(m):
    if a_list[i][0] - 1 in h_flag and a_list[i][1] - 1 in w_flag:
        flag += 1
s = len(h_flag) * len(w_flag)
print(max(h_list) + max(w_list) - 1 if flag == s else max(h_list) + max(w_list))

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
a_list = []
b_list = []
h_list = [0 for _ in range(h)]
w_list = [0 for _ in range(w)]
for i in range(m):
    a, b = map(int, input().split())
    a_list.append([a,b])
    h_list[a - 1] += 1
    w_list[b - 1] += 1
# print(a_list)
# if (max(h_list), max(w_list)) in a_list:
#     print(a)
#     print(max(h_list) + max(w_list) - 1)
# else:
#     print(max(h_list) + max(w_list))
w_flag = []
for i in range(w):
    if w_list[i] == max(w_list):
        w_flag.append(i)
h_flag = []
for i in range(h):
    if h_list[i] == max(h_list):
        h_flag.append(i)
ans = []
for i in w_flag:
    for j in h_flag:
        if [j + 1, i + 1] not in a_list:
            # ans.append(max(h_list) + max(w_list) - 1)
            print(max(h_list) + max(w_list))
            exit()
print(max(h_list) + max(w_list) - 1)

Submission status: Time Limit Exceeded

Solution code:
import sys
h,w,m = map(int,input().split())
H = [0]*(h)
W = [0]*(w)
ls = []
for _ in range(m):
    a,b = map(int,input().split())
    ls.append([a-1,b-1])
    H[a-1] += 1
    W[b-1] += 1
hm = max(H)
wm = max(W)
a = [i for i in range(h) if H[i] == hm]
b = [j for j in range(w) if W[j] == wm]
for i in a:
    for j in b:
        if [i,j] not in ls:
            print(hm+wm)
            sys.exit()
print(hm+wm-1)

Submission status: Time Limit Exceeded

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

hs = [0] * H
ws = [0] * W
s = list()
for i in range(M):
  h,w = map(int, input().split())
  hs[h-1] += 1
  ws[w-1] += 1
  s.append([h-1,w-1])
  
mh = 0
mw = 0
for i in range(H):
  mh = max(mh, hs[i])
for j in range(W):
  mw = max(mw, ws[j])
  
i_s = list()
j_s = list()
for i in range(H):
  if mh == hs[i]:
    i_s.append(i)
for j in range(W):
  if mw == ws[j]:
    j_s.append(j)

ans = mh + mw
for i in i_s:
  for j in j_s:
    if [i,j] in s:
      continue
    print(ans)
    exit()
ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
h,w,m = map(int,input().rstrip().split())
hw = [list(map(int,input().rstrip().split()))for _ in range(m)]

tate = [0]*(h+1)
yoko = [0]*(w+1)
bombpoint = []

for x,y in hw:
    tate[x] += 1
    yoko[y] += 1
    bombpoint.append((x,y))

ans_x = []
ans_y = []
x_max = max(tate)
y_max = max(yoko)
for i in range(len(tate)):
    if tate[i] == x_max:
        ans_x.append(i)
for i in range(len(yoko)):
    if yoko[i] == y_max:
        ans_y.append(i)

ans = x_max + y_max
for x in ans_x:
    for y in ans_y:
        if (x,y) not in bombpoint:
            print(ans)
            exit()
print(ans - 1)

Submission status: Time Limit Exceeded

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

tate = [0]*(h+1)
yoko = [0]*(w+1)
bombpoint = []

for x,y in hw:
    tate[x] += 1
    yoko[y] += 1
    bombpoint.append((x,y))

ans_x = set()
ans_y = set()
x_max = max(tate)
y_max = max(yoko)
for i in range(len(tate)):
    if tate[i] == x_max:
        ans_x.add(i)
for i in range(len(yoko)):
    if yoko[i] == y_max:
        ans_y.add(i)

ans = x_max + y_max
for x in ans_x:
    for y in ans_y:
        if (x,y) not in bombpoint:
            print(ans)
            exit()
print(ans - 1)

Submission status: Time Limit Exceeded

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

tate = [0]*(h+1)
yoko = [0]*(w+1)

for x,y in hw:
    tate[x] += 1
    yoko[y] += 1

ans_x = set()
ans_y = set()
x_max = max(tate)
y_max = max(yoko)
for i in range(len(tate)):
    if tate[i] == x_max:
        ans_x.add(i)
for i in range(len(yoko)):
    if yoko[i] == y_max:
        ans_y.add(i)

ans = x_max + y_max
for x in ans_x:
    for y in ans_y:
        if [x,y] not in hw:
            print(ans)
            exit()
print(ans - 1)

Submission status: Time Limit Exceeded

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

tate = [0]*(h+1)
yoko = [0]*(w+1)

for x,y in hw:
    tate[x] += 1
    yoko[y] += 1

ans_x = []
ans_y = []
x_max = max(tate)
y_max = max(yoko)
for i in range(len(tate)):
    if tate[i] == x_max:
        ans_x.append(i)
for i in range(len(yoko)):
    if yoko[i] == y_max:
        ans_y.append(i)

ans = x_max + y_max
for x in ans_x:
    for y in ans_y:
        if [x,y] not in hw:
            print(ans)
            exit()
print(ans - 1)

Submission status: Time Limit Exceeded

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

tate = [0]*(h+1)
yoko = [0]*(w+1)

for x,y in hw:
    tate[x] += 1
    yoko[y] += 1

ans_x = []
ans_y = []
x_max = max(tate)
y_max = max(yoko)
for i in range(len(tate)):
    if tate[i] == x_max:
        ans_x.append(i)
for i in range(len(yoko)):
    if yoko[i] == y_max:
        ans_y.append(i)

ans = x_max + y_max
kaburi = True
for x in ans_x:
    if kaburi == False:
        break
    for y in ans_y:
        if [x,y] not in hw:
            kaburi = False
            break
print(ans - int(kaburi))

Submission status: Time Limit Exceeded

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

tate = [0]*(h+1)
yoko = [0]*(w+1)

for x,y in hw:
    tate[x] += 1
    yoko[y] += 1

ans_x = []
ans_y = []
x_max = max(tate)
y_max = max(yoko)
for i in range(len(tate)):
    if tate[i] == x_max:
        ans_x.append(i)
for i in range(len(yoko)):
    if yoko[i] == y_max:
        ans_y.append(i)

ans = x_max + y_max
kaburi = True
for x in ans_x:
    if kaburi == False:
        break
    for y in ans_y:
        if [x,y] in hw:
            continue
        else:
            kaburi = False
            break
print(ans - int(kaburi))

Submission status: Time Limit Exceeded

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

tate = [0]*(h+1)
yoko = [0]*(w+1)

for x,y in hw:
    tate[x] += 1
    yoko[y] += 1

ans_x = []
ans_y = []
x_max = max(tate)
y_max = max(yoko)
for i in range(len(tate)):
    if tate[i] == x_max:
        ans_x.append(i)
for i in range(len(yoko)):
    if yoko[i] == y_max:
        ans_y.append(i)

ans = x_max + y_max
kaburi = True
for x in ans_x:
    for y in ans_y:
        if [x,y] in hw:
            continue
        else:
            kaburi = False
print(ans - int(kaburi))

Submission status: Time Limit Exceeded

Solution code:
import sys
h,w,m = map(int,input().split())
h_lst = [[0,i] for i in range(h)]
w_lst = [[0,i] for i in range(w)]
bomb = [['t']*w for i in range(h)]

for i in range(m):
  x,y = map(int,input().split())
  h_lst[x-1][0] += 1
  w_lst[y-1][0] += 1
  bomb[x-1][y-1] = 'b'

h_lst.sort(reverse = True)
w_lst.sort(reverse = True)

Max_h = h_lst[0][0]

for [y,j] in h_lst:
  if y == Max_h:
    for [z,k] in w_lst:
      if bomb[j][k] != 'b' and w_lst[0][0] == z:
        print(Max_h+w_lst[0][0])
        sys.exit()
print((Max_h+w_lst[0][0])-1)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
H=[0]*h
W=[0]*w
for i in range(m):
  H[l[i][0]-1]+=1
  W[l[i][1]-1]+=1
hmax=max(H)
wmax=max(W)
cthmax=0
ctwmax=0
for i in range(h):
  if H[i]==hmax:
    cthmax+=1
for i in range(w):
  if W[i]==wmax:
    ctwmax+=1
ct=0
for i in range(m):
  if H[l[i][0]-1]==max(H) and W[l[i][1]-1]==max(W):
    ct+=1
if cthmax*ctwmax>ct:
  print(hmax+wmax)
else:
  print(hmax+wmax-1)

Submission status: Time Limit Exceeded

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from math import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
import heapq
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
#
# インデックス系
# int min_y = max(0, i - 2), max_y = min(h - 1, i + 2);
# int min_x = max(0, j - 2), max_x = min(w - 1, j + 2);
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
INF = float('inf')
from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
    return int(readline())
H,W,M = readInts()

dic1 = defaultdict(int)
dic2 = defaultdict(int)
s = set()
for i in range(M):
    h,w = map(lambda x:int(x)-1, input().split())
    dic1[h] += 1
    dic2[w] += 1
    s.add((h,w))
# print(dic)
ans = 0


# 辞書の値が最大・最小となるキーを取得
max1 = max(dic1.values())
max2 = max(dic2.values())
for k1,v1 in dic1.items():
    if v1 < max1:
        continue
    for k2,v2 in dic2.items():
        if v2 < max2:
            continue
        if (k1,k2) in s:
            ans = max(ans,dic1[k1] + dic2[k2] - 1)
        else:
            ans = max(ans,dic1[k1] + dic2[k2])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W, M = map(int, input().split())
m = [[False] * W for _ in range(H)]
dh = dict()
dw = dict()
for _ in range(M):
    h_, w_ = map(int, input().split())
    m[h_ - 1][w_ - 1] = True
    
    if h_ in dh:
        dh[h_] += 1
    else:
        dh[h_] = 1
        
    if w_ in dw:
        dw[w_] += 1
    else:
        dw[w_] = 1

dhl = list(dh.items())
dhl.sort(key=lambda x: x[1], reverse=True)
dwl = list(dw.items())
dwl.sort(key=lambda x: x[1], reverse=True)

h, w = [], []
maxh = dhl[0][1]
maxw = dwl[0][1]

for i, n in dhl:
    if n == maxh:
        h.append(i)
for i, n in dwl:
    if n == maxw:
        w.append(i)

res = maxh + maxw - 1
for hh in h:
    for ww in w:
        if m[hh - 1][ww - 1] is False:
            res = maxh + maxw
            break

print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from bisect import bisect_left,bisect
def main():
    h, w, m = map(int,input().split()) 
    h_lis = [0] * (h+1)
    w_lis = [0] * (w+1)
    bombs = set([tuple(map(int,input().split())) for i in range(m)])
    for hh, ww in bombs:
        h_lis[hh] += 1
        w_lis[ww] += 1
    h_max_index = [i for i, h in enumerate(h_lis) if h == max(h_lis)]
    w_max_index = [i for i, w in enumerate(w_lis) if w == max(w_lis)]
    for h_i in h_max_index:
        for w_i in w_max_index:
            if not (h_i, w_i) in bombs:
                print(h_lis[h_i] + w_lis[w_i])
                sys.exit()
    else:
        print(h_lis[h_i] + w_lis[w_i] - 1)

if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from bisect import bisect_left,bisect
def main():
    h, w, m = map(int,input().split()) 
    h_lis = [0] * h
    w_lis = [0] * w
    hw_lis = [] 
    for i in range(m):
        hh, ww = map(int,input().split())
        hh -= 1
        ww -= 1
        h_lis[hh] += 1
        w_lis[ww] += 1
        hw_lis.append((hh, ww))
    max_h = max(h_lis)
    max_w = max(w_lis)
    h_max_index = [i for i, h in enumerate(h_lis) if h == max_h]
    w_max_index = [i for i, w in enumerate(w_lis) if w == max_w]
    for h_i in h_max_index:
        for w_i in w_max_index:
            if not (h_i, w_i) in hw_lis:
                print(h_lis[h_i] + w_lis[w_i])
                sys.exit()
    else:
        print(h_lis[h_i] + w_lis[w_i] - 1)

if __name__=='__main__':
    main()