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: Runtime Error

Solution code:
H, W, M = map(int, input().split())
hws = [list(map(lambda x :int(x)-1, input().split())) for _ in range(M)]

booms = [set() for _ in range(H)]

yoko = [0 for _ in range(W)]
tate = [0 for _ in range(H)]

for hw in hws:
    tate[hw[0]] += 1
    yoko[hw[1]] += 1
    booms[hw[0]].add(hw[1])
    
max_tate = max(tate)
max_yoko = max(yoko)


tate_candidates = []
yoko_candidates = []

for i in range(H):
    if tate[i] == max_tate:
        tate_candidates.append(i)
        
for i in range(W):
    if yoko[i] == max_yoko:
        yoko_candidates.append(i)

exist = 0
for tate_candidate in tate_candidates:
    for yoko_candidate in yoko_candidates:
        if yoko_candidate not in booms[tate_candidate]:
            exist = 1
			break
    else:
      continue
    break
print(max_tate + max_yoko + (exist-1))

Submission status: Runtime Error

Solution code:
H, W, M = map(int, input().split())
hws = [list(map(lambda x :int(x)-1, input().split())) for _ in range(M)]

booms = [set() for _ in range(H)]

yoko = [0 for _ in range(W)]
tate = [0 for _ in range(H)]

for hw in hws:
    tate[hw[0]] += 1
    yoko[hw[1]] += 1
    booms[hw[0]].add(hw[1])
    
max_tate = max(tate)
max_yoko = max(yoko)


tate_candidates = []
yoko_candidates = []

for i in range(H):
    if tate[i] == max_tate:
        tate_candidates.append(i)
        
for i in range(W):
    if yoko[i] == max_yoko:
        yoko_candidates.append(i)

exist = 0
for tate_candidate in tate_candidates:
    for yoko_candidate in yoko_candidates:
        if yoko_candidate not in booms[tate_candidate]:
            exist = 1
			break
print(max_tate + max_yoko + (exist-1))

Submission status: Runtime Error

Solution code:
f = open("C:/Users/naoki/Desktop/Atcoder/input.txt")

H, W, M = map(int, f.readline().split())
hws = [list(map(lambda x :int(x)-1, f.readline().split())) for _ in range(M)]

booms = [set() for _ in range(H)]

yoko = [0 for _ in range(W)]
tate = [0 for _ in range(H)]

for hw in hws:
    tate[hw[0]] += 1
    yoko[hw[1]] += 1
    booms[hw[0]].add(hw[1])
    
max_tate = max(tate)
max_yoko = max(yoko)


tate_candidates = []
yoko_candidates = []

for i in range(H):
    if tate[i] == max_tate:
        tate_candidates.append(i)
        
for i in range(W):
    if yoko[i] == max_yoko:
        yoko_candidates.append(i)

exist = 0
for tate_candidate in tate_candidates:
    for yoko_candidate in yoko_candidates:
        if yoko_candidate not in booms[tate_candidate]:
            exist = 1
            break

print(max_tate + max_yoko + (exist-1))

Submission status: Runtime Error

Solution code:
f = open("C:/Users/naoki/Desktop/Atcoder/input.txt")

H, W, M = map(int, f.readline().split())
hws = [list(map(lambda x :int(x)-1, f.readline().split())) for _ in range(M)]

booms = [set() for _ in range(H)]

yoko = [0 for _ in range(W)]
tate = [0 for _ in range(H)]

for hw in hws:
    tate[hw[0]] += 1
    yoko[hw[1]] += 1
    booms[hw[0]].add(hw[1])
    
max_tate = max(tate)
max_yoko = max(yoko)


tate_candidates = []
yoko_candidates = []

for i in range(H):
    if tate[i] == max_tate:
        tate_candidates.append(i)
        
for i in range(W):
    if yoko[i] == max_yoko:
        yoko_candidates.append(i)

exist = 0
for tate_candidate in tate_candidates:
    for yoko_candidate in yoko_candidates:
        if yoko_candidate not in booms[tate_candidate]:
            exist = 1

print(max_tate + max_yoko + (exist-1))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.buffer.readline
 
H, W, M = map(int, input().split())
 

for _ in range(M):
    h, w = map(int, input().split())
    h -= 1
    w -= 1
    Y[h] += 1
    X[w] += 1
    Map.append((h, w))
 
MX = max(X)
MY = max(Y)
ans = MX + MY
Xans = set()
Yans = set()
for i, x in enumerate(X):
    if x == MX:
        Xans.add(i)
for i, y in enumerate(Y):
    if y == MY:
        Yans.add(i)
cnt = 0
for h, w in Map:
    if h in Yans and w in Xans:
        cnt += 1
if cnt == len(Xans) * len(Yans):
    ans -= 1
print(ans)

Submission status: Runtime Error

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

X = [0] * W
Y = [0] * H
Map = [[0] * W for _ in range(H)]

for _ in range(M):
    h, w = map(int, input().split())
    h -= 1
    w -= 1
    Y[h] += 1
    X[w] += 1
    Map[h][w] = 1

MX = max(X)
MY = max(Y)
ans = MX + MY - 1
Xans = []
Yans = []
for i, x in enumerate(X):
    if x == MX:
        Xans.append(i)
for i, y in enumerate(Y):
    if y == MY:
        Yans.append(i)
flag = False
for x in Xans:
    if flag:
        break
    for y in Yans:
        if Map[y][x] == 0:
            ans += 1
            flag = True
            break
print(ans)

Submission status: Runtime Error

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

X = [0] * W
Y = [0] * H
Map = [[0] * W for _ in range(H)]

for _ in range(M):
    h, w = map(int, input().split())
    h -= 1
    w -= 1
    Y[h] += 1
    X[w] += 1
    Map[h][w] = 1

MX = max(X)
MY = max(Y)
ans = MX + MY - 1
Xans = []
Yans = []
for i, x in enumerate(X):
    if x == MX:
        Xans.append(i)
for i, y in enumerate(Y):
    if y == MY:
        Yans.append(i)
for x in Xans:
    for y in Yans:
        if Map[y][x] == 0:
            ans += 1
            break
print(ans)

Submission status: Runtime Error

Solution code:
import math
import collections
import bisect
from collections import deque
from copy import copy, deepcopy
import time

import numpy as np
import numba
from numba import njit, b1, i4, i8, f8
from numba import jit


def main():
    H,W,M = map(int,input().split())
    h={}
    w={}
    bomb_list=[]
    for i in range(M):
        ih,iw = map(int,input().split())
        bomb_list.append([ih,iw])
        if ih not in h:
            h[ih]=1
        else:
            h[ih]+=1
        if iw not in w:
            w[iw]=1
        else:
            w[iw]+=1

    max_h = [k for k, v in h.items() if v == max(h.values())]
    max_w = [k for k, v in w.items() if v == max(w.values())]

    for h,w in bomb_list:
        if h not in max_h:
            if w not in max_w:
                print(max(h.values())+max(w.values()))
                return
                
    
    print(max(h.values())+max(w.values())-1)
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n,m,t=map(int,input().split())

grid=[[0]*m for _ in range(n)]

row=[0]*n
col=[0]*m

for _ in range(t):
    x,y=map(int,input().split())
    grid[x-1][y-1]=1
    row[x-1]+=1
    col[y-1]+=1

prow=[]
pcol=[]
q=max(row)
for i in range(n):
    if row[i]==q :
        prow.append(i)

q=max(col)
for i in range(m):
    if col[i]==q :
        pcol.append(i)

ans=0
for i in prow :
    for j in pcol:
        if grid[i][j]==0 :
            ans=row[i]+col[j]
            break
        else:
            ans=row[i]+col[j]-1

print(ans)

Submission status: Runtime Error

Solution code:
#爆弾を置く行；対象が最多の行
#爆弾を置く列；最多の行を除いたグリッドで、対象が最多の列
#答え；まず行方向に爆破し、残ったグリッドで列方向に爆破して合算する

#import statistics
H, W, M = map(int, input().split())#行数H、列数W、爆弾数M
lh = []#対象物の行座標リスト
lw = []#対象物の列座標リスト＿
lw2 = []#列座標リストから爆破済みの対象物の列座標を除いたリスト
ans = 0#爆破数
#対象物の座標を行と列で分けてリスト化
for i in range(M):
    bh, bw = map(int, input().split())
    lh.append(bh - 1)#行方向
    lw.append(bw - 1)#列方向

#最頻値が複数の場合、以下の式だとエラーになる
#hmode = statistics.mode(lh)#対象物が最多の行
hmode = max(lh, key = lh.count)#対象物が最多の行
ans += lh.count(hmode)#行方向の爆破最大数

#爆破済みの対象物を除いた新しい列座標リストを作る
for i in range(M):
    if lh[i] != hmode:
        lw2.append(lw[i])
wmode = max(lw2, key = lw2.count)
ans += lw2.count(wmode)#列方向の爆破最大数

print(ans)

Submission status: Runtime Error

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    H, W, M = map(int, readline().split())
    row = [0] * H
    column = [0] * W
    grid = [[0] * W for _ in range(H)]

    for _ in range(M):
        h, w = map(int, readline().split())
        row[h-1] += 1
        column[w-1] += 1
        grid[h-1][w-1] += 1

    max_row = [i for i,x in enumerate(row) if x == max(row)]
    max_column = [i for i,x in enumerate(column) if x == max(column)]
    ans = 0
    for h in max_row:
        for w in max_column:
            tmp = row[h] + column[w] - grid[h][w]
            ans = max(tmp,ans)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    H, W, M = map(int, readline().split())
    row = [0] * H
    column = [0] * W
    grid = [[0] * W for _ in range(H)]

    for _ in range(M):
        h, w = map(int, readline().split())
        row[h-1] += 1
        column[w-1] += 1
        grid[h-1][w-1] += 1

    max_row = [i for i,x in enumerate(row) if x == max(row)]
    max_column = [i for i,x in enumerate(column) if x == max(column)]
    ans = 0
    for h in max_row:
        for w in max_column:
            tmp = row[h] + column[w] - grid[h][w]
            ans = max(tmp,ans)
    print(tmp)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import Counter

H, W, M = map(int, input().split())
Bomb = [[0] * W for _ in range(H)]

counterH = Counter()
counterW = Counter()

for _ in range(M):
    hi, wi = map(lambda x: int(x) - 1, input().split())
    counterH[hi] += 1
    counterW[wi] += 1
    Bomb[hi][wi] += 1

# for row in Bomb:
#     print(*row)


HH = [h for (h, _) in counterH.most_common(1)]
WW = [w for (w, _) in counterW.most_common(1)]

def count(h, w):
    bomb = 0
    for _w in range(W):
        if Bomb[h][_w] > 0:
            bomb += 1
    for _h in range(H):
        if Bomb[_h][w] > 0:
            bomb += 1
    if Bomb[h][w] > 0:
        bomb -= 1
    return bomb

m = -1
for h in HH:
    for w in range(W):
        m = max(m, count(h, w))
for w in WW:
    for h in range(H):
        m = max(m, count(h, w))

print(m)
# # 2次元累積和
# BombW = [[0] * (W + 1) for _ in range(H)]
# BombH = [[0] * W for _ in range(H + 1)]
# for h in range(H):
#     for w in range(W):
#         BombW[h][w + 1] = BombW[h][w] + Bomb[h][w]
# for w in range(W):
#     for h in range(H):
#         BombH[h + 1][w] = BombH[h][w] + Bomb[h][w]
# for row in BombW:
#     print(*row)
# for row in BombH:
#     print(*row)

Submission status: Runtime Error

Solution code:
import math, sys

H,W,M=list(map(int, input().split()))
Bs=[]
Map=[ [False for _ in range(W+1)] for _ in range(H+1)]
for _ in range(M):
    bh,bw=list(map(int, input().split()))
    Map[bh][bw]=True
    Bs.append((bh,bw))
Hs=[0 for _ in range(H+1)]
Ws=[0 for _ in range(W+1)]

for each in Bs:
    hh,ww=each
    Hs[hh]+=1
    Ws[ww]+=1

hmax=0
wmax=0
for each in Hs:
    if each >hmax:
        hmax=each
for each in Ws:
    if each>wmax:
        wmax=each
hmax_list=[]
wmax_list=[]
for i in range(1,H+1):
    if Hs[i]==hmax:
        hmax_list.append(i)
for i in range(1,H+1):
    if Ws[i]==wmax:
        wmax_list.append(i)
ans=hmax+wmax
for hi in hmax_list:
    for wi in wmax_list:
        if not Map[hi][wi]:
            print(ans)
            sys.exit()
print(ans-1)

Submission status: Runtime Error

Solution code:
h, w, m = map(int, input().split())
bord_x, bord_y = [0 for i in range(h)], [0 for i in range(w)]
bord = [[0]*w for _i in range(h)]

hw = [list(map(int, input().split())) for _i in range(m)]

for s, t in hw:
    s -= 1
    t -= 1
    bord[s][t] += 1
    bord_x[s] += 1
    bord_y[t] += 1
x_max, y_max = max(bord_x), max(bord_y)
a = [x for x, p in enumerate(bord_x) if x_max==p]
b = [y for y, p in enumerate(bord_y) if y_max==p]

r = x_max + y_max - 1
for i in a:
    for j in b:
        if bord[i][j] >= 1:
            continue
        else:
            r += 1
            break
print(r)

Submission status: Runtime Error

Solution code:

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

h_b_list = [0 for i in range(h)]
w_b_list = [0 for i in range(w)]

targets = set()
for i in range(m):
  bh, bw = map(int, input().split())
  targets.add(bh, bw)
  h_b_list[bh-1] += 1
  w_b_list[bw-1] += 1
  
h_max = max(h_b_list)
w_max = max(w_b_list)

h_cans = []
w_cans = []
for i in range(h):
  if h_b_list[i] == h_max:
    h_cans.append(i)
    
for i in range(w):
  if w_b_list[i] == w_max:
    w_cans.append(i)

for hi in h_cans:
  for wi in w_cans:
    if (hi+1, wi+1) not in targets:
      print(h_max+w_max)
      exit()
else:
  print(h_max+w_max-1)

Submission status: Runtime Error

Solution code:
import numpy as np
h, w, m = map(int, input().split())

hw_map = np.zeros((h,w), int)


for i in range(m):
  bh, bw = map(int, input().split())
  hw_map[bh-1][bw-1] = 1

h_max = np.max(hw_map.sum(axis=1))
h_max_arg = np.argmax(hw_map.sum(axis=1))
hw_map_sub = np.delete(hw_map, h_max_arg, 0)
w_max = np.max(hw_map_sub.sum(axis=0))
print(int(h_max+w_max))

Submission status: Runtime Error

Solution code:
def sol(x,y):
    row = {}
    col = {}
    for i in x:
        if i not in row:
            row[i] = 1
        else:
            row[i] += 1
    for i in y:
        if i not in col:
            col[i] = 1
        else:
            col[i] += 1
    maxRow = 0
    maxCol = 0
    mR = 0
    mC = 0
    for i in row:
        if row[i]>mR:
            mR = row[i]
            maxRow = i
    for i in col:
        if col[i]>mC:
            mC = col[i]
            maxCol = i
    rd = {}
    cd = {}
    for i in row:
        if row[i] == mR:
            rd.add(i)
    for i in col:
        if row[i] == mC:
            cd.add(i)
    diff = rd.difference(cd)
    s = mR+mC
    if len(diff) == 0:
        s-=1
        print(s)
        return
    print(s)
    return

x,y,z = list(map(int,input().split(' ')))
row = []
col = []
for i in range(z):
    p = list(map(int,input().split(' ')))
    row.append(p[0])
    col.append(p[1])
sol(row,col)

Submission status: Runtime Error

Solution code:
H,W,M=map(int,input().split())
row=[0]*H
col=[0]*W
for i in range(M):
    h,w=map(int,input().split())
    row[h-1]+=1
    col[w-1]+=1
A=max(row)
B=max(col)
a=[x for x in range(H) if row[x]==A]
b=[x for x in range(W) if col[x]==B]
cnt=0
for i in a:
    for j in b:
        cnt+=1
        if (i,j) not in target:
            print(A+B)
            exit(0)
        if cnt>M:
            print(A+B-1)
            exit(0)
print(A+B-1)

Submission status: Runtime Error

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

for i in range(m):
  ih,iw = map(int,input().split())
  H[ih-1] += 1
  W[iw-1] += 1    
  dic[(ih,iw)]=True
  
mh = max(H)
mw = max(W)
ans = mh + mw

mh_idx = [i for i in range(h) if H[i]==mh]
mw_idx = [i for i in range(h) if W[i]==mw]
  
no_bomb = False
for hi in mh_idx:
  for wi in mw_idx:    
    if (hi+1,wi+1) not in dic:          
      no_bomb = True
      
if not no_bomb:
  ans -= 1
  
print(ans)

Submission status: Runtime Error

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

from pprint import pprint
import sys

sys.setrecursionlimit(10 ** 6)


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

num_bombs_each_row = [0] * H
num_bombs_each_col = [0] * W
bombs = set()

for _ in range(M):
    h, w = map(int, input().split())
    # 0-index
    h -= 1
    w -= 1

    bombs.add((h, w))
    num_bombs_each_row[h] += 1
    num_bombs_each_col[w] += 1

max_bombs_in_row = max(num_bombs_each_row)
max_bombs_in_col = max(num_bombs_each_col)

rows_with_max_bombs = []
for row in range(H):
    if num_bombs_each_row[row] == max_bombs_in_row:
        rows_with_max_bombs.append(row)

cols_with_max_bombs = []
for col in range(H):
    if num_bombs_each_col[col] == max_bombs_in_col:
        cols_with_max_bombs.append(col)

found = False
for row in rows_with_max_bombs:
    for col in cols_with_max_bombs:
        if not (row, col) in bombs:
            found = True
            break

ans = max_bombs_in_row + max_bombs_in_col
if found:
    print(ans)
else:
    print(ans - 1)

Submission status: Runtime Error

Solution code:
H,W,M = map(int, input().split())
grid = [[0 for _ in range(W)] for _ in range(H)]

hc = [(0, h) for h in range(H)]
wc = [(0, w) for w in range(W)]

for _ in range(M):
    h,w = map(int, input().split())
    grid[h-1][w-1] = 1
    hc[h-1] = (hc[h-1][0] + 1, hc[h-1][1])
    wc[w-1] = (wc[w-1][0] + 1, wc[w-1][1])

hc = sorted(hc, key=lambda p: p[0], reverse=True)
wc = sorted(wc, key=lambda p: p[0], reverse=True)

hmax = hc[0][0]
wmax = wc[0][0]

uh = 0
while hc[uh][0] == hmax:
    uh += 1

uw = 0
while wc[uw][0] == wmax:
    uw += 1

uh -= 1
uw -= 1
#print(grid)
#print(wc, uw)
#print(hc, uh)

found = False
for h in range(uh):
    for w in range(uw):

        if grid[hc[h][1]][wc[w][1]] == 0:
            found = True
            break
    if found:
        break

print(hmax + wmax if found else hmax + wmax - 1)

Submission status: Runtime Error

Solution code:
H,W,M = map(int, input().split())
grid = [[0 for _ in range(W)] for _ in range(H)]

hc = [(0, h) for h in range(H)]
wc = [(0, w) for w in range(W)]

for _ in range(M):
    h,w = map(int, input().split())
    grid[h-1][w-1] = 1
    hc[h-1] = (hc[h-1][0] + 1, hc[h-1][1])
    wc[w-1] = (wc[w-1][0] + 1, wc[w-1][1])

hc = sorted(hc, key=lambda p: p[0], reverse=True)
wc = sorted(wc, key=lambda p: p[0], reverse=True)

hmax = hc[0][0]
wmax = wc[0][0]

uh = 0
while hc[uh][0] == hmax:
    uh += 1

uw = 0
while hc[uw][0] == wmax:
    uw += 1

uh -= 1
uw -= 1
#print(grid)
#print(wc, uw)
#print(hc, uh)

found = False
for h in range(uh):
    for w in range(uw):

        if grid[hc[h][1]][wc[w][1]] == 0:
            found = True
            break
    if found:
        break

print(hmax + wmax if found else hmax + wmax - 1)

Submission status: Runtime Error

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

countW = {0:0}
countH = {0:0}
bomb = [[0]*(W+1) for _ in range(H+1)]
for i in range(M):
    h, w = map(int, input().split())
    bomb[h][w] += 1
    if h in countH.keys():
        countH[h] += 1
    else:
        countH[h] = 1
    if w in countW.keys():
        countW[w] += 1
    else:
        countW[w] = 1

sortedW = sorted(countW.items(), key=lambda x:x[1])
sortedH = sorted(countH.items(), key=lambda x:x[1])

ans = 0
szH = len(sortedH)
szW = len(sortedW)
found = False
for h in range(szH-1, 0, -1):
    if found: break
    hh, bh = sortedH[h]
    for w in range(szW-1, 0, -1):
        ww, bw = sortedW[w]
        nval = bh + bw - bomb[hh][ww]
        if ans < nval: ans = nval
        elif ans > nval:
            found = True
            break

print(ans)

Submission status: Runtime Error

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

r_n = [0]*h
c_n = [0]*w
bomb_table = set()
for i in range(m):
	hi, wi = map(int, input().split())
	hi-=1
	wi-=1
	r_n[hi] += 1
	c_n[wi] += 1
	bomb_table.add((hi, wi))

max_x = max(c_n)
max_y = max(r_n)
ans = max_x + max_y

for i in range(h):
	if r_n[i] != max_y:
		continue
	for j in range(w):
		if c_n[j] != max_x:
			continue
		if (i,j) not in bomb_table:
			print(ans)
			exit()
			
print(ans - 1)

Submission status: Runtime Error

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

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

row = [0]*w
col = [0]*h
maze = [[0 for _ in range(w)] for _ in range(h)]

for i in range(m):
    hh, ww = map(lambda x:int(x)-1,input().split())
    row[hh] += 1
    col[ww] += 1
    maze[hh][ww] = 1

rmax = max(row)
cmax = max(col)
 
rind = [n for n, v in enumerate(row) if v == rmax]
cind = [n for n, v in enumerate(col) if v == cmax]
 
#print(rind)
#print(cind)
ans = 0
 
for r in rind:
    for c in cind:
        if maze[r][c] == 1:
            ans = rmax+cmax-1
        else:
            ans = rmax+cmax
            print(ans)
            exit()
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
#======================================================#
def main():
    h, w, m = MII()
    bombs = [MIIZ() for _ in range(m)]
    a = [0]*h
    b = [0]*w
    for y,x in bombs:
        a[y] += 1
        b[x] += 1
    maxa = max(a)
    maxb = max(b)
    sumv = maxa + maxb
    c = a.count(maxa)*b.count(maxb) - sum(a[y]+b[y] == sumv for y,x in bombs)
    print(sumv - (c<=0))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
#======================================================#
def main():
    h, w, m = MII()
    bombs = [MIIZ() for _ in range(m)]
    area = np.zeros((h,w))
    for x, y in bombs:
        area[x:x+1, :] += 1
        area[:, y:y+1] += 1
        area[x:x+1, y:y+1] -= 1
    print(int(area.max()))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
#======================================================#
def main():
    h, w, m = MII()
    bombs = [MIIZ() for _ in range(m)]
    area = np.zeros((h,w), dtype=int)
    # print(area)
    for x, y in bombs:
        area[x:x+1, :] += 1
        area[:, y:y+1] += 1
        area[x:x+1, y:y+1] -= 1
        # print(area)
    print(area.max())

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import defaultdict
h,w,m = map(int,input().split())
dh = defaultdict(int)
dw = defaultdict(int)
for _ in range(m):
    x,y = map(int,input().split())
    dh[x]+=1
    dw[y]+=1
    l.append((x,y))
m=0
hi=0
for i in dh:
    if dh[i]>m:
        m=dh[i]
        hi=i
c=m
m=0
for i in dw:
    if dw[i]>m:
        m=dw[i]
        if (hi,i) in l:
            m-=1
c+=m
print(c)

Submission status: Runtime Error

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

X=[]
Y=[]
tt=[]
for i in range(M):
    inp=list(map(int,input().split()))
    X.append(inp[0])
    Y.append(inp[1])
    tt.append((inp[0],inp[1]))
Z1=Counter(X)
Z2=Counter(Y)
maxmax=0
for i in Z1.keys():
    for j in Z2.keys():
        aa=0
        if (i,j) in tt:
            aa=-1
        if maxmax<(aa+Z1[i]+Z2[j]):
            maxmax=(aa+Z1[i]+Z2[j])
print(maxmax)

Submission status: Runtime Error

Solution code:
import collections

h, w, m = map(int, input().split())
x = []
y = []
bomb = []
for i in range(m):
  tmp_x, tmp_y = map(int, input().split())
  x.append(tmp_x)
  y.append(tmp_y)
  bomb.append([tmp_x, tmp_y])

c_x = collections.Counter(x)
c_y = collections.Counter(y)
_max_x = c_x.most_common()[0][1]
_max_y = c_y.most_common()[0][1]
ys = c_y.most_common()
count = 0
for i in bomb:
  if x[i[0]] == _max_x and y[i[1]] == _max_y:
    count += 1
    
if count == x.count(_max_x) * y.count(_max_y):
  print(_max_x + _max_y - 1)
else:
  print(_max_x + _max_y)

Submission status: Runtime Error

Solution code:
import numpy as np
# input h, w, m


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

bomb_list = []

for i in range(m):
    bomb_list.append(list(map(int, input().split())))

grid = [[0] * w for i in range(h)]

# confirmation
#print(grid)
#print(bomb_list)

#i個目の爆弾を処理する
for i in range(m):
    grid[bomb_list[i][0]-1][bomb_list[i][1]-1] = 1

#爆弾の位置をプロット
#print(grid)



row_grid = np.array(grid)

#print(row_grid)

column_grid = np.rot90(row_grid)




result_list = []


#print("デバック")
#Column方向で精査する
for height in range(h):
    for width in range(w):
        count = 0
        #print(count)
        #print('これから精査していくよ')

        if row_grid[height][width] == 0:

            #print(np.count_nonzero(row_grid[height] == 1))
            count += np.count_nonzero(row_grid[height] == 1)

            #print(np.count_nonzero(column_grid[width] == 1))
            count += np.count_nonzero(column_grid[width] == 1)

            result_list.append(count)


#print(result_list)

print(max(result_list))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
import copy
sys.setrecursionlimit(10**6)

# lis_of_lis = [[] for _ in range(N)]


def zz(): return list(map(int, sys.stdin.readline().split()))


def z(): return int(sys.stdin.readline())


def S(): return sys.stdin.readline()[:-1]


def C(line): return [sys.stdin.readline() for _ in range(line)]


H, W, M = zz()
target_h = [0]*M
target_w = [0] * M
target_table = [[0]*W for _ in range(H)]
for i in range(M):
    h, w = zz()
    target_h[i] = h-1
    target_w[i] = w-1
    target_table[h-1][w-1] = 1

count_h = collections.Counter(target_h)
count_w = collections.Counter(target_w)

pos_h, num_max_h = count_h.most_common()[0]
pos_w, num_max_w = count_w.most_common()[0]
ans = num_max_h + num_max_w
muinus_one_flg = True
for pos_h, num_h in count_h.most_common():
    if (num_h != num_max_h):
        break
    for pos_w, num_w in count_w.most_common():
        if (num_w != num_max_w):
            break
        if (target_table[pos_h][pos_w] == 0):
            muinus_one_flg = False
if (muinus_one_flg):
    ans -= 1
print(ans)

# print('pos_h', 'pos_w', pos_h, pos_w)
# print('num_h', 'num_w', num_h, num_w)
# for pos_h, pos_w in zip(target_h, target_w):
#     if (count_h[pos_h] == num_h and count_w[pos_w] == num_w):

Submission status: Runtime Error

Solution code:
from numba import njit

@njit
def main():
    import collections
 
    H, W, M_ko = map(int, input().split())
 
    zahyou = [[0 for i in range(W)] for j in range(H)]
    zahyou_H = [0 for i in range(H)]
    zahyou_W = [0 for i in range(W)]
 
    for i in range(M_ko):
        height, width = map(int, input().split())
 
        zahyou[height - 1][width - 1] = 1
        
        zahyou_H[height - 1] += 1
        zahyou_W[width - 1] += 1
 
    CTR_H = collections.Counter(zahyou_H)
    CTR_W = collections.Counter(zahyou_W)
 
    max_h = max(zahyou_H)
    max_w = max(zahyou_W)
 
    len_h = CTR_H[max_h]
    len_w = CTR_W[max_w]
 
    index_h = [i for i, x in enumerate(zahyou_H) if x == max_h]
    index_w = [i for i, x in enumerate(zahyou_W) if x == max_w]
 
 
    maxi = max_h + max_w
    FLAG = False
 
 
    if (len_h == 1 and len_w == 1):    
        maxi -= zahyou[index_h[0]][index_w[0]]
 
 
    else:
        for i in range(len_h):        
            for j in range(len_w):
                val = zahyou[index_h[i]][index_w[j]]
                
                if (val == 0):
                    FLAG = True
                    break
        
        if (not FLAG):
            maxi -= 1
 
    print(maxi)
 
 
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
from collections import deque,Counter,defaultdict

H,W,M = map(int , input().split())
alls = defaultdict(set)
ths = deque()
tws = deque()

for _ in range(M):
  h,w = map(int,input().split())
  ths.append(h)
  tws.append(w)
  alls[h].add(w)


mh = Counter(ths).most_common()
mw = Counter(tws).most_common()

tghq = deque()
tgwq = []

tghq.append(mh[0])
tgwq.append(mw[0])

i=1
while mh[0][1] == mh[i][1]:
  tghq.append(mh[i])
  i+=1
  if i >= len(tghq):
    break

i=1
while mw[0][1] == mw[i][1]:
  tgwq.append(mw[i])
  i+=1
  if i >= len(tgwq):
    break

sums = mw[0][1] + mh[0][1] - 1
breaker = True
while tghq and breaker:
  tgh,mxh = tghq.pop()
  for tw in tgwq:
    if tw[0] not in alls[tgh]:
      sums +=1
      breaker = False
      break

print(sums)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
from collections import deque,Counter,defaultdict

H,W,M = map(int , input().split())
alls = defaultdict(set)
ths = deque()
tws = deque()

for _ in range(M):
  h,w = map(int,input().split())
  ths.append(h)
  tws.append(w)
  alls[h].add(w)


mh = Counter(ths).most_common()
mw = Counter(tws).most_common()

tghq = deque()
tgwq = []

tghq.append(mh[0])
tgwq.append(mw[0])

i=1
while mh[0][1] == mh[i][1]:
  tghq.append(mh[i])
  i+=1
  if i >= len(tghq):
    break

i=1
while mw[0][1] == mw[i][1]:
  tgwq.append(mw[i])
  i+=1
  if i >= len(tghq):
    break

sums = mw[0][1] + mh[0][1] - 1
breaker = True
while tghq and breaker:
  tgh,mxh = tghq.pop()
  for tw in tgwq:
    if tw[0] not in alls[tgh]:
      sums +=1
      breaker = False
      break

print(sums)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
from collections import deque,Counter,defaultdict

H,W,M = map(int , input().split())
alls = defaultdict(set)
ths = deque()
tws = deque()
for _ in range(M):
  h,w = map(int,input().split())
  ths.append(h)
  tws.append(w)
  alls[h].add(w)


mh = Counter(ths).most_common()
mw = Counter(tws).most_common()

i=1
tghq = deque()
tghq.append(mh[0])
tgwq = []
tgwq.append(mw[0])

while mh[0][1] == mh[i][1]:
  tghq.append(mh[i])
  i+=1
  if i >= len(tghq):
    break
i=1

while mw[0][1] == mw[i][1]:
  tgwq.append(mw[i])
  i+=1
  if i >= len(tghq):
    break
ad = 0
sums = mw[0][1] + mh[0][1] - 1
while tghq:
  tgh,mxh = tghq.pop()
  for tw in tgwq:
    if tw[0] not in alls[tgh]:
      sums = mxh + tw[1]
      break

print(sums)

Submission status: Runtime Error

Solution code:
h, w, m = map(int, input().split())
bomb = []
h_bomb = []
w_bomb = []
for i in range(m):
    bomb.append(list(map(int, input().split())))
    h_bomb.append(0)
    w_bomb.append(0)
for j in range(m):
    h_bomb[bomb[j][0]] += 1
    w_bomb[bomb[j][1]] += 1
print(max(h_bomb) + max(w_bomb))

Submission status: Runtime Error

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

hs = [0] * H
ws = [0] * W
checked = set()

for i in range(M):
    r,c = map(int, input().split())
    r -= 1
    c -= 1

    hs[r] += 1
    ws[c] += 1
    checked.add((r, c))

mh, mw = 0, 0
for i in range(H):
    mh = max(mh, hs[i])
    mw = max(mw, ws[i])

max_h_list = []
max_w_list = []
    
for i in range(H):
    if hs[i] == mh: max_h_list.append(i)    
for i in range(W):
    if ws[i] == mw: max_w_list.append(i)

ans = mh + mw

for i in max_h_list:
    for j in max_w_list:
        if (i,j) not in checked:
            print(ans)
            exit()
print(ans-1)