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:
import numpy as np

h,w,m = map(int, input().split())
column = [0 for i in range(h+1)]
row = [0 for i in range(w+1)]
column_max = 0
row_max = 0
column_max_index = []
row_max_index = []
matrix = np.zeros((h+1,w+1))

for i in range(m):
  a,b = map(int, input().split())
  column[a] += 1
  row[b] += 1
  if column[a] == column_max:
    column_max_index.append(a)
    
  if column[a] > column_max:
    column_max = column[a]
    column_max_index = [a]
  
  if row[b] == row_max:
    row_max_index.append(b)
    
  if row[b] > row_max:
    row_max = row[b]
    row_max_index = [b]
  matrix[a][b] = 1
  
count = 0
for i in column_max_index:
  for j in row_max_index:
    if matrix[i][j] == 0:
      count += 1
      break
if count != 0:
  print(column_max + row_max)
else:
  print(column_max + row_max - 1)

Submission status: Runtime Error

Solution code:
import numpy as np

h,w,m = map(int, input().split())
column = [0 for i in range(h+1)]
row = [0 for i in range(w+1)]
column_max = 0
row_max = 0
column_max_index = []
row_max_index = []
matrix = np.zeros((w+1,h+1), dtype = int)

for i in range(m):
  a,b = map(int, input().split())
  column[a] += 1
  row[b] += 1
  if column[a] == column_max:
    column_max_index.append(a)
    
  if column[a] > column_max:
    column_max = column[a]
    column_max_index = [a]
  
  if row[b] == row_max:
    row_max_index.append(b)
    
  if row[b] > row_max:
    row_max = row[b]
    row_max_index = [b]
  matrix[a][b] = 1
  
count = 0
for i in column_max_index:
  for j in row_max_index:
    if matrix[i][j] == 0:
      count += 1
      break
if count != 0:
  print(column_max + row_max)
else:
  print(column_max + row_max - 1)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np

h,w,m = map(int, input().split())
column = [0 for i in range(h+1)]
row = [0 for i in range(w+1)]
column_max = 0
row_max = 0
column_max_index = []
row_max_index = []
matrix = np.zeros((h+1,w+1), dtype = int)

for i in range(m):
  a,b = map(int, input().split())
  column[a] += 1
  row[b] += 1
  if column[a] == column_max:
    column_max_index.append(a)
    
  if column[a] > column_max:
    column_max = column[a]
    column_max_index = [a]
  
  if row[b] == row_max:
    row_max_index.append(b)
    
  if row[b] > row_max:
    row_max = row[b]
    row_max_index = [b]
  matrix[a][b] = 1
  
count = 0
for i in column_max_index:
  for j in row_max_index:
    if matrix[i][j] == 0:
      count += 1
      break
if count != 0:
  print(column_max + row_max)
else:
  print(column_max + row_max - 1)

Submission status: Runtime Error

Solution code:
import numpy as np
h, w, m = map(int, input().split())
arr = np.zeros([h,w])
for _ in range(m):
    c_h, c_w = map(int, input().split())
    arr[c_h-1, c_w-1] = 1

Submission status: Runtime Error

Solution code:
import numpy as np
h, w, m = map(int, input().split())
arr = np.zeros([h,w])
for _ in range(m):
    c_h, c_w = map(int, input().split())
    arr[c_h-1, c_w-1] = 1
    
row_sum = np.sum(arr, axis=1)
col_sum = np.sum(arr, axis=0)
row_max_pos = np.argwhere(row_sum == np.max(row_sum))
col_max_pos = np.argwhere(col_sum == np.max(col_sum))

Submission status: Runtime Error

Solution code:
import numpy as np
h, w, m = map(int, input().split())
arr = np.zeros([h,w])
for _ in range(m):
    c_h, c_w = map(int, input().split())
    arr[c_h-1, c_w-1] = 1
    
row_sum = np.sum(arr, axis=1)
col_sum = np.sum(arr, axis=0)
row_max_pos = np.argwhere(row_sum == np.max(row_sum))
col_max_pos = np.argwhere(col_sum == np.max(col_sum))
    
row_max = int(max(row_sum))
col_max = int(max(col_sum))
ans = row_max + col_max

Submission status: Runtime Error

Solution code:
import numpy as np

h,w,m = map(int, input().split())
hw = [list(map(int, input().split())) for i in range(m)]
map = np.zeros((h, w))

for i in hw:
  map[i[0]-1, i[1]-1] = 1

hcount = np.count_nonzero(map=1, axis=0)
wcount = np.count_nonzero(map=1, axis=1)

hmax = max(hcount)
wmax = max(wcount)

hindex = hcount.index(hmax)
windex = wcount.index(wmax)

cnt = 0

if [hindex, windex] in hw:
  print(hmax + wmax - 1)

else:
  print(hmax + wmax)

Submission status: Runtime Error

Solution code:
import numpy as np
h, w, m = map(int, input().split())
arr = np.zeros([h,w])
for _ in range(m):
    c_h, c_w = map(int, input().split())
    arr[c_h-1, c_w-1] = 1
    
row_sum = np.sum(arr, axis=1)
col_sum = np.sum(arr, axis=0)
row_max_pos = np.argwhere(row_sum == np.max(row_sum))
col_max_pos = np.argwhere(col_sum == np.max(col_sum))
    
row_max = int(max(row_sum))
col_max = int(max(col_sum))
ans = row_max + col_max

if sum(sum(arr[row_max_pos, col_max_pos] == 0)) > 0:
    print(ans)
else:
    print(ans-1)

Submission status: Runtime Error

Solution code:
h,w,m=map(int,input().split())
bomb=[]
x=[0]*(h+1)
y=[0]*(w+1)
for i in range(m):
    h1,w1=map(int,input().split())
    x[h1]+=1
    y[w1]+=1
    bomb.append((h1,w1))
maxx=max(x)
maxy=max(y)
r=[]
c=[]
for i in range(1,h+1):
    if x[i]==maxx:
        r.append(i)
for j in range(1,w+1):
    if y[i]==maxy:
        c.append(i)
for j in r:
    for k in c:
        if (j,k) not in bomb:
            print(maxx+maxy)
            exit()
print(maxx+maxy-1)

Submission status: Runtime Error

Solution code:
h,w,m=map(int,input().split())
bomb=[]
x=[0]*(h+1)
y=[0]*(w+1)
for i in range(m):
    h1,w1=map(int,input().split())
    x[h1]+=1
    y[w1]+=1
    bomb.append((h1,w1))
maxx=max(x)
maxy=max(y)
r=[]
c=[]
for i in range(1,h+1):
    if x[i]==maxx:
        r.append(i)
for j in range(1,w+1):
    if y[i]==maxy:
        c.append(i)
for j in r:
    for k in c:
        if not (j,k) in bomb:
            print(maxx+maxy)
            exit()
print(maxx+maxy-1)

Submission status: Runtime Error

Solution code:
h,w,m=map(int,input().split())
g=[0]*h
l=[0]*w
b=[[0]*w for i in range(h)]
for i in range(m):
  x,y=map(int,input().split())
  g[x-1]+=1
  l[y-1]+=1
  b[x-1][y-1]=1
mg=[i for i,x in enumerate(g) if x==max(g)]
ml=[i for i,x in enumerate(l) if x==max(l)]
for i in mg:
  for j in ml:
    if b[i][j]==0:
      print(max(g)+max(l))
      exit()
print(max(g)+max(l)-1)

Submission status: Runtime Error

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

max_value_h = max(h_list)
max_index_h = [i for i, x in enumerate(h_list) if x == max_value_h]

max_value_w = max(w_list)
max_index_w = [i for i, x in enumerate(w_list) if x == max_value_w]

ans = 0
for i in max_index_h:
    for j in max_index_w:
        if FIELD[i][j] == 1:
            ans = max(ans, h_list[i] + w_list[j] - 1)
        else:
            ans = max(ans, h_list[i] + w_list[j])
print(ans)

Submission status: Runtime Error

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

max_value_h = max(h_list)
max_index_h = [i for i, x in enumerate(h_list) if x == max_value_h]

max_value_w = max(w_list)
max_index_w = [i for i, x in enumerate(w_list) if x == max_value_w]

ans = max_value_h + max_value_w
for i in max_index_h:
    for j in max_index_w:
        if FIELD[i][j] == 1:
            ans = max(ans, h_list[i]+w_list[j]-1)
        else:
            ans = max(ans, h_list[i]+w_list[j])
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
from numba import njit, b1, i4, i8, f8

@njit((i8, i8, i8[:]), cache=True)
def main():
    H, W, M = map(int, input().split())
    row = np.zeros(W)
    col = np.zeros(H)
    memo = []
    for _ in range(M):
        h, w = map(int, input().split())
        h -=1
        w -=1
        row[w] += 1
        col[h] += 1
        memo.append((h, w))

    col_max = col.max()
    row_max = row.max()

    max_col_indexs = np.where(col == col_max)[0]
    max_row_indexs = np.where(row == row_max)[0]

    ans = col_max + row_max -1
    for c in max_col_indexs:
        for r in max_row_indexs:
            if (c, r) not in memo:
                ans = ans = col_max + row_max
                break

    print(int(ans))

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
from statistics import mode

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

hlist=[] #爆破対象の行のリスト
wlist=[] #爆破対象の列のリスト

for i in range(m):
    hlist.append(hw[i][0])
    wlist.append(hw[i][1])

hmost=(mode(hlist)) #hlistの中で一番出現数の多い要素
wmost=(mode(wlist)) #wlistの中で一番出現数の多い要素

hnum=(hlist.count(hmost)) #hlistの中で一番出現数の多い要素の出現数
wnum=(wlist.count(wmost)) #wlistの中で一番出現数の多い要素の出現数

cnt=0
if hnum==wnum: #hlistとwlistの中の最多出現要素の出現数が同じ場合
    h2list=[i for i in hlist if i!=hmost] #hlistから最多出現要素を削除
    w2list=[i for i in wlist if i!=wmost] #wlistから最多出現要素を削除
    h2most=(mode(h2list)) #hlist2の中で一番出現数の多い要素の出現数
    w2most=(mode(w2list)) #wlist2の中で一番出現数の多い要素の出現数
    h2num=(h2list.count(h2most)) #hlist2の中で一番出現数の多い要素の出現数
    w2num=(w2list.count(w2most)) #wlist2の中で一番出現数の多い要素の出現数
    if h2num>=w2num:
        cnt+=wnum #wlistの中で一番出現数の多い要素の出現数
        wlist_idx=[i for i, x in enumerate(wlist) if x==wmost] #wlistの中で一番出現数の多い要素のindex
        for i in wlist_idx[::-1]:
            hlist.pop(i) #hlistからwlistで最多出現の要素と対応する要素を削除
        hmost=(mode(hlist)) #hlistの中で一番出現数の多い要素
        hnum=(hlist.count(hmost)) #hlistの中で一番出現数の多い要素の出現数
        cnt+=hnum
        print(cnt)
        sys.exit()
    if w2num>h2num:
        cnt+=hnum #hlistの中で一番出現数の多い要素の出現数
        hlist_idx=[i for i, x in enumerate(hlist) if x==hmost] #hlistの中で一番出現数の多い要素のindex
        for i in hlist_idx[::-1]:
            wlist.pop(i) #wlistからhlistで最多出現の要素と対応する要素を削除
        wmost=(mode(wlist)) #wlistの中で一番出現数の多い要素
        wnum=(wlist.count(wmost)) #wlistの中で一番出現数の多い要素の出現数
        cnt+=wnum #hlistの最多出現要素と対応する要素を削減した後のwlistで最多出現要素の出現数をcntに追加
        print(cnt)
        sys.exit()

if hnum>wnum:
    cnt+=wnum #wlistの中で一番出現数の多い要素の出現数
    wlist_idx=[i for i, x in enumerate(wlist) if x==wmost] #wlistの中で一番出現数の多い要素のindex
    for i in wlist_idx[::-1]:
        hlist.pop(i) #hlistからwlistで最多出現の要素と対応する要素を削除
    hmost=(mode(hlist)) #hlistの中で一番出現数の多い要素
    hnum=(hlist.count(hmost)) #hlistの中で一番出現数の多い要素の出現数
    cnt+=hnum #wlistの最多出現要素と対応する要素を削減した後のhlistで最多出現要素の出現数をcntに追加
    print(cnt)
    sys.exit()

if wnum>hnum:
    cnt+=hnum #hlistの中で一番出現数の多い要素の出現数をcntに追加
    hlist_idx=[i for i, x in enumerate(hlist) if x==hmost] #hlistの中で一番出現数の多い要素のindex
    for i in hlist_idx[::-1]:
        wlist.pop(i) #wlistからhlistで最多出現の要素と対応する要素を削除
    wmost=(mode(wlist)) #wlistの中で一番出現数の多い要素
    wnum=(wlist.count(wmost)) #wlistの中で一番出現数の多い要素の出現数
    cnt+=wnum  #hlistの最多出現要素と対応する要素を削減した後のwlistで最多出現要素の出現数をcntに追加
    print(cnt)
    sys.exit()

Submission status: Runtime Error

Solution code:
h,w,m = (int(x) for x in input().split())

index = [0]*h*w

h_total = []
w_total = []
chk_sum = []

for i in range(m):
    l = input()
    a = l.split(' ')

    index[(int(a[1])-1)*h+int(a[0])-1] = 1

for i in range(h):
    temp = 0
    for j in range(w):
        temp += index[h*j+i]
    h_total.append(temp)

for i in range(w):
    temp = 0
    for j in range(h):
        temp += index[h*i+j]
    w_total.append(temp)

h_max = [i for i, x in enumerate(h_total) if x == max(h_total)]
w_max = [i for i, x in enumerate(w_total) if x == max(w_total)]

for i in range(len(w_max)):
    for j in range(len(h_max)):
        chk_sum.append(h_total[h_max[j]] + w_total[w_max[i]] - index[h_max[j]*h + w_max[i]])

print(max(chk_sum))

Submission status: Runtime Error

Solution code:
H,W,M = map(int, input().split())
bombmat = [[0]*(W+1) for _ in range(H+1)]
bomb_row = [0]*M; bomb_col = [0]*M
for i in range(M):
    h,w = map(lambda x: int(x)-1, input().split())
    bombmat[h][w] = 1
    bombmat[H][w] += 1
    bombmat[h][W] += 1
    
    bomb_row[i] = h
    bomb_col[i] = w

row_H = bombmat[H]
maxcol = 0
for col in range(W):
    maxcol = max(maxcol, row_H[col])

maxcollist = [False]*W
for col in range(W):
    if row_H[col] == maxcol:
        maxcollist[col] = True

maxrow = 0
for row in range(H):
    maxrow = max(maxrow, bombmat[row][W])

maxrowlist = [False]*H
for row in range(H):
    if bombmat[row][W] == maxrow:
        maxrowlist[row] = True

#最高行または最高列上にあり、
#かつ最高行と最高列の交点にない爆弾があるかをチェック
cross_cnt = sum(maxrowlist) * sum(maxcollist)
for i in range(M):
    h,w = bomb_row[i], bomb_col[i]
    if maxrowlist[h] and maxcollist[w]:
        cross_cnt -= 1

if cross_cnt > 0:
    print(maxcol + maxrow)
else:
    print(maxcol + maxrow - 1)

Submission status: Runtime Error

Solution code:
H,W,M = map(int, input().split())
bombmat = [[0]*(W+1) for _ in range(H+1)]
bomb_row = [0]*M; bomb_col = [0]*M
for i in range(M):
    h,w = map(lambda x: int(x)-1, input().split())
    bombmat[h][w] = 1
    bombmat[H][w] += 1
    bombmat[h][W] += 1
    
    bomb_row[i] = h
    bomb_col[i] = w

row_H = bombmat[H]
maxcol = 0
for col in range(W):
    maxcol = max(maxcol, row_H[col])

maxcollist = [False]*W
for col in range(W):
    if row_H[col] == maxcol:
        maxcollist[col] = True

maxrow = 0
for row in range(H):
    maxrow = max(maxrow, bombmat[row][W])

maxrowlist = [False]*H
for row in range(H):
    if bombmat[row][W] == maxrow:
        maxrowlist[row] = True

#最高行または最高列上にあり、
#かつ最高行と最高列の交点にない爆弾があるかをチェック
cross_cnt = sum(maxrowlist) * sum(maxcollist)
for i in range(M):
    h,w = bomb_row[i], bomb_col[w]
    if maxrowlist[h] and maxcollist[w]:
        cross_cnt -= 1

if cross_cnt > 0:
    print(maxcol + maxrow)
else:
    print(maxcol + maxrow - 1)

Submission status: Runtime Error

Solution code:
h,w,m = (int(x) for x in input().split())

index = [0]*h*w

h_total = []
w_total = []
chk_sum = []

for i in range(m):
    l = input()
    a = l.split(' ')

    index[(int(a[1])-1)*h+int(a[0])-1] = 1

for i in range(h):
    temp = 0
    for j in range(w):
        temp += index[h*j+i]
    h_total.append(temp)

for i in range(w):
    temp = 0
    for j in range(h):
        temp += index[h*i+j]
    w_total.append(temp)

#print(max(h_total))
#print(max(w_total))

if(max(h_total) >= max(w_total)):
    key = h_total.index(max(h_total))
#    print(key)
    temp = 0
    for i in range(w):
        chk_sum.append(h_total[key] + w_total[i] - index[h*i+key])
    print(max(chk_sum))

else:
    key = w_total.index(max(w_total))
#    print(key)
    temp = 0
    for i in range(h):
        chk_sum.append(w_total[key] + h_total[i] - index[w*i+key])
    print(max(chk_sum))

Submission status: Runtime Error

Solution code:
from collections import defaultdict
H,W,M = list(map(int,input().split()))

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

hw = defaultdict(set)

for _ in range(H):
    hw.append([0] * W)

for _ in range(M):
    a,b = list(map(int,input().split()))
    h[a - 1] += 1
    w[b - 1] += 1
    hw[a - 1].add(b - 1)
    
hm = max(h)
wm = max(w)

hp = [i for i, v in enumerate(h) if v == hm]
wp = [i for i, v in enumerate(w) if v == wm]

for x in hp:
    for y in wp:
        if y in hw[x]:
            print(hm + wm)
            exit()
else:
    print(hm + wm - 1)

Submission status: Runtime Error

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def MAP1()  : return map(lambda x:int(x)-1,input().split())
def LIST()  : return list(MAP())
def LIST1() : return list(MAP1())

def solve():
    h, w, m = MAP()
    s = [LIST1() for i in range(m)]
    ay = [0]*h
    ax = [0]*w
    c = [[0]*w for i in range(h)]
    for x in s:
        ay[x[0]] += 1
        ax[x[1]] += 1
        c[x[0]][x[1]] = 1

    my = max(ay)
    mx = max(ax)
    
    by = bx = 0
    for y in ay:
        if y == my:
            by += 1
    for x in ax:
        if x == mx:
            bx += 1
            
    ans = my + mx
    tmp = 0
    for x in s:
        if ay[x[0]]+ax[x[1]]==ans:
            tmp += c[x[0]][x[1]]
    if tmp == by*bx:
        ans -= 1
    print(ans)
    
if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys 
import itertools
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
H, W, M = map(int,readline().split())
h_cnt = [0]*(H+1)
w_cnt = [0]*(W+1)
data = [[False]*(W+1) for i in range(H+1)]
 
for i in range(M):
    h,w = map(int,readline().split())
    data[h][w] = True
    h_cnt[h] += 1 
    w_cnt[w] += 1
    
maxh = max(h_cnt)
maxw = max(w_cnt)
maxhInd = []
maxwInd = []
maxhInd = [i for i,a in enumerate(h_cnt) if a == maxh]
maxwInd = [i for i,a in enumerate(w_cnt) if a == maxw]
maxhCnt = len(maxhInd)
maxwCnt = len(maxwInd)

if max_w_cnt * max_h_cnt > M:
    print(max_h+max_w)
else:
    t = list(itertools.product(max_h_ind, max_w_ind))
    for h,w in t:
        if data[h][w] == False:
            print(max_h+max_w)
            sys.exit()
    print(max_h+max_w-1)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys 
import itertools
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
H, W, M= map(int,readline().split())
h_cnt = [0]*(H+1)
w_cnt = [0]*(W+1)
data = [[False]*(W+1) for i in range(H+1)]
 
for i in range(M):
    h,w = map(int,readline().split())
    data[h][w] = True
    h_cnt[h] += 1 
    w_cnt[w] += 1
    
max_h = max(h_cnt)
max_w = max(w_cnt)
 
max_h_ind = []
max_w_ind = []
max_h_cnt = 0
max_w_cnt = 0
 
for i in range(H+1):
    if max_h == h_cnt[i]:
        max_h_cnt += 1
        max_h_ind.append(i)
        
for i in range(W+1):
    if max_w == w_cnt[i]:
        max_w_cnt += 1
        max_w_ind.append(i)
max_h_ind = set(max_h_ind)
max_w_ind = set(max_w_ind)
 
if max_w_cnt * max_h_cnt > M:
    print(max_h+max_w)
else:
    t = list(itertools.product(max_h_ind, max_w_ind))
    for h,w in t:
        if data[h][w] == False:
            print(max_h+max_w)
            sys.exit()
    print(max_h+max_w-1)

Submission status: Runtime Error

Solution code:
import sys
HK,WK,M = map(int, input().split())
W = [0] * WK + 1
H = [0] * HK + 1
HW = [[0 for i in range(WK+1)] for _ in range(HK+1)]
# 3*10^5

for i in range(M):
    h, w = map(int, input().split())

    H[h] += 1
    W[w] += 1

    HW[h][w] = 1

answer = 0
HMAX = max(H)
WMAX = max(W)

HEMAX = H.count(HMAX)
WEMAX = W.count(WMAX)
if HEMAX * WEMAX > M:
    print(HMAX + WMAX)
    sys.exit(0)

HMAXES = [i for i, _ in enumerate(H) if _ == HMAX]
WMAXES = [i for i, _ in enumerate(W) if _ == WMAX]

# 3*10^5
answer = HMAX + WMAX
for h in HMAXES:
    for w in WMAXES:
        if HW[h][w] == 0:
            print(answer)
            sys.exit(0)
print(answer-1)

Submission status: Runtime Error

Solution code:
import sys
HK,WK,M = map(int, input().split())
W = [0] * WK
H = [0] * HK
HW = [[0 for i in range(WK+1)] for _ in range(HK+1)]
# 3*10^5

for i in range(M):
    h, w = map(int, input().split())

    H[h] += 1
    W[w] += 1

    HW[h][w] = 1

answer = 0
HMAX = max(H)
WMAX = max(W)

HEMAX = H.count(HMAX)
WEMAX = W.count(WMAX)
if HEMAX * WEMAX > M:
    print(HMAX + WMAX)
    sys.exit(0)

HMAXES = [i for i, _ in enumerate(H) if _ == HMAX]
WMAXES = [i for i, _ in enumerate(W) if _ == WMAX]

# 3*10^5
answer = HMAX + WMAX
for h in HMAXES:
    for w in WMAXES:
        if HW[h][w] == 0:
            print(answer)
            sys.exit(0)
print(answer-1)

Submission status: Runtime Error

Solution code:
import sys
HK,WK,M = map(int, input().split())
hw_s = [list(map(int, input().split())) for _ in range(M)]
W = [0 for _ in range(HK+1)]
H = [0 for _ in range(WK+1)]
HW = [[0 for i in range(WK+1)] for _ in range(HK+1)]
# 3*10^5

for i, hw in enumerate(hw_s):
    h=hw[0]
    w=hw[1]

    H[h] += 1
    W[w] += 1

    HW[h][w] = 1

HMAXES = []
WMAXES = []
answer = 0
HMAX = max(H)
WMAX = max(W)


# 3*10^5
for i, h in enumerate(H):
    if h == HMAX:
        HMAXES.append(i)
for i, w in enumerate(W):
    if w == WMAX:
        HMAXES.append(i)
# 3*10^5
answer = HMAX + WMAX
for i, h in enumerate(HMAXES):
    for j, w in enumerate(WMAXES):
        if HW[i][j] == 0:
            print(answer)
            sys.exit(0)
print(answer-1)

Submission status: Runtime Error

Solution code:
import sys
HK,WK,M = map(int, input().split())
hw_s = [list(map(int, input().split())) for _ in range(M)]
W = [0 for _ in range(HK+1)]
H = [0 for _ in range(WK+1)]
HW = [[0 for i in range(WK+1)] for _ in range(HK+1)]
# 3*10^5

for i, hw in enumerate(hw_s):
    h=hw[0]
    w=hw[1]

    H[h] += 1
    W[w] += 1

    HW[h][w] = 1

HMAXES = []
WMAXES = []
answer = 0
HMAX = max(H)
WMAX = max(W)


# 3*10^5
for i, h in enumerate(H):
    if h == HMAX:
        HMAXES.append(i)
for i, w in enumerate(W):
    if w == WMAX:
        HMAXES.append(i)
# 3*10^5
answer = HMAX + WMAX
for i, h in enumerate(HMAX):
    for j, w in enumerate(WMAX):
        if HW[i][j] == 0:
            print(answer)
            sys.exit(0)
print(answer-1)

Submission status: Runtime Error

Solution code:
h,w,m=map(int,input().split())
table=[]
H=[0]*h
W=[0]*w
li_h=[]
li_w=[]

for i in range(m):
    h1,w1=list(map(int,input().split()))
    table.append((h1-1,w1-1))
    H[h1-1]+=1
    W[w1-1]+=1

h_flag=0
w_flag=0

Hm=max(H)
Wm=max(W)

Hh=[i for i, x in enumerate(H) if x == HM]
Ww=[i for i, x in enumerate(W) if x == Wm]

cnt=0

for (h, w) in table:
    if H[h] == HM and W[w] ==  Wm:
        cnt += 1

if cnt<len(Hh)*len(Ww):
    print(HM+Wm)
else:
    print(HM+Wm-1)

Submission status: Runtime Error

Solution code:
# -*- coding:utf-8 -*-

def solve():
    import sys
    from collections import deque
    import heapq

    H, W = list(map(int, sys.stdin.readline().split()))
    Ch, Cw = list(map(int, sys.stdin.readline().split()))
    Ch, Cw = Ch-1, Cw-1
    Dh, Dw = list(map(int, sys.stdin.readline().split()))
    Dh, Dw = Dh-1, Dw-1
    S = []
    for _ in range(H):
        s = input()
        S.append(list(s))

    que = []
    heapq.heapify(que)
    heapq.heappush(que, [0, Ch+Cw*(10**4)])  # [ワープ回数, 座標]
    visited = set()
    while len(que) != 0:
        n, yx = heapq.heappop(que)
        y, x = yx%(10**4), yx//(10**4)
        if (y, x) in visited: continue
        visited.add((y, x))

        # ゴールか？
        if y == Dh and x == Dw:
            print(n)
            return

        # 上下左右移動可能ならばqueに入れる
        for ny, nx in ([y-1, x], [y+1, x], [y, x-1], [y, x+1]):
            if 0 <= ny < H and 0 <= nx < W and not (ny, nx) in visited and S[ny][nx] == ".":
                heapq.heappush(que, [n, ny+nx*(10**4)])
        # ワープ可能位置をqueに入れる
        for ny, nx in ([y-2, x-2], [y-2, x-1], [y-2, x], [y-2, x+1], [y-2, x+2],
                       [y-1, x-2], [y-1, x-1], [y-1, x+1], [y-1, x+2],
                       [y, x-2], [y, x+2],
                       [y+2, x-2], [y+2, x-1], [y+2, x], [y+2, x+1], [y+2, x+2],
                       [y+1, x-2], [y+1, x-1], [y+1, x+1], [y+1, x+2]):
            if 0 <= ny < H and 0 <= nx < W and not (ny, nx) in visited and S[ny][nx] == ".":
                heapq.heappush(que, [n+1, ny+nx*(10**4)])

    # ゴールはたどり着かなかったよ・・・
    print(-1)
    return


def solve2():
    """TLE"""
    import sys
    from collections import deque
    import heapq

    H, W = list(map(int, sys.stdin.readline().split()))
    Ch, Cw = list(map(int, sys.stdin.readline().split()))
    Ch, Cw = Ch-1, Cw-1
    Dh, Dw = list(map(int, sys.stdin.readline().split()))
    Dh, Dw = Dh-1, Dw-1
    S = []
    for _ in range(H):
        s = input()
        S.append(list(s))

    que = []
    heapq.heapify(que)
    heapq.heappush(que, [0, Ch, Cw])  # [ワープ回数, h座標, w座標]
    visited = set()
    while len(que) != 0:
        n, y, x = heapq.heappop(que)
        visited.add((y, x))

        # ゴールか？
        if y == Dh and x == Dw:
            print(n)
            return

        # 上下左右移動可能ならばqueに入れる
        for ny, nx in ([y-1, x], [y+1, x], [y, x-1], [y, x+1]):
            if 0 <= ny < H and 0 <= nx < W and not (ny, nx) in visited and S[ny][nx] == ".":
                heapq.heappush(que, [n, ny, nx])
        # ワープ可能位置をqueに入れる
        for ny, nx in ([y-2, x-2], [y-2, x-1], [y-2, x], [y-2, x+1], [y-2, x+2],
                       [y-1, x-2], [y-1, x-1], [y-1, x+1], [y-1, x+2],
                       [y, x-2], [y, x+2],
                       [y+2, x-2], [y+2, x-1], [y+2, x], [y+2, x+1], [y+2, x+2],
                       [y+1, x-2], [y+1, x-1], [y+1, x+1], [y+1, x+2]):
            if 0 <= ny < H and 0 <= nx < W and not (ny, nx) in visited and S[ny][nx] == ".":
                heapq.heappush(que, [n+1, ny, nx])

    # ゴールはたどり着かなかったよ・・・
    print(-1)
    return


if __name__ == "__main__":
    solve()

Submission status: Runtime Error

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

hw = set()
w = [0] * 3*10**5
h = [0] * 3*10**5

for i in range(M):
    h_i, w_j = map(int, input().split())
    hw.add([h_i-1, w_j-1])
    h[h_i-1] += 1
    w[w_j-1] += 1

a = 0
d = 1
mxh_v = max(h)
mxw_v = max(w)

x = [i for i,j in enumerate(h) if j == mxh_v]
y = [i for i,j in enumerate(w) if j == mxw_v]

for i in x:
    for j in y:
        if not([i,j] in hw):
            print(mxh_v+mxw_v)
            exit()
print(mxh_v+mxw_v-d)

Submission status: Runtime Error

Solution code:
import sys, math, bisect



def solve(W_N, V_N, bombs, H, W):

    max_x = 1
    xs = []
    for i in range(W):
        if(W_N[i] > max_x):
            max_x = W_N[i]
            xs = [i]
        elif(W_N[i] == max_x):
            xs.append(i)
    max_y = 1
    ys = []
    for j in range(H):
        if(V_N[j] > max_y):
            max_y = V_N[j]
            ys = [j]
        elif(V_N[j] == max_y):
            ys.append(j)

    value = max_x + max_y

    for j in ys:

        bombs[j].sort()
        for i in xs:
            index = bisect.bisect_left(bombs[j], i)
            if( index < W and i != bombs[j][index] ):
            #if(  i != bombs[j][index] ):
                return value
    return value-1



if(__name__ == "__main__"):
    """
    N = int( input().strip() )
    Length = list( map(int, input().strip().split() ) )
    """
    H, W, N_bombs= map(int, input().strip().split() )


    V_N = [0] * H
    W_N = [0] * W
    bombs = [ [] for _ in range(H) ]


    for i in range(N_bombs):
        y, x = map(int, input().strip().split() )
        """
        W_N_bomb[x-1][0] += 1
        V_N_bomb[y-1][0] += 1
        """
        V_N[y-1] += 1
        W_N[x-1] += 1
        bombs[y-1].append(x-1)




    print( solve(W_N, V_N, bombs, H, W) )

Submission status: Runtime Error

Solution code:
import sys, math, heapq, bisect



def solve(W_N, V_N, bombs, H, W):

    max_x = 1
    xs = []
    for i in range(W):
        if(W_N[i] > max_x):
            max_x = W_N[i]
            xs = [i]
        elif(W_N[i] == max_x):
            xs.append(i)
    max_y = 1
    ys = []
    for j in range(H):
        if(V_N[j] > max_y):
            max_y = V_N[j]
            ys = [j]
        elif(V_N[j] == max_y):
            ys.append(j)

    value = max_x + max_y

    for j in ys:
        bombs[j].sort()
        for i in xs:
            index = bisect.bisect_left(bombs[j], i)
            if( i!=bombs[j][index] ):
                return value
    return value-1

    """
    for i in range( H ):
        if(V_N[i]==max_y):
            for j in range( W ):
                if(W_N[j]==max_x and W_N[j] not in bombs[i] ):
                #if(W_N[j]==max_x and bombs[i][j] == 0 ):

                    return value

    return value-1
    """

if(__name__ == "__main__"):
    """
    N = int( input().strip() )
    Length = list( map(int, input().strip().split() ) )
    """
    H, W, N_bombs= map(int, input().strip().split() )

    """
    V_N_bomb = [ [0, i]  for i in range(H) ]
    W_N_bomb = [ [0, i]  for i in range(W) ]
    """
    V_N = [0] * H
    W_N = [0] * W
    bombs = [ [] for _ in range(H) ]


    for i in range(N_bombs):
        y, x = map(int, input().strip().split() )
        """
        W_N_bomb[x-1][0] += 1
        V_N_bomb[y-1][0] += 1
        """
        V_N[y-1] += 1
        W_N[x-1] += 1
        bombs[y-1].append(x-1)


    """
    W_N_bomb.sort(key = lambda x: x[0])
    V_N_bomb.sort(key = lambda x: x[0])
    """

    print( solve(W_N, V_N, bombs, H, W) )

    """


    Ws = [ W_N_bomb[i][0] for i in range(W)]
    Hs = [ V_N_bomb[i][0] for i in range(H)]



    pos_x= bisect.bisect_left(Ws, Ws[-1])
    pos_y= bisect.bisect_left(Hs, Hs[-1])


    xs = W_N_bomb[pos_x:]
    ys = V_N_bomb[pos_y:]
    # xs.sort(key = lambda x: x[1])
    # ys.sort(key = lambda x: x[1])

    flag = False
    value = Ws[-1] + Hs[-1]

    for i in range( len(ys) ):
        for j in range( len(xs) ):
            if( bombs[i][j] == 1 ):
                flag = True
                break

    xs2 = [ xs[i][1] for i in range( len(xs) )]
    ys2 = [ ys[i][1] for i in range( len(ys) )]
    #print(W_N_bomb, V_N_bomb, pos_x, pos_y)
    flag = False
    value = Ws[-1] + Hs[-1]

    if(  bisect.bisect(bomb, xs[0]) != bisect.bisect(bomb, xs[-1]) ):
        flag = True
    else:
        for j in range(pos_y, H):
            pos_j = V_N_bomb[j][1]
            if(len(bombs[pos_j])==0 ):
                flag = True
                break
            indices = []
            for i in range(pos_x, W):
                pos_i = W_N_bomb[i][1]

                indices.append( bisect.bisect(bombs[pos_j], pos_i) )
                indices.sort()
                if(indices[0] != indices[-1])
                    flag=True
                    break

    if(flag==True):
        print(value)
    else:
        print(value-1)
    """