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 heapq
def main():
    H, W, M = map(int, input().split())

    bn_h = {}
    bn_v = {}

    for i in range(M):
        h, w = map(int, input().split())
        bn_h.setdefault(h, set())
        bn_h[h].add(w)
        bn_v.setdefault(w, set())
        bn_v[w].add(h)

    b1 = sorted(bn_h.items(), key = lambda x:len(x[1]), reverse=True)
    b2 = sorted(bn_v.items(), key = lambda x:len(x[1]), reverse=True)

    m1 = len(b1[0][1])
    m2 = len(b2[0][1])

    max_result = 0
    for i in b1:
        h, sh = i
        if len(sh) < m1:
            break
        for j in b2:
            v, sv = j
            if len(sv) < m2:
                break
            result = len(sh) + len(sv) - 1
            if h not in sv:
                result += 1
            flag = True
            max_result = max(max_result, result)
            if flag = True:
                break
        if flag = True:
            break
    print(max_result)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# coding: utf-8

import numpy as np

def main():
    h, w, m = map(int, input().split())
    matrix = np.zeros((h, w))
    
    for i in range(m):
        x, y = map(int, input().split())
        matrix[x-1, y-1] = 1
    
    sum_row = matrix.sum(axis=1).reshape(h, 1)
    sum_col = matrix.sum(axis=0)
    
    row_matrix = np.tile(sum_row, (1, w))
    col_matrix = np.tile(sum_col, (h, 1))
    
    ans_matrix = row_matrix + col_matrix - matrix
    
    
    print(int(np.max(ans_matrix)))
 
main()

Submission status: Runtime Error

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

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

count_H = [0 for _ in range(H)]
count_W = [0 for _ in range(W)]
bomb = set()

for _ in range(M):
  h,w = map(int, input().split())
  bomb.add((h-1,w-1))
  count_H[h-1] += 1
  count_W[w-1] += 1

def argmax(L):
  indeices = []
  maximum = 0
  for i in range(len(L)):
    if L[i]>maximum:
      indices = [i]
      maximum = L[i]
    elif L[i]==maximum:
      indices.append(i)
  return indices, maximum

indices_H, max_H = argmax(count_H)
indices_W, max_W = argmax(count_W)

answer = max_H + max_W - 1
flg = -1
for h in indices_H:
  for w in indices_W:
    if (h,w) not in bomb:
      answer = max_H + max_W
      flg = 0
      break
  if flg == 0:
    break

print(answer)

Submission status: Runtime Error

Solution code:
def putmx(lis):
  ret=[]
  mx=max(lis)
  for i in range(len(lis)):
    if mx==lis[i]:
      ret.append(i)
  return ret

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

hb=[0]*h
wb=[0]*w
mp=[]
for i in range(h):
  mp.append([0]*w)
for i in range(m):
  hh,ww=map(int,input().split())
  hh-=1
  ww-=1
  hb[hh]+=1
  wb[ww]+=1
  mp[hh][ww]=1

hmx=putmx(hb)
wmx=putmx(wb)
mhb=max(hb)
mwb=max(wb)

fl=False
for i in hmx:
  for j in wmx:
    if mp[i][j]!=1:
      print(mhb+mwb)
      fl=True
      break
  if fl:
    break
else:
  print(mhb+mwb-1)

Submission status: Runtime Error

Solution code:
h,w,m=list(map(int, input().split()))
hl=[]
wl=[]
hc=[0]*h
wc=[0]*w
mp=[0]*(h*w)
for j in range(m):
    h1,w1=[int(j) for j in input().split()]
    hl.append(h1)
    wl.append(w1)
for i in range(m):
    x=hl[i]-1
    y=wl[i]-1
    z=y*h+x
    hc[x]+=1
    wc[y]+=1
    mp[z]+=1
mx=max(hc)
my=max(wc)
lmx=[]
lmy=[]
for p in range (h):
    if hc[p]==mx:
        lmx.append(p)
for q in range (w):
    if wc[q]==my:
        lmy.append(q)
zzx=len(lmx)
zzy=len(lmy)
zzz=1
if zzx*zzy<=300000:
    for s in range(zzx):
        ss=lmx[s]
        for t in range(zzy):
            tt=lmy[t]
            uu=tt*h+ss
            if mp[uu]==0:
                zzz=0
else:
    zzz=0
print(mx+my-zzz)

Submission status: Runtime Error

Solution code:
H, W, M = map(int, input().split())
h = [0 for i in range(H)]
w = [0 for i in range(W)]
dp = [[0 for i in range(W)] for j in range(H)]

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

hhh = max(h)
www = max(w)

print(hhh + www - (1-check))

Submission status: Runtime Error

Solution code:
import numpy as np
from collections import Counter
h,w,m=(int(x) for x in input().split())
a=[[int(x) for x in input().split()] for _ in range(m)]
b=np.array(a).reshape(-1,)
c=Counter(b[0::2]).most_common()
d=Counter(b[1::2]).most_common()

i=0
j=0
while len(c)<=i+1 and c[0][1]==c[i][1]:
    while len(d)<=j+1 and d[0][1] == d[j][1]:
        if not [c[i][0],d[i][0]] in a:
            print(c[0][1]+d[0][1])
            quit()
        j+=1
    i+=1
print(c[0][1]+d[0][1]-1)

Submission status: Runtime Error

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

grid = [[0]*W for _ in range(H)]
grid_h = [0]*H
grid_w = [0]*W
ans = 0

for i in range(m):
    h,w = map(int,input().split())
    grid_h[h-1] += 1
    grid_w[w-1] += 1
    grid[h-1][w-1] = 1

for i in range(H):
    for j in range(W):
        if grid[i][j] == 1:
            ans = max(ans,grid_h[i]+grid_w[j]-1)
        else:
            ans = max(ans,grid_h[i]+grid_w[j])

print(ans)

Submission status: Runtime Error

Solution code:
h,w,m = map(int,input().split())
lir = []
lic = []
ans = 0
for _ in range(m):
    x,y = map(int,input().split())
    lir.append(x)
    lic.append(y)
maxr = max(lir, key = lir.count)
while maxr in lir:
    ans+=1
    ind = lir.index(maxr)
    lir.pop(ind)
    lic.pop(ind)
maxc = max(lic, key = lic.count)
while maxc in lic:
    ans+=1
    ind = lic.index(maxc)
    lic.pop(ind)
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = list(map(int,input().split()))
grid = np.zeros((H,W),int)
Hnum = np.zeros((H),int)
Wnum = np.zeros((W),int)
for i in range(M):
    bh, bw = list(map(int,input().split()))
    Hnum[bh-1] += 1
    Wnum[bw-1] += 1
    grid[bh-1, bw-1] = 1

maxb = 0
for i in range(H):
    for j in range(W):
        bomb = Hnum[i]+Wnum[j]-grid[i,j]
        if maxb < bomb:
            maxb = bomb
print(maxb)

Submission status: Runtime Error

Solution code:
H,W,M = map(int,input().split())
a = [[0]*W for i in range(H)]
hh = [0] *H
ww = [0] * W
for i in range(M):
    h,w = (map(lambda a: int(a)-1, input().split()))
    hh[h] += 1
    ww[w] += 1
    a[h][w] = 1
Max = 0
for h in range(H):
    for w in range(W):
        Max = max(Max,hh[h]+ww[w]-a[h][w])
print(Max)

Submission status: Runtime Error

Solution code:
h,w,m=map(int,input().split())
hm=[0]*h
wm=[0]*m
za=[]
for i in range(m):
  hi,wi=map(int,input().split())
  hi-=1
  wi-=1
  hm[hi]+=1
  wm[wi]+=1
  za.append([hi,wi])
maxh=max(hm)
maxw=max(wm)
mhl=[i for i,j in enumerate(hm) if j==maxh]
mwl=[i for i,j in enumerate(wm) if j==maxw]
for i in mhl:
  for j in mwl:
    if [i,j] not in za:
      print(maxh+maxw)
      break
  else:  
    continue
  break
else:
  print(maxh+maxw-1)

Submission status: Runtime Error

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

for _ in range(m):
    i,j=map(int,input().split())
    H[i-1] += 1
    W[j-1] += 1
if m==1:print(1)
else:print(max(H)+max(W)-1)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

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

maze = [[0]*W for _ in range(H)]
for _ in range(M):
  h,w = map(int,input().split())
  h -= 1
  w -= 1
  maze[h][w] = 1

max_row = 0
rows = defaultdict(list)
for i in range(H):
  s = sum(maze[i])
  rows[s].append(i)
  max_row = max(max_row, s)

max_col = 0
cols = defaultdict(list)
for i in range(W):
  s = 0
  for j in range(H):
    s += maze[j][i]
  cols[s].append(i)
  max_col = max(max_col, s)

r_cans = rows[max_row]
c_cans = cols[max_col]

ans = 0
for r_can in r_cans:
  for c_can in c_cans:
    t = max_row + max_col
    if maze[r_can][c_can]: t -= 1
    ans = max(ans, t)

print(ans)

Submission status: Runtime Error

Solution code:
H, W, M = map(int, input().split())
HW = [[0] * W for i in range(H)]
H_list = [0] * H
W_list = [0] * W
for i in range(M):
  h, w = map(int, input().split()) 
  HW[h - 1][w - 1] = 1
  H_list[h - 1] += 1
  W_list[w - 1] += 1
  
#print(HW, H_list, W_list)  
H_max = max(H_list)
W_max = max(W_list)
H_index = []
W_index = []
for i in range(H):
  if H_list[i] == H_max:
    H_index.append(i)
    
for i in range(W):
  if W_list[i] == W_max:
    W_index.append(i)
    

x = 1
for i in H_index:
  for j in W_index:
    if HW[i][j] == 0:
      x = 0
      
print(H_max + W_max - x)

Submission status: Runtime Error

Solution code:
import numpy as np
from collections import Counter
h,w,m=(int(x) for x in input().split())
a=[[int(x) for x in input().split()] for _ in range(m)]
b=np.array(a).reshape(-1,)
ma=0
c=Counter(b[0::2]).most_common()
d=Counter(b[1::2]).most_common()

print(c,d,c[0][1],d[0][1])
i=0
j=0
while len(c)<=i+1 and c[0][1]==c[i][1]:
    while len(c)<=j+1 and d[0][1] == d[j][1]:
        if not [c[i][0],d[i][0]] in a:
            print(c[0][1]+d[0][1],c[i][0],d[i][0],a)
            quit()
        j+=1
    i+=1
print(c[0][1]+d[0][1]-1)

Submission status: Runtime Error

Solution code:
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前
sort()
sorted(reverse=true)
配列の初期化
配列名=[a]*n+[b]*m
Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter
import itertools
import fractions
import sys

d = [int(s) for s in stdin.readline().rstrip().split()]
h = d[0]
w = d[1]
m = d[2]
hCount = [0]*h
wCount = [0]*w
poss = [[0]*h]*w
for el in range(m):
    pos = [int(s) for s in stdin.readline().rstrip().split()]
    hCount[pos[0]-1] += 1
    wCount[pos[1]-1] += 1
    poss[pos[0]-1][pos[1]-1] = 1
hPos = 0
hValue = 0
count = 0
for el in hCount:
    if hValue < el:
        hValue = el
        hPos = count
    count += 1
wPos = 0
wValue = 0
count = 0
for el in wCount:
    if wValue < el:
        wValue = el
        wPos = count
    count += 1
if poss[hPos][wPos] == 1:
    print(wValue+hValue-1)
else:
    print(wValue+hValue)

Submission status: Runtime Error

Solution code:
from collections import Counter
import numpy as np
H, W, M = map(int, input().split())
h_list = []
w_list = []
ans = 0
for _ in range(M):
  h, w = map(int, input().split())
  h_list.append(h)
  w_list.append(w)
c_h = Counter(h_list)
h_list = np.array(h_list)
w_list = np.array(w_list)
h_most_common, h_most_common_count = c_h.most_common()[0]
ans += h_most_common_count
c_w = Counter(w_list[h_list != h_most_common])
w_most_common, w_most_common_count = c_w.most_common()[0]
ans += w_most_common_count

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

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

G = np.zeros((H, W))
for _ in range(M):
    h, w = map(int, input().split())
    G[h-1, w-1] += 1

h_idx = np.argmax(np.sum(G, axis=0))
w_idx = np.argmax(np.sum(G, axis=1))

print(int(sum(G[:, h_idx]) + sum(G[w_idx]) - G[h_idx, w_idx]))

Submission status: Runtime Error

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

import numpy as np

H,W,M=map(int,input().split())
V=np.zeros([H,W])

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

hs=[]
for i in range(H):
    hs.append(sum(V[i,:]))

ws=[]
for i in range(W):
    ws.append(sum(V[:,i]))

MH=max(hs)
MW=max(ws)
ans=MH+MW

mh=[]
for i in range(len(hs)):
    if hs[i]==MH:
        mh.append(i)
mw=[]
for i in range(len(ws)):
    if ws[i]==MW:
        mw.append(i)

add=1
for i in mh:
    for j in mw:
        if V[i,j]==0: 
            add=0
            break


ans-=add

print(int(ans))

Submission status: Runtime Error

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

field=np.zeros([h,w])

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

print(int(field.max()))

Submission status: Runtime Error

Solution code:
class SquareSkipList:
    # SkipList の層数を 2 にした感じの何か
    # std::multiset の代用になる
    # 検証1 (add, pop) データ構造: https://atcoder.jp/contests/arc033/submissions/14718760
    # 検証2 (init, add, remove, search_higher_equal) Exclusive OR Queries: https://atcoder.jp/contests/cpsco2019-s1/submissions/14705333
    # 検証3 (add, search_higher, search_lower) Second Sum: https://atcoder.jp/contests/abc140/submissions/7488469
    # 検証4 (add, __getitem__) [CF] Optimal Subsequences (Hard Version): https://codeforces.com/contest/1261/submission/65643461
    # 検証5 (init, add, pop) Dinner Planning: https://atcoder.jp/contests/code-festival-2018-final-open/submissions/13916065
    # 検証6 (要素がタプル, init, add, remove, max, pop_max) Lake: https://atcoder.jp/contests/snuke21/submissions/14718529
    # 検証7 (init, add, remove, pop, max, pop_max) ドーナツの箱詰め: https://atcoder.jp/contests/donuts-2015/submissions/14829916
    # 検証8 (init, add, remove, min, max) Smart Infants: https://atcoder.jp/contests/abc170/submissions/15264112
    def __init__(self, values=None, sorted_=False, square=1000, seed=42, inf=float("inf")):
        # values: 初期値のリスト
        # sorted_: 初期値がソート済みであるか
        # square: 最大データ数の平方根
        # seed: 乱数のシード
        # inf: 番兵（要素がタプルのときは (float("inf"), float("inf")) にする）
        self.square = square
        if values is None:
            self.rand_y = seed
            self.layer1 = [inf]
            self.layer0 = [[]]
        else:
            self.layer1 = layer1 = []
            self.layer0 = layer0 = []
            if not sorted_:
                values.sort()
            y = seed
            l0 = []
            for v in values:
                y ^= (y & 0x7ffff) << 13
                y ^= y >> 17
                y ^= (y & 0x7ffffff) << 5
                if y % square == 0:
                    layer0.append(l0)
                    l0 = []
                    layer1.append(v)
                else:
                    l0.append(v)
            layer1.append(inf)
            layer0.append(l0)
            self.rand_y = y

    def add(self, x):  # 要素の追加  # O(sqrt(n))
        # xorshift
        y = self.rand_y
        y ^= (y & 0x7ffff) << 13
        y ^= y >> 17
        y ^= (y & 0x7ffffff) << 5
        self.rand_y = y

        if y % self.square == 0:
            layer1, layer0 = self.layer1, self.layer0
            idx1 = bisect_right(layer1, x)
            layer1.insert(idx1, x)
            layer0_idx1 = layer0[idx1]
            idx0 = bisect_right(layer0_idx1, x)
            layer0.insert(idx1 + 1, layer0_idx1[idx0:])  # layer0 は dict で管理した方が良いかもしれない  # dict 微妙だった
            del layer0_idx1[idx0:]
        else:
            idx1 = bisect_right(self.layer1, x)
            insort_right(self.layer0[idx1], x)

    def remove(self, x):  # 要素の削除  # O(sqrt(n))
        # x が存在しない場合、x 以上の最小の要素が削除される
        idx1 = bisect_left(self.layer1, x)
        layer0_idx1 = self.layer0[idx1]
        idx0 = bisect_left(layer0_idx1, x)
        if idx0 == len(layer0_idx1):
            del self.layer1[idx1]
            self.layer0[idx1] += self.layer0.pop(idx1 + 1)
        else:
            del layer0_idx1[idx0]

    def search_higher_equal(self, x):  # x 以上の最小の値を返す  O(log(n))
        idx1 = bisect_left(self.layer1, x)
        layer0_idx1 = self.layer0[idx1]
        idx0 = bisect_left(layer0_idx1, x)
        if idx0 == len(layer0_idx1):
            return self.layer1[idx1]
        return layer0_idx1[idx0]

    def search_higher(self, x):  # x を超える最小の値を返す  O(log(n))
        idx1 = bisect_right(self.layer1, x)
        layer0_idx1 = self.layer0[idx1]
        idx0 = bisect_right(layer0_idx1, x)
        if idx0 == len(layer0_idx1):
            return self.layer1[idx1]
        return layer0_idx1[idx0]

    def search_lower(self, x):  # x 未満の最大の値を返す  O(log(n))
        idx1 = bisect_left(self.layer1, x)
        layer0_idx1 = self.layer0[idx1]
        idx0 = bisect_left(layer0_idx1, x)
        if idx0 == 0:  # layer0_idx1 が空の場合とすべて x 以上の場合
            return self.layer1[idx1 - 1]
        return layer0_idx1[idx0 - 1]

    def pop(self, idx):
        # 小さい方から idx 番目の要素を削除してその要素を返す（0-indexed）
        # O(sqrt(n))
        # for を回すので重め、使うなら square パラメータを大きめにするべき
        layer0 = self.layer0
        s = -1
        for i, l0 in enumerate(layer0):
            s += len(l0) + 1
            if s >= idx:
                break
        if s == idx:
            layer0[i] += layer0.pop(i + 1)
            return self.layer1.pop(i)
        else:
            return layer0[i].pop(idx - s)

    def pop_max(self):
        # 最大値を削除してその要素を返す（0-indexed） O(1)
        # 空ならエラー
        if self.layer0[-1]:
            return self.layer0[-1].pop()
        else:
            del self.layer0[-1]
            return self.layer1.pop(-2)

    def __getitem__(self, item):
        # 小さい方から idx 番目の要素を返す  O(sqrt(N))
        layer0 = self.layer0
        s = -1
        for i, l0 in enumerate(layer0):
            s += len(l0) + 1
            if s >= item:
                break
        if s == item:
            return self.layer1[i]
        else:
            return layer0[i][item - s]

    def min(self):  # 最小値を返す  空なら inf を返す  O(1)
        return self.layer0[0][0] if self.layer0[0] else self.layer1[0]

    def max(self):  # 最大値を返す  空ならエラー  O(1)
        return self.layer0[-1][-1] if self.layer0[-1] else self.layer1[-2]

    def merge(self, r):  # 結合  O(sqrt(n))
        self.layer0[-1] += r.layer0[0]
        self.layer0 += r.layer0[1:]
        del self.layer1[-1]
        self.layer1 += r.layer1

    def split(self, k):  # k 以上を切り離す  O(sqrt(n))
        idx1 = bisect_left(self.layer1, k)
        layer0_idx1 = self.layer0[idx1]
        idx0 = bisect_left(layer0_idx1, k)
        r = SquareSkipList(square=self.square, seed=self.rand_y)
        r.layer1 = self.layer1[idx1:]
        r.layer0 = [layer0_idx1[idx0:]] + self.layer0[idx1 + 1:]
        del self.layer1[idx1:-1], layer0_idx1[idx0:], self.layer0[idx1 + 1:]
        return r

    def print(self):
        print(self.layer1)
        print(self.layer0)

    def __iter__(self):
        layer1 = self.layer1
        layer0 = self.layer0
        idx1 = idx0 = 0
        layer0_idx1 = layer0[idx1]
        while True:
            if len(layer0_idx1) == idx0:
                if len(layer1) - 1 == idx1:
                    return
                yield layer1[idx1]
                idx1 += 1
                layer0_idx1 = layer0[idx1]
                idx0 = 0
            else:
                yield layer0_idx1[idx0]
                idx0 += 1


import sys
from operator import itemgetter
from itertools import groupby
def main():
    H, W, M = map(int, sys.stdin.buffer.readline().split())
    hw = map(int, sys.stdin.buffer.read().split())
    hw = sorted(zip(hw, hw), key=itemgetter(0))
    Cnt_w = [0] * (W+1)
    for _, w in hw:
        Cnt_w[w] += 1
    sl = SquareSkipList(Cnt_w[:])
    ans = 0
    for h, group in groupby(hw, key=itemgetter(0)):
        group = list(group)
        for _, w in group:
            cnt = Cnt_w[w]
            sl.remove(cnt)
            sl.add(cnt-1)
            Cnt_w[w] -= 1
        an = len(group) + sl.max()
        if ans < an:
            ans = an
        for _, w in group:
            cnt = Cnt_w[w]
            sl.remove(cnt)
            sl.add(cnt+1)
            Cnt_w[w] += 1
    print(ans)

main()

Submission status: Runtime Error

Solution code:
import numpy as np
h, w, m = map(int, input().split())
data = [list(map(int, input().split())) for _ in range(m)]
data = np.array(data)
field = np.zeros((h, w), dtype=np.int)
for i in range(m):
    field[data[i][0] - 1, :] += 1
    field[:, data[i][1] - 1] += 1
    field[data[i][0] - 1, data[i][1] - 1] -= 1
print(np.max(field))

Submission status: Runtime Error

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

z = []
Hc = [0]*m
Wc = [0]*m

for i in range(m):
    th,tw = map(int,input().split())
    z.append((th,tw))
    Hc[th-1] += 1
    Wc[tw-1] += 1

Hi = [n for n,v in enumerate(Hc) if v == max(Hc)]
Wi = [n for n,v in enumerate(Wc) if v == max(Wc)]

ans = 0
for i in Hi:
    for j in Wi:
        ans = Hc[i] + Wc[j]
        if (i+1,j+1) in z:
            ans -= 1
        else:
            break

print(ans)

Submission status: Runtime Error

Solution code:

def get_count(li,c):
    m=(li[0][c],1)
    h={}
    h[li[0][c]]=1
    for i in li[1:]:
        if(i[c] in h):
            h[i[c]]+=1
        else:
            h[i[c]]=1
        if(m[1]<h[i[c]]):
            m=(i[c],h[i[c]])
    return m


h,w,n=list(map(int,input().split()))
if(h==1 or w==1 or n<=1):
    li = []
    for i in range(n):
        li.append(tuple(map(int, input().split())))
    print(n)
else:
    li=[]
    for i in range(n):
        li.append(tuple(map(int,input().split())))
    li=list(set(li))

    r=get_count(li,0)
    c=get_count(li,1)
    # print(r,c)
    s=0
    if(r[1]>c[1]):
        temp=[]
        for i in li:
            if(i[0]!=r[0]):
                temp.append(i)
        s+=r[1]+get_count(temp,1)[1]
    else:
        temp=[]
        for i in li:
            if(i[1]!=c[0]):
                temp.append(i)
        s+=c[1]+get_count(temp,0)[1]

    print(s)

Submission status: Runtime Error

Solution code:
from collections import Counter
import numpy as np
H, W, M = map(int, input().split())
S = np.zeros((H, W))
h_list = []
w_list = []

ans = 0
for _ in range(M):
  h, w = map(int, input().split())
  h_list.append(h)
  w_list.append(w)
c_h = Counter(h_list)
h_list = np.array(h_list)
w_list = np.array(w_list)
h_most_common, h_most_common_count = c_h.most_common()[0]
ans += h_most_common_count
c_w = Counter(w_list[h_list != h_most_common])
w_most_common, w_most_common_count = c_w.most_common()[0]
ans += w_most_common_count

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = list(map(int,input().split()))
grid = np.zeros((H,W),int)
Hnum = np.zeros((H),int)
Wnum = np.zeros((W),int)
for i in range(M):
    bh, bw = list(map(int,input().split()))
    Hnum[bh-1] += 1
    Wnum[bw-1] += 1
    grid[bh-1, bw-1] = 1

# print(grid)
# print(Hnum, Wnum)

maxH = np.argmax(Hnum)
maxW = np.argmax(Wnum)
print(Hnum[maxH]+Wnum[maxW]-grid[maxH,maxW])

Submission status: Runtime Error

Solution code:
# Fast IO (be careful about bytestring)

# import os,io
# input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline


H,W,M = map(int,input().split())
targetList = []
rowCount = [0] * H
columnCount = [0] * W
for _ in range(M):
    p,q = map(int,input().split())
    targetList.append((p-1,q-1))
    rowCount[p-1] += 1
    columnCount[q-1] += 1
rowCountLabelled = []
columnCountLabelled = []
for i in range(H):
    rowCountLabelled.append((rowCount[i],i))
for i in range(W):
    columnCountLabelled.append((columnCount[i],i))
rowCountLabelled.sort(reverse = True)
columnCountLabelled.sort(reverse = True)
targetList.sort()
maxi = rowCountLabelled[0][0] + columnCountLabelled[0][0]
isMaxAchievable = False
for elem1 in rowCountLabelled:
    if elem1[0] != rowCountLabelled[0][0]:
        break
    for elem2 in columnCountLabelled:
        if elem2[0] != columnCountLabelled[0][0]:
            break
        target = (elem1[1],elem2[1])
        l = 0
        r = len(targetList)
        while l < r:
            m = (l + r) // 2
            if target == targetList[m]:
                l = m
                r = m
                break
            elif target < targetList[m]:
                r = m-1
            elif target > targetList[m]:
                l = m + 1
        if (l >= len(targetList) or target != targetList[l]) and target != targetList[r]:
            isMaxAchievable = True
            break
    if isMaxAchievable:
        break
if isMaxAchievable:
    print(maxi)
else:
    print(maxi - 1)

Submission status: Runtime Error

Solution code:
import numpy as np
h, w, m = map(int, input().split())
data = [list(map(int, input().split())) for _ in range(m)]
field = np.zeros((h, w), dtype=np.int)
for i in range(m):
    field[data[i][0] - 1, :] += 1
    field[:, data[i][1] - 1] += 1
    field[data[i][0] - 1, data[i][1] - 1] -= 1
print(np.max(field))

Submission status: Runtime Error

Solution code:
import sys
from collections import Counter
input = lambda : sys.stdin.readline().rstrip()

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

xs = []
ys = []
for i in range(m):
    x, y = map(int, input().split(" "))
    xs.append(x)
    ys.append(y)

xcounter = Counter(xs)
ycounter = Counter(ys)

x_most, x_most_num = xcounter.most_common(1)[0]
y_most, y_most_num = ycounter.most_common(1)[0]

out = 0
if x_most_num < y_most_num:
    #swap
    x_most_num = y_most_num
    tmp = ys
    ys = xs
    xs = tmp


out = x_most_num

y2s = [] 
for i in range(m):
    if xs[i] == x_most:
        continue
    else:  
        y2s.append(ys[i])
    

y2counter = Counter(y2s)
y2_most, y2_most_num = y2counter.most_common(1)[0]
out += y2_most_num

print(out)

Submission status: Runtime Error

Solution code:

def get_count(li,c):
    m=(li[0][c],1)
    h={}
    h[li[0][c]]=1
    for i in li[1:]:
        if(i[c] in h):
            h[i[c]]+=1
        else:
            h[i[c]]=1
        if(m[1]<h[i[c]]):
            m=(i[c],h[i[c]])
    return m


h,w,n=list(map(int,input().split()))
if(h==1 or w==1):
    li = []
    for i in range(n):
        li.append(tuple(map(int, input().split())))
    print(n)
else:
    li=[]
    for i in range(n):
        li.append(tuple(map(int,input().split())))
    li=list(set(li))

    r=get_count(li,0)
    c=get_count(li,1)
    # print(r,c)
    s=0
    if(r[1]>c[1]):
        temp=[]
        for i in li:
            if(i[0]!=r[0]):
                temp.append(i)
        s+=r[1]+get_count(temp,1)[1]
    else:
        temp=[]
        for i in li:
            if(i[1]!=c[0]):
                temp.append(i)
        s+=c[1]+get_count(temp,0)[1]

    print(s)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)


def main(input, print):
    H, W, M = map(int, input().split())
    hdic = [0] * (3*10**5)
    wdic = [0] * (3*10**5)
    table = [[0 for _ in range(2)] for _ in range(M)]
    hmax = 0
    wmax = 0
    hkey = None
    wkey = None
    for i in range(M):
        h, w = map(int, input().split())
        table[i][0] = h
        table[i][1] = w
        hdic[h] += 1
        wdic[w] += 1
        if hmax < hdic[h]:
            hmax = hdic[h]
            hkey = h
        if wmax < wdic[h]:
            wmax = wdic[h]
            wkey = h
    ans = 0
    for i in range(M):
        if table[i][0] == hkey:
            ans += 1
        elif table[i][1] == wkey:
            ans += 1
    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)

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())
    H_list = [0] * H
    W_list = [0] * W
    grid = [[0]*W for _ in range(H)]

    for  _ in range(M):
        h, w = map(int, readline().split())
        H_list[h-1] += 1
        W_list[w-1] += 1
        grid[h-1][w-1] += 1
    H_max = 0
    W_max = 0
    H_max_index = []
    W_max_index = []

    for h in range(H):
        if H_list[h]>H_max:
            H_max = H_list[h]
            H_max_index = [h]
        elif H_list[h]==H_max:
            H_max_index.append(h)
        else:
            continue

    for w in range(W):
        if W_list[w]>W_max:
            W_max = W_list[w]
            W_max_index = [w]
        elif W_list[w]==W_max:
            W_max_index.append(w)
        else:
            continue
    ans = 0
    for h in H_max_index:
        for w in W_max_index:
            ans = max(ans, H_list[h]+W_list[w]-grid[h][w])
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
h, w, m = map(int, input().split())
hl=[0]*h
wl=[0]*w
ans =[[0]*w for i in range(h)]
for i in range(m):
  a, b = map(int, input().split())
  a-=1
  b-=1
  hl[a]+=1
  wl[b]+=1
  ans[a][b]+=1

mh = 0
mhi = []
for i in range(h):
  if hl[i]>mh:
    mh = hl[i]
    mhi=[i]
  if hl[i]==mh:
    mhi.append(i)

wh = 0
whi = []
for i in range(w):
  if wl[i]>wh:
    wh = wl[i]
    whi=[i]
  if wl[i]==wh:
    whi.append(i)

for i in mhi:
  for j in whi:
    if ans[i][j]==0:
      print(mh+wh)
      quit()
print(mh+wh-1)

Submission status: Runtime Error

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


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


cnt_h = [0] * h
cnt_w = [0] * w
is_ = [[0] * w for i in range(h)]
for i, j in info:
    i -= 1
    j -= 1
    cnt_h[i] += 1
    cnt_w[j] += 1
    is_[i][j] += 1

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, cnt_h[i] + cnt_w[j] - is_[i][j])
print(ans)

Submission status: Runtime Error

Solution code:

h,w,m=map(int,input().split())
tate=[0]*(h+1)
yoko=[0]*(w+1)
box=[[0]*w for i in range(h)]
for i in range(m):
    a, b = map(int, input().split())
    tate[a]+=1
    yoko[b]+=1
    box[a-1][b-1]+=1
tmax=tate[0]
ymax=yoko[0]
tbox=[0]
ybox=[0]
for i in range(1,h+1):
    if tmax<tate[i]:
        tmax=tate[i]
        tbox=[i]
    elif tmax==tate[i]:
        tbox.append(i)
for i in range(1,w+1):
    if ymax<yoko[i]:
        ymax=yoko[i]
        ybox=[i]
    elif ymax==yoko[i]:
        ybox.append(i)
flag=True
for i in tbox:
    for j in ybox:
        if box[i-1][j-1]==0:
            flag=False
if flag:
    print(tmax+ymax-1)
else:
    print(tmax + ymax)

Submission status: Runtime Error

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

hb=[0]*h
wb=[0]*w
mp=np.zeros((h,w))
for i in range(m):
  hh,ww=map(int,input().split())
  hh-=1
  ww-=1
  hb[hh]+=1
  wb[ww]+=1
  mp[hh][ww]=1

hmx=[i for i, x in enumerate(hb) if x == max(hb)]
wmx=[i for i, x in enumerate(wb) if x == max(wb)]
mhb=max(hb)
mwb=max(wb)
fl=False
for i in hmx:
  for j in wmx:
    if mp[i][j]!=1:
      print(mhb+mwb)
      fl=True
      break
  if fl:
    break
else:
  print(mhb+mwb-1)