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:
h,w,m=map(int,input().split())
A=[0]*h ; B=[0]*w ;G=[]
for i in range(m):
    x,y=map(int,input().split())
    A[x-1]+=1 ; B[y-1]+=1
    G.append([x-1,y-1])

q=max(A) ; e=max(B)
Q=[i for i in range(h) if A[i]==q] 
W=[j for j in range(w) if B[j]==e];#print(A,B,Q,W)
grid={}
for i in Q:
    for j in W:
        grid[ (i,j) ]=0



for i in range(m):
    x,y=G[i]
    if A[x]==q and B[y]==e:
        grid[(x,y)]=1
#print(grid)
if 0 in grid.values():
    print(q+e)
else:
    print(q+e-1)

Submission status: Time Limit Exceeded

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

maxh = max(h)
maxw = max(w)

listh = [i for i, v in enumerate(h, 1) if v == maxh]
listw = [j for j, v in enumerate(w, 1) if v == maxw]

b = [(i, j) for i in listh for j in listw if (i, j) not in Mem]

if len(b):
  ans = maxh + maxw
else:
  ans = maxh + maxw - 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w,m = map(int,input().split())
bombX = [0]*h
bombY = [0]*w
bomb = [0]*m
maxX = maxY = 0
for i in range(m):
    i,j = map(lambda x:int(x)-1, input().split())
    bomb.append((i,j))
    bombX[i] += 1
    bombY[j] += 1
    maxX = max(maxX,bombX[i])
    maxY = max(maxY,bombY[j])
maxX_index = list(i for i in range(h) if bombX[i] == maxX)
maxY_index = list(i for i in range(w) if bombY[i] == maxY)
for i in maxX_index:
    for j in maxY_index:
        if (i,j) in bomb: continue
        print(maxX+maxY)
        exit()
print(maxX+maxY-1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
h,w,m = map(int,input().split())
bombX = [0]*h
bombY = [0]*w
bomb = []
maxX = maxY = 0
for i in range(m):
    i,j = map(lambda x:int(x)-1, input().split())
    bomb.append((i,j))
    bombX[i] += 1
    bombY[j] += 1
    maxX = max(maxX,bombX[i])
    maxY = max(maxY,bombY[j])
maxX_index = list(i for i in range(h) if bombX[i] == maxX)
maxY_index = list(i for i in range(w) if bombY[i] == maxY)
for i in maxX_index:
    for j in maxY_index:
        if (i,j) in bomb: continue
        print(maxX+maxY)
        exit()
print(maxX+maxY-1)

Submission status: Time Limit Exceeded

Solution code:
x, y, m = map(int,input().split())
mat = []
r = [0]*x
c = [0]*y
smx = 0
smy = 0
for _ in range(m):
    p, q = map(int,input().split())
    p -= 1
    q -= 1
    mat.append((p,q))
    r[p] += 1
    c[q] += 1
    smx = max(smx,r[p])
    smy = max(smy,c[q])
xs = []
ys = []
for i in range(x):
    if smx == r[i]: xs.append(i)
for i in range(y):
    if smy == c[i]: ys.append(i)

ans = smx + smy - 1

for h in xs:
    for w in ys:
        if (h, w) in mat: continue
        print(ans + 1)
        exit()
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W, M = map(int, input().split())
P = []
X = [0 for i in range(H)]
Y = [0 for j in range(W)]
s = set([])
for i in range(M):
  h, w = map(int, input().split())
  X[h-1] += 1
  Y[w-1] += 1
  s.add((h-1, w-1)) #タプルを使う
I = [i for i in range(H) if X[i] == max(X)]
J = [j for j in range(W) if Y[j] == max(Y)]
flag = 0
for i in I:
  for j in J:
    if (i, j) not in s:
      flag = 1
      print(max(X)+max(Y))
      break
  if flag == 1:
    break
if flag == 0:
  print(max(X)+max(Y)-1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
h,w,m = map(int,input().split())
bombX = [0]*h
bombY = [0]*w
bomb = []
maxX = maxY = 0
for i in range(m):
    i,j = map(lambda x:int(x)-1, input().split())
    bomb.append((i,j))
    bombX[i] += 1
    bombY[j] += 1
    maxX = max(maxX,bombX[i])
    maxY = max(maxY,bombY[j])
maxX_index = list(i for i in range(h) if bombX[i] == maxX)
maxY_index = list(i for i in range(w) if bombY[i] == maxY)
for i in maxX_index:
    for j in maxY_index:
        if (i,j) not in bomb:
            print(maxX+maxY)
            exit()
print(maxX+maxY-1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
h,w,m = map(int,input().split())
bombX = [0]*h
bombY = [0]*w
bomb = []
for i in range(m):
    i,j = map(lambda x:int(x)-1, input().split())
    bomb.append((i,j))
    bombX[i] += 1
    bombY[j] += 1
maxX = max(bombX)
maxY = max(bombY)
maxX_index = list(i for i in range(h) if bombX[i] == maxX)
maxY_index = list(i for i in range(w) if bombY[i] == maxY)
for i in maxX_index:
    for j in maxY_index:
        if (i,j) not in bomb:
            print(maxX+maxY)
            exit()
print(maxX+maxY-1)

Submission status: Time Limit Exceeded

Solution code:
x, y, m = map(int,input().split())
mat = [[0 for i in range(y)] for j in range(x)]
for _ in range(m):
    p, q = map(int,input().split())
    p -= 1
    q -= 1
    mat[p][q] = 1

r = []
c = []
for i in range(x):
    sm = 0
    for j in range(y):
        sm += mat[i][j]
        if j == y - 1:
            r.append(sm)
for i in range(y):
    sm = 0
    for j in range(x):
        sm += mat[j][i]
        if j == x - 1:
            c.append(sm)
mx = 0
for i in range(x):
    for j in range(y):
        if mat[i][j] == 1:
            mx = max(mx,r[i]+c[j]-1)
        else:
            mx = max(mx,r[i]+c[j])
print(mx)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
h,w,m = map(int,input().split())
bomb = list(tuple(map(lambda x: int(x)-1,input().split())) for i in range(m))
bombX = [0]*h
bombY = [0]*w
for i,j in bomb:
    bombX[i] += 1
    bombY[j] += 1
maxX = max(bombX)
maxY = max(bombY)
maxX_index = list(i for i in range(h) if bombX[i] == maxX)
maxY_index = list(i for i in range(w) if bombY[i] == maxY)
for i in maxX_index:
    for j in maxY_index:
        if (i,j) not in bomb:
            print(maxX+maxY)
            exit()
print(maxX+maxY-1)

Submission status: Time Limit Exceeded

Solution code:
import pprint
H, W, M = map(int, input().split())
bombs = set()
hCount = [0]*H
vCount = [0]*W

for i in range(M):
    h, w = map(int, input().split())
    h -= 1; w -= 1;
    hCount[h] += 1
    vCount[w] += 1
    bombs.add((h, w))

hMax = max(hCount)
vMax = max(vCount)
hMaxIndex = [i for i, x in enumerate(hCount) if x==max(hCount)]
vMaxIndex = [i for i, x in enumerate(vCount) if x==max(vCount)]

ans = -1
for x in vMaxIndex:
    for y in hMaxIndex:
        if (y, x) in bombs:
            continue
        else:
            print(hMax + vMax)
            exit()
print(hMax + vMax - 1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
h,w,m = map(int,input().split())
bomb = list(tuple(map(lambda x: int(x)-1,input().split())) for i in range(m))
MapX = [[0]*w for i in range(h)]
MapY = [[0]*h for i in range(w)]
for i in bomb:
    MapX[i[0]][i[1]] = 1
    MapY[i[1]][i[0]] = 1
bombx = list(i.count(1) for i in MapX)
bomby = list(i.count(1) for i in MapY)
maxx = max(bombx)
maxy = max(bomby)
maxx_index = list(i for i in range(h) if bombx[i] == maxx)
maxy_index = list(i for i in range(w) if bomby[i] == maxy)
flag = 0
for i in maxx_index:
    for j in maxy_index:
        if (i,j) not in bomb:
            print(maxx+maxy)
            exit()
print(maxx+maxy-1)

Submission status: Time Limit Exceeded

Solution code:
def main():
    H, W, M = (int(i) for i in input().split())
    hc = [0]*(H+1)
    wc = [0]*(W+1)
    maps = set()
    for _ in range(M):
        h, w = (int(i) for i in input().split())
        hc[h] += 1
        wc[w] += 1
        maps.add((h, w))
    mah = -1
    maw = -1
    hmaps = set()
    wmaps = set()
    for i, h in enumerate(hc):
        if mah < h:
            mah = h
            hmaps = {i}
        elif mah == h:
            hmaps.add(i)
    for i, w in enumerate(wc):
        if maw < w:
            maw = w
            wmaps = {i}
        elif maw == w:
            wmaps.add(i)
    ans = mah+maw
    aru = True

    for h in list(hmaps):
        for w in list(wmaps):
            if (h, w) not in maps:
                aru = False
                break
    if aru:
        print(ans - 1)
    else:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

num_h = [0] * (h+1)
num_w = [0] * (w+1)
bomb_set = set()
for hi,wi in bomb:
  num_h[hi] += 1
  num_w[wi] += 1
  bomb_set.add((hi,wi))
  
max_hs = [k for k,v in enumerate(num_h) if v == max(num_h)]
max_ws = [k for k,v in enumerate(num_w) if v == max(num_w)]

result = max(num_h) + max(num_w)
for i in max_hs:
  for j in max_ws:
    if (i,j) not in bomb_set:
      print(result)
      exit(0)
print(result-1)

Submission status: Time Limit Exceeded

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

num_h = [0] * (h+1)
num_w = [0] * (w+1)
for hi,wi in bomb:
  num_h[hi] += 1
  num_w[wi] += 1
  
max_hs = [k for k,v in enumerate(num_h) if v == max(num_h)]
max_ws = [k for k,v in enumerate(num_w) if v == max(num_w)]

result = max(num_h) + max(num_w)
for i in max_hs:
  for j in max_ws:
    if [i,j] not in bomb:
      print(result)
      exit(0)
print(result-1)

Submission status: Time Limit Exceeded

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

num_h = [0] * (h+1)
num_w = [0] * (w+1)
for hi,wi in bomb:
  num_h[hi] += 1
  num_w[wi] += 1
  
max_hs = [k for k,v in enumerate(num_h) if v == max(num_h)]
max_ws = [k for k,v in enumerate(num_w) if v == max(num_w)]

flg = 0
for i in max_hs:
  for j in max_ws:
    if [i,j] not in bomb:
      flg = 1

result = max(num_h) + max(num_w)
if flg == 1:
  print(result)
else:
  print(result-1)

Submission status: Time Limit Exceeded

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

bomb = []
row = [0] * H
column = [0] * W

for i in range(M):
    h, w = map(int, input().split())
    h -= 1
    w -= 1
    
    bomb.append([h, w])
    
    row[h] += 1
    column[w] += 1
    
row_max = max(row)
col_max = max(column)

r_check = []
c_check = []
for i in range(H):
    if row[i] == row_max:
        r_check.append(i)

for i in range(W):
    if column[i] == col_max:
        c_check.append(i)

num = len(r_check) * len(c_check)

ans = row_max + col_max - 1
if M < num:
    ans = row_max + col_max
else:
    for r in r_check:
        for c in c_check:
            if not [r, c] in bomb:
                ans = row_max + col_max
                break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter
H,W,m = map(int,input().split())
h_ls = [0] * H
w_ls = [0] * W
bombers = [[0,0] for _ in range(m)]
for i in range(m):
    h,w = map(int,input().split())
    h_ls[h-1] += 1
    w_ls[w-1] += 1
    bombers[i] = [h-1,w-1]

h_max = max(h_ls)
h_counter = Counter(h_ls)
# Couter使ってみる
h_max_args = [0]*h_counter[h_max]
next_ind = 0
for i in range(H):
    if h_ls[i] == h_max:
        h_max_args[next_ind] = i
        next_ind += 1

w_max = max(w_ls)
w_counter = Counter(w_ls)
w_max_args = [0]*w_counter[w_max]
next_ind = 0
for i in range(W):
    if w_ls[i] == w_max:
        w_max_args[next_ind] = i
        next_ind += 1

for h in h_max_args:
    for w in w_max_args:
        if not [h,w] in bombers:
            print(w_max+h_max)
            sys.exit()

print(w_max+h_max-1)

Submission status: Time Limit Exceeded

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

for i in range(M):
    ARR.append(list(map(int,input().split())))

def calculate(h, w, m, arr):
    points = set()



    rowSum = [0 for i in range(h)]
    columnSum = [0 for i in range(w)]

    for ar in arr:
        points.add((ar[0]-1, ar[1]-1))
        rowSum[ar[0]-1] += 1
        columnSum[ar[1]-1] += 1

    maxRowSum = max(rowSum)
    maxColumnSum = max(columnSum)


    a0 = []

    for i in range(len(rowSum)):
        if rowSum[i] == maxRowSum:
            a0.append(i)

    a1 = []
    for i in range(len(columnSum)):
        if columnSum[i] == maxColumnSum:
            a1.append(i)

    result = 0
    for hIndex in a0:
        for wIndex in a1:
            if (hIndex,wIndex) in points:
                result = max(result, maxRowSum + maxColumnSum - 1)
            else:
                result = max(result, maxRowSum + maxColumnSum)

    print(result)


calculate(H, W, M, ARR)

Submission status: Time Limit Exceeded

Solution code:
import sys
H,W,m = map(int,input().split())
h_ls = [0] * H
w_ls = [0] * W
bombers = [[0,0] for _ in range(m)]
for i in range(m):
    h,w = map(int,input().split())
    h_ls[h-1] += 1
    w_ls[w-1] += 1
    bombers[i] = [h-1,w-1]
h_max = max(h_ls)
h_max_args = [-1] * H
for i in range(H):
    if h_ls[i] == h_max:
        h_max_args[i] = i

w_max = max(w_ls)
w_max_args = [-1] * W
for i in range(W):
    if w_ls[i] == w_max:
        w_max_args[i] = i

for h in h_max_args:
    for w in w_max_args:
        if h == -1 or w == -1:
            continue
        if not [h,w] in bombers:
            print(w_max+h_max)
            sys.exit()

print(w_max+h_max-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
H,W,m = map(int,input().split())
h_ls = [0] * H
w_ls = [0] * W
bombers = [[0,0] for _ in range(m)]
for i in range(m):
    h,w = map(int,input().split())
    h_ls[h-1] += 1
    w_ls[w-1] += 1
    bombers[i] = [h-1,w-1]
h_max = max(h_ls)
h_max_args = []
for i in range(H):
    if h_ls[i] == h_max:
        h_max_args.append(i)

w_max = max(w_ls)
w_max_args = []
for i in range(W):
    if w_ls[i] == w_max:
        w_max_args.append(i)

for h in h_max_args:
    for w in w_max_args:
        if not [h,w] in bombers:
            print(w_max+h_max)
            sys.exit()

print(w_max+h_max-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
H,W,m = map(int,input().split())
h_ls = [0] * H
w_ls = [0] * W
bombers = [[0,0] for _ in range(m)]
for i in range(m):
    h,w = map(int,input().split())
    h_ls[h-1] += 1
    w_ls[w-1] += 1
    bombers[i] = [h-1,w-1]
h_max = 0
h_max_args = []
for i in range(H):
    if h_max == h_ls[i]:
        h_max_args.append(i)
    elif h_max < h_ls[i]:
        h_max = h_ls[i]
        h_max_args = [i]

w_max = 0
w_max_args = []
for i in range(W):
    if w_max == w_ls[i]:
        w_max_args.append(i)
    elif w_max < w_ls[i]:
        w_max = w_ls[i]
        w_max_args = [i]

for h in h_max_args:
    for w in w_max_args:
        if not [h,w] in bombers:
            print(w_max+h_max)
            sys.exit()

print(w_max+h_max-1)

Submission status: Time Limit Exceeded

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

hhh = max(h)
www = max(w)
hhhh = [i for i, x in enumerate(h) if x == hhh]
wwww = [i for i, x in enumerate(w) if x == www]
check = 0

for i in hhhh:
    for j in wwww:
        if (i, j) not in DP:
            print(hhh + www)
            exit()
print(hhh + www - 1)

Submission status: Time Limit Exceeded

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

hhh = max(h)
www = max(w)
hhhh = [i for i, x in enumerate(h) if x == hhh]
wwww = [i for i, x in enumerate(w) if x == www]
check = 0

for i in hhhh:
    for j in wwww:
        if (i, j) not in DP:
            check = 1
            break
print(hhh + www - (1 - check))

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int,input().split())
d_h=[0]*(h+1)
d_w=[0]*(w+1)
d=set()
for i in range(m):
  nh,nw=map(int,input().split())
  d_h[nh]+=1
  d_w[nw]+=1
  d.add((nh,nw))
ans=0
hantei=0
for i in range(1,h+1):
  hantei=0
  for j in range(1,w+1):
    if (i,j) in d:
      hantei=(d_h[i]+d_w[j]-1)
    else:
      hantei=(d_h[i]+d_w[j])
    if ans < hantei:
      ans=hantei
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def e_bomber():
    from collections import defaultdict
    H, W, M = [int(i) for i in input().split()]

    bomb_row = [0] * H
    bomb_col = [0] * W
    bomb_pos = defaultdict(int)
    for _ in range(M):
        r, c = [int(i) - 1 for i in input().split()]
        bomb_row[r] += 1
        bomb_col[c] += 1
        bomb_pos[(r, c)] += 1

    bomb_row_max = max(bomb_row)
    bomb_col_max = max(bomb_col)

    # 破壊できる数が最大になるような行と列の組を全探索する。
    # (行, 列) が破壊対象の上になってしまわないような組があれば、そこに置けばいい。
    # そのような組がまったくなければ、破壊対象の上に置かざるを得ない (-1 される)。
    flag = False
    for row in [r for r in range(H) if bomb_row_max == bomb_row[r]]:
        for col in [c for c in range(W) if bomb_col_max == bomb_col[c]]:
            if bomb_pos[(row, col)] == 0:
                return bomb_row_max + bomb_col_max
    return bomb_row_max + bomb_col_max - 1

print(e_bomber())

Submission status: Time Limit Exceeded

Solution code:
def e_bomber():
    from collections import defaultdict
    H, W, M = [int(i) for i in input().split()]
    Bomb = [[int(i) - 1 for i in input().split()] for j in range(M)]

    bomb_row = [0] * H
    bomb_col = [0] * W
    bomb_pos = defaultdict(int)
    for r, c in Bomb:
        bomb_row[r] += 1
        bomb_col[c] += 1
        bomb_pos[(r, c)] += 1

    bomb_row_max = max(bomb_row)
    bomb_col_max = max(bomb_col)

    # 破壊できる数が最大になるような行と列の組を全探索する。
    # (行, 列) が破壊対象の上になってしまわないような組があれば、そこに置けばいい。
    # そのような組がまったくなければ、破壊対象の上に置かざるを得ない (-1 される)。
    flag = False
    for row in [r for r in range(H) if bomb_row_max == bomb_row[r]]:
        for col in [c for c in range(W) if bomb_col_max == bomb_col[c]]:
            if bomb_pos[(row, col)] == 0:
                return bomb_row_max + bomb_col_max
    return bomb_row_max + bomb_col_max - 1

print(e_bomber())

Submission status: Time Limit Exceeded

Solution code:
def e_bomber():
    from collections import defaultdict
    H, W, M = [int(i) for i in input().split()]
    Bomb = [[int(i) - 1 for i in input().split()] for j in range(M)]

    bomb_row = [0] * H
    bomb_col = [0] * W
    bomb_pos = defaultdict(int)
    for r, c in Bomb:
        bomb_row[r] += 1
        bomb_col[c] += 1
        bomb_pos[(r, c)] += 1

    bomb_row_max = max(bomb_row)
    bomb_col_max = max(bomb_col)

    # 破壊できる数が最大になるような行と列の組を全探索する。
    # (行, 列) が破壊対象の上になってしまわないような組があれば、そこに置けばいい。
    # そのような組がまったくなければ、破壊対象の上に置かざるを得ない (-1 される)。
    flag = False
    for row in [r for r in range(H) if bomb_row_max == bomb_row[r]]:
        for col in [c for c in range(W) if bomb_col_max == bomb_col[c]]:
            if bomb_pos[(row, col)] == 0:
                return bomb_row_max + bomb_col_max
    return bomb_row_max + bomb_col_max - 1

print(e_bomber())

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
h, w, m = map(int, input().split())
targets = []
targets_count_yoko = [0]*w
targets_count_tate = [0]*h
for _ in range(m):
    y, x = map(int, input().split())
    y -= 1
    x -= 1
    targets_count_yoko[x] += 1
    targets_count_tate[y] += 1
    targets.append((y, x))

max_row = max(targets_count_yoko)
max_line = max(targets_count_tate)
y_idx = [i for i, v in enumerate(targets_count_tate) if v == max_line]
x_idx = [i for i, v in enumerate(targets_count_yoko) if v == max_row]

ans = max_line + max_row
flag = False
for y in y_idx:
    for x in x_idx:
        if (y, x) not in targets:
            flag = True
            break
    else:
        continue
    break

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
h, w, m = map(int, input().split())
targets = defaultdict(int)
targets_count_yoko = defaultdict(int)
targets_count_tate = defaultdict(int)
for _ in range(m):
    y, x = map(int, input().split())
    y -= 1
    x -= 1
    targets_count_yoko[x] += 1
    targets_count_tate[y] += 1
    targets[(y, x)] = -1

max_row = max(targets_count_yoko.values())
max_line = max(targets_count_tate.values())
y_idx = []
x_idx = []
max_count_x = 0
max_count_y = 0
for i in range(w):
    if targets_count_yoko[i] == max_row:
        x_idx.append(i)
        max_count_x += 1
for i in range(h):
    if targets_count_tate[i] == max_line:
        y_idx.append(i)
        max_count_y += 1


ans = max_line + max_row
kumi = max_count_x*max_count_y
for key_y, key_x in targets.keys():
    if key_y in y_idx and key_x in x_idx:
        kumi -= 1
    if kumi == 0:
        break

if kumi == 0:
    print(ans-1)
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
h, w, m = map(int, input().split())
targets = defaultdict(int)
targets_count_yoko = defaultdict(int)
targets_count_tate = defaultdict(int)
for _ in range(m):
    y, x = map(int, input().split())
    y -= 1
    x -= 1
    targets_count_yoko[x] += 1
    targets_count_tate[y] += 1
    targets[(y, x)] = -1

max_row = max(targets_count_yoko.values())
max_line = max(targets_count_tate.values())
y_idx = []
x_idx = []
max_count_x = 0
max_count_y = 0
for i in range(w):
    if targets_count_yoko[i] == max_row:
        x_idx.append(i)
        max_count_x += 1
for i in range(h):
    if targets_count_tate[i] == max_line:
        y_idx.append(i)
        max_count_y += 1


ans = max_line + max_row
kumi = max_count_x*max_count_y
for key_y, key_x in targets.keys():
    if key_y in y_idx and key_x in x_idx:
        kumi -= 1

if kumi > 0:
    print(ans)
else:
    print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
import itertools
H, W, M = map(int, input().split())
Bh = [0]*(H+1)
Bw = [0]*(W+1)
OD = []

for _ in range(M):
    h, w = map(int, input().split())
    Bh[h] += 1
    Bw[w] += 1
    OD.append((h, w))

M1 = max(Bh)
M2 = max(Bw)
l1 = [i for i, x in enumerate(Bh) if x == M1]
l2 = [j for j, x in enumerate(Bw) if x == M2]
for i, j in itertools.product(l1, l2):
    if (i, j) not in OD:
        break
else:
    print(M1+M2-1)
    exit()
print(M1+M2)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
h, w, m = map(int, input().split())
targets = defaultdict(int)
targets_count_yoko = defaultdict(int)
targets_count_tate = defaultdict(int)
for _ in range(m):
    y, x = map(int, input().split())
    y -= 1
    x -= 1
    targets_count_yoko[x] += 1
    targets_count_tate[y] += 1
    targets[(y, x)] = -1

max_row = max(targets_count_yoko.values())
max_line = max(targets_count_tate.values())
y_idx = []
x_idx = []
for i in range(w):
    if targets_count_yoko[i] == max_row:
        x_idx.append(i)
for i in range(h):
    if targets_count_tate[i] == max_line:
        y_idx.append(i)


ans = 0
for y in y_idx:
    for x in x_idx:
        res = max_line + max_row + targets[(y, x)]
        ans = max(ans, res)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
h, w, m = map(int, input().split())
targets = defaultdict(int)
targets_count_yoko = [0]*w
targets_count_tate = [0]*h
for _ in range(m):
    y, x = map(int, input().split())
    y -= 1
    x -= 1
    targets_count_yoko[x] += 1
    targets_count_tate[y] += 1
    targets[(y, x)] = -1

max_row = max(targets_count_yoko)
max_line = max(targets_count_tate)
y_idx = []
x_idx = []
for i in range(w):
    if targets_count_yoko[i] == max_row:
        x_idx.append(i)
for i in range(h):
    if targets_count_tate[i] == max_line:
        y_idx.append(i)


ans = 0
for y in y_idx:
    for x in x_idx:
        res = max_line + max_row + targets[(y, x)]
        ans = max(ans, res)

print(ans)

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
f = len(M1L) * len(M2L)
if f > M:
  print(M1+M2)
  exit()
for X in G3:
  if X[0] in M1L and X[1] in M2L:
    c += 1
#for i in M1L:
#  for j in M2L:
#    if [i,j] not in G3:
#      c = 1
#break
#  if c ==1:
#    break
if c == f:
  print(M1 + M2 - 1)
else:
  print(M1 + M2)

Submission status: Time Limit Exceeded

Solution code:
import itertools
H, W, M = map(int, input().split())
Bh = [0]*(H+1)
Bw = [0]*(W+1)
OD = set()

for _ in range(M):
    h, w = map(int, input().split())
    Bh[h] += 1
    Bw[w] += 1
    OD.add((h, w))

l1 = [i for i, x in enumerate(Bh) if x == max(Bh)]
l2 = [j for j, x in enumerate(Bw) if x == max(Bw)]
for i, j in itertools.product(l1, l2):
    if (i, j) not in OD:
        break
else:
    print(max(Bh)+max(Bw)-1)
    exit()
print(max(Bh)+max(Bw))

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
targets = []
targets_count_yoko = [0]*w
targets_count_tate = [0]*h
for _ in range(m):
    y, x = map(int, input().split())
    y -= 1
    x -= 1
    targets_count_yoko[x] += 1
    targets_count_tate[y] += 1
    targets.append((y, x))

max_row = max(targets_count_yoko)
max_line = max(targets_count_tate)
y_idx = []
x_idx = []
for i in range(w):
    if targets_count_yoko[i] == max_row:
        x_idx.append(i)
for i in range(h):
    if targets_count_tate[i] == max_line:
        y_idx.append(i)


ans = max_line + max_row - 1
for y in y_idx:
    for x in x_idx:
        if (y, x) not in targets:
            ans += 1
            break
    else:
        continue
    break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
targetRow = []
targetCol = []
targets = []
for i in range(m):
    row, col = map(int, input().split())
    targetRow += [row]
    targetCol += [col]
    targets.append((row, col))
maxCtr = 0
for r in range(1, h+1):
    ctRow = targetRow.count(r)
    for c in range(1, w+1):
        ctCol = targetCol.count(c)
        ct = ctRow + ctCol
        if (r,c) in targets:
            ct = ct-1
        if ct > maxCtr:
            maxCtr = ct
print(maxCtr)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
targets = []
targets_count_tate = [0]*w
targets_count_yoko = [0]*h
for _ in range(m):
    y, x = map(int, input().split())
    y -= 1
    x -= 1
    targets_count_tate[x] += 1
    targets_count_yoko[y] += 1
    targets.append((y, x))


ans = 0
for y in range(h):
    for x in range(w):
        res = targets_count_tate[x] + targets_count_yoko[y]
        if (y, x) in targets:
            res -= 1
        ans = max(ans, res)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
targetRow = []
targetCol = []
targets = []
for i in range(m):
    row, col = map(int, input().split())
    targetRow += [row]
    targetCol += [col]
    targets.append((row, col))
dp = {}
for r in range(1, h+1):
    ctRow = targetRow.count(r)
    for c in range(1, w+1):
        ctCol = targetCol.count(c)
        ct = ctRow + ctCol
        if (r,c) in targets:
            dp[(r, c)] = ct-1
        else:
            dp[(r, c)] = ct
print(max(dp.values()))

Submission status: Time Limit Exceeded

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

MAP=[["." for _ in range(w)] for _ in range(h)]
H,W=[0 for _ in range(h)],[0 for _ in range(w)]
for i,j in HW:
    i,j=i-1,j-1
    MAP[i][j]="#"
    H[i] +=1
    W[j] +=1

max_h,max_w=max(H),max(W)
ans=max_h+max_w
cnt=H.count(max_h)*W.count(max_w)
for i,j in HW:
    if H[i-1]==max_h and W[j-1]==max_w:
        cnt -=1
print(ans if cnt!=0 else ans-1)

Submission status: Time Limit Exceeded

Solution code:
H, W, M, *HW = map(int, open(0).read().split())

R = HW[::2]
C = HW[1::2]

A = [0] * (H + 1)
B = [0] * (W + 1)
for h, w in zip(*[iter(HW)] * 2):
    A[h] += 1
    B[w] += 1

S = set(zip(*[iter(HW)] * 2))

ans = 0
for r in R:
    for c in C:
        ans = max(ans, A[r] + B[c] - ((r, c) in S))

print(ans)

Submission status: Time Limit Exceeded

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

MAP=[["." for _ in range(w)] for _ in range(h)]
H,W=[0 for _ in range(h)],[0 for _ in range(w)]
for i,j in HW:
    i,j=i-1,j-1
    MAP[i][j]="#"
    H[i] +=1
    W[j] +=1

max_h,max_w=max(H),max(W)
X,Y=[],[]
for i in range(h):
    if H[i]==max_h:
        X.append(i)
for i in range(w):
    if W[i]==max_w:
        Y.append(i)

flag=0
for i in X:
    for j in Y:
        if MAP[i][j]==".":
            flag=1
ans=max_w+max_h
print(ans if flag else ans-1)

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    h, w, m = map(int,input().split())
    bomb = [tuple(map(lambda x: int(x)-1, input().split())) for _ in range(m)]
    x = [0] * h
    y = [0] * w

    for h,w in bomb:
        x[h] +=1
        y[w] +=1

    maxX = max(x)
    maxY = max(y)

    r = [h for h,x in enumerate(x) if x == maxX]
    c = [w for w,y in enumerate(y) if y == maxY]

    for ri in r:
        for ci in c:
            if (ri,ci) not in bomb:
                print(maxX + maxY)
                sys.exit()
    print(maxX + maxY -1)
    
main()

Submission status: Time Limit Exceeded

Solution code:
import sys

h, w, m = map(int,input().split())
bomb = [tuple(map(lambda x: int(x)-1, input().split())) for _ in range(m)]
x = [0] * h
y = [0] * w

for h,w in bomb:
    x[h] +=1
    y[w] +=1

maxX = max(x)
maxY = max(y)

r = [h for h,x in enumerate(x) if x == maxX]
c = [w for w,y in enumerate(y) if y == maxY]

for ri in r:
    for ci in c:
        if (ri,ci) not in bomb:
            print(maxX + maxY)
            sys.exit()
print(maxX + maxY -1)

Submission status: Time Limit Exceeded

Solution code:
import itertools

h, w, m = map(int, input().split())
spot = []
tate = [0 for i in range(h)]
yoko = [0 for i in range(w)]
for i in range(m):
  x, y = map(int, input().split())
  spot.append([x, y])
  tate[x-1] += 1
  yoko[y-1] += 1
mt = max(tate)
my = max(yoko)
maxh = []
maxw = []
for i in range(h):
  if(tate[i] == mt):
    maxh.append(i)
for i in range(w):
  if(yoko[i] == my):
    maxw.append(i)
target = list(itertools.product(maxh, maxw))
for i in range(len(target)):
  target[i] = list(target[i])
for i in range(m):
  x = spot[i][0]-1
  y = spot[i][1]-1
  if([x, y] in target):
    del target[target.index([x, y])]
if(len(target) > 0):
  print(mt+my)
else:
  print(mt+my-1)