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: Accepted

Solution code:
from collections import Counter
H,W,m = map(int,input().split())
ci = Counter([])
cj = Counter([])
s = set()
for i in range(m):
    h,w = map(int,input().split())
    ci[h] += 1
    cj[w] += 1
    s.add((h,w))
mi = max(ci.values())
mj = max(cj.values())
ci = sorted(ci.items(),key=lambda x:x[1], reverse=True)
cj = sorted(cj.items(),key=lambda x:x[1], reverse=True)
xi = [ci[i][0] for i in range(len(ci)) if ci[i][1] == mi]
xj = [cj[i][0] for i in range(len(cj)) if cj[i][1] == mj]
for i in xi:
    for j in xj:
        if (i,j) not in s:
            print(mi+mj)
            quit()
print(mi+mj-1)

Submission status: Accepted

Solution code:
h,w,m=map(int,input().split())
hlist=[0]*(h+1)
wlist=[0]*(w+1)
cnt=set()


for i in range(m):
  a,b=map(int,input().split())
  hlist[a]+=1
  wlist[b]+=1
  cnt.add((a,b))


hcnt,wcnt=max(hlist),max(wlist)


hhh=[i for i,v in enumerate(hlist) if v==hcnt]
www=[i for i,v in enumerate(wlist) if v==wcnt]


for i in hhh:
  for j in www:
    if (i,j) not in cnt:
      print(hcnt+wcnt)
      exit()


print(hcnt+wcnt-1)

Submission status: Accepted

Solution code:
h,w,m=map(int,input().split())
hlist=[0]*(h+1)
wlist=[0]*(w+1)
cnt=set()


for i in range(m):
  a,b=map(int,input().split())
  hlist[a]+=1
  wlist[b]+=1
  cnt.add((a,b))


hcnt,wcnt=max(hlist),max(wlist)


hhh=[i for i,v in enumerate(hlist) if v==hcnt]
www=[i for i,v in enumerate(wlist) if v==wcnt]


for i in hhh:
  for j in www:
    if not (i,j) in cnt:
      print(hcnt+wcnt)
      exit()


print(hcnt+wcnt-1)

Submission status: Accepted

Solution code:
import numpy as np

h,w,m = map(int,input().split())
bomb_list = []
for i in range(m):
    th,tw = map(int,input().split()) 
    bomb_list.append([th,tw])

bomb_list = np.array(bomb_list)

row_dict = [0]*h
column_dict = [0]*w

for i in range(m):
    th,tw = bomb_list[i]
    row_dict[th-1] += 1 
    column_dict[tw-1] += 1 

max_row = max(row_dict)
max_column = max(column_dict)

total = row_dict.count(max_row) * column_dict.count(max_column)

dup = 0
for i in range(m):
    th,tw = bomb_list[i]
    if row_dict[th-1] == max_row and column_dict[tw-1] == max_column:
        dup += 1

if dup < total:
    print(max_row+max_column)
else:
    print(max_row+max_column-1)

Submission status: Accepted

Solution code:
import numpy as np

h,w,m = map(int,input().split())
t = list(tuple(map(lambda x: int(x)-1,input().split())) for _ in range(m))

H = [0] * h
W = [0] * w
for x,y in t:
    H[x] += 1
    W[y] += 1
H_max = max(H)
W_max = max(W)

H_index = [x for x,i in enumerate(H) if i == H_max]
W_index = [y for y,j in enumerate(W) if j == W_max]

ans = H_max + W_max

t = set(t)
for r in H_index:
    for c in W_index:
        if (r, c) not in t:
            print(ans)
            exit()
print(ans - 1)

Submission status: Accepted

Solution code:
H, W, M = map(int, input().split())
HW = [list(map(int, input().split())) for i in range(M)]

g_h = [0] * (H+1)
g_w = [0] * (W+1)

for h, w in HW:
    g_h[h] += 1
    g_w[w] += 1

mxh = max(g_h)
mxw = max(g_w)

cnt = 0
for h, w in HW:
    if g_h[h] == mxh and g_w[w] == mxw:
        cnt += 1
if cnt == g_h.count(mxh) * g_w.count(mxw):
    print(mxh + mxw - 1)
else:
    print(mxh + mxw)

Submission status: Accepted

Solution code:
H,W,M = map(int,input().split())
tate,yoko = [0]*W,[0]*H
ans = 0
memo = set([])
for _ in range(M):
  h,w = map(int,input().split())
  h -= 1
  w -= 1
  tate[w] += 1
  yoko[h] += 1
  memo.add((h,w))
tateArr,yokoArr = [],[]
ans = 0
t,y = max(tate),max(yoko)
for i in range(W):
  if tate[i] == t:
    tateArr.append(i)
for i in range(H):
  if yoko[i] == y:
    yokoArr.append(i)
for i in yokoArr:
  for j in tateArr:
    if (i,j) not in memo:
      print(t+y)
      exit()

print(t+y-1)

Submission status: Accepted

Solution code:
h, w, m = map(int, input().split())
target_points = {tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(m)}

row_points = [0] * h
col_points = [0] * w
for row, col in target_points:
    row_points[row] += 1
    col_points[col] += 1

row_max = max(row_points)
col_max = max(col_points)

row_max_indexes = [i for i in range(h) if row_points[i] == row_max]
col_max_indexes = [i for i in range(w) if col_points[i] == col_max]

empty_crossing_exist = False
for row in row_max_indexes:
    for col in col_max_indexes:
        if (row, col) not in target_points:
            empty_crossing_exist = True
            break
    if empty_crossing_exist:
        break

print(row_max + col_max - (not empty_crossing_exist))

Submission status: Accepted

Solution code:
H, W, M = map(int, input().split())
h_list = [0 for _ in range(H + 1)]
w_list = [0 for _ in range(W + 1)]
bomb = set([])
for _ in range(M):
    h, w = map(int, input().split())
    h_list[h] += 1
    w_list[w] += 1
    bomb.add((h, w))
h_max = max(h_list)
w_max = max(w_list)
h_index = []
w_index = []
for i in range(H + 1):
    if h_list[i] == h_max:
        h_index.append(i)
for i in range(W + 1):
    if w_list[i] == w_max:
        w_index.append(i)
tmp = -1        
for h in h_index:
    for w in w_index:
        if (h, w) not in bomb:
            tmp = 0
            break
print(h_max + w_max + tmp)

Submission status: Accepted

Solution code:
h,w,m=map(int, input().split())
hh=[]
ww=[]
b=set()
for _ in range(m):
    hi,wi=map(int, input().split())
    hh.append(hi)
    ww.append(wi)
    b.add((hi,wi))
from collections import Counter

hc=Counter(hh)
wc=Counter(ww)

hmx=0;hnow=-1
wmx=0;wnow=-1
hs=[];ws=[]
for hi in hc:
    if hc[hi]>hmx:
        hmx=hc[hi]
        hnow=hi
        hs=[hi]
    elif hc[hi]==hmx:
        hs.append(hi)
for wi in wc:
    if wc[wi]>wmx:
        wmx=wc[wi]
        wnow=wi
        ws=[wi]
    elif wc[wi]==wmx:
        ws.append(wi)

ans=hmx+wmx
flag=False
for hi in hs:
    for wi in ws:
        if (hi,wi) not in b:
            flag=True
            break
    if flag:break
if not flag:
    ans-=1

ans2=0

for hi,wi in b:
    ans2=max(ans2,hc[hi]+wc[wi]-1)
print(max(ans,ans2))

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h, w, m = LI()
bomb = defaultdict(lambda : 0)
cnt_h = [0] * h
cnt_w = [0] * w
for _ in range(m):
    H, W = LI()
    bomb[(H-1, W-1)] = 1
    cnt_h[H-1] += 1
    cnt_w[W-1] += 1

m_h = max(cnt_h)
m_w = max(cnt_w)
H_l = []
W_l = []
for i in range(h):
    if cnt_h[i] == m_h:
        H_l.append(i)

for i in range(w):
    if cnt_w[i] == m_w:
        W_l.append(i)

ans = m_h + m_w

for i in H_l:
    for j in W_l:
        if not bomb[(i, j)]:
            print(ans)
            quit()
print(ans-1)

Submission status: Accepted

Solution code:
#実験 貼り付けコード
h,w,m = map(int,input().split())
s = [list(map(int,input().split())) for _ in range(m)]

r = [0] * (h + 1)
c = [0] * (w + 1)
for x, y in s:
    r[x] += 1
    c[y] += 1
R = max(r)
nr = {i for i, x in enumerate(r) if x == R}
C = max(c)
nc = {i for i, x in enumerate(c) if x == C}
count = sum(x in nr and y in nc for x, y in s)
print(R + C - (len(nr) * len(nc) == count))

Submission status: Accepted

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


def main():
    h, w, m = map(int, input().split())
    horizontal_counts = [0] * h
    vertical_counts = [0] * w
    bombs = set()
    for _ in range(m):
        y, x = map(int, input().split())
        y -= 1
        x -= 1
        vertical_counts[x] += 1
        horizontal_counts[y] += 1
        bombs.add((y, x))

    y_max = max(horizontal_counts)
    x_max = max(vertical_counts)
    y_candidates = [y for y, count in enumerate(horizontal_counts) if count == y_max]
    x_candidates = [x for x, count in enumerate(vertical_counts) if count == x_max]

    for y in y_candidates:
        for x in x_candidates:
            if (y, x) in bombs:
                continue
            else:
                print(y_max + x_max)
                return
    print(y_max + x_max - 1)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
H, W, M = map(int, input().split())
 
HB = [0] * H
WB = [0] * W
 
place = []
 
for i in range(M):
    h, w = map(int, input().split())
    place.append((h, w,))
 
    HB[h - 1] += 1
    WB[w - 1] += 1
m_hb = max(HB)
m_wb = max(WB)
result = m_hb + m_wb
 
count = 0
for x, y in place:
    if HB[x - 1] == m_hb and WB[y - 1] == m_wb:
        count += 1


 
if HB.count(m_hb) * WB.count(m_wb) <= count:
    result -= 1
 
print(result)

Submission status: Accepted

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

for i in range(m):
    _h, _w = map(int, input().split())
    h_cnt[_h-1] += 1
    w_cnt[_w-1] += 1
    bomb.add(((_h-1),(_w-1))) #爆破対象の位置を判断

h_max = max(h_cnt)
w_max = max(w_cnt)

max_h = []
max_w = []

for i in range(h):
    if h_cnt[i] == h_max:
        max_h.append(i)

for i in range(w):
    if w_cnt[i] == w_max:
        max_w.append(i)

flg = True

for i in max_h:
    for j in max_w:
        if (i,j) not in bomb:
            flg = False
            break
print(h_max+w_max-1 if flg else h_max+w_max)

Submission status: Accepted

Solution code:
def main():
    H,W,M = map(int,input().split())
    dict_gyo = {key: 0 for key in range(H)}
    dict_retu = {key: 0 for key in range(W)}
    dict_ichi = {}
    for i in range(M):
        h,w = map(int,input().split())
        dict_gyo[h-1] += 1
        dict_retu[w-1] += 1
        dict_ichi[(h-1,w-1)] = 1

    #print(dict_gyo,dict_ichi)

    maxgyo = max(dict_gyo.values())
    maxretu = max(dict_retu.values())
    #maxgyo_k = max(dict_gyo, key = dict_gyo.get)
    #maxretu_k = max(dict_retu, key = dict_gyo.get)
    #print(maxgyo_k,maxretu_k)
    result = maxgyo + maxretu - 1
    wi = [i for i in range(W) if dict_retu[i] == maxretu]
    for h in [i for i in range(H) if dict_gyo[i] == maxgyo]:
        for w in wi:
            if (h,w) not in dict_ichi:
                result = maxgyo + maxretu
                return result

    return result




print(main())

Submission status: Accepted

Solution code:
H,W,M=map(int,input().split())
HW=[tuple(map(lambda x:int(x)-1,input().split()))for i in range(M)]
st=set(HW)
countH=[0]*H
countW=[0]*W
for h,w in HW:
    countH[h]+=1
    countW[w]+=1
maxH=max(countH)
maxW=max(countW)
candiH=[]
candiW=[]
for i in range(H):
    if maxH==countH[i]:
        candiH.append(i)
for i in range(W):
    if maxW==countW[i]:
        candiW.append(i)
count=0
ans=maxH+maxW-1
add=0
for i in candiH:
    for j in candiW:
        if (i,j) not in st:
            add=1
        if count>300000:
            break
        count+=1
print(ans+add)

Submission status: Accepted

Solution code:
H,W,M=map(int,input().split())
HW=[tuple(map(int,input().split()))for i in range(M)]
HW=[(h-1,w-1)for h,w in HW]
st=set(HW)
countH=[0]*H
countW=[0]*W
for h,w in HW:
    countH[h]+=1
    countW[w]+=1
maxH=max(countH)
maxW=max(countW)
candiH=[]
candiW=[]
for i in range(H):
    if maxH==countH[i]:
        candiH.append(i)
for i in range(W):
    if maxW==countW[i]:
        candiW.append(i)
count=0
ans=maxH+maxW-1
add=0
for i in candiH:
    for j in candiW:
        if (i,j) not in st:
            add=1
        if count>300000:
            break
        count+=1
print(ans+add)

Submission status: Accepted

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

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

x=[[0,i] for i in range(w)]
y=[[0,j] for j in range(h)]

for j,i in g:
    i-=1
    j-=1
    x[i][0]+=1
    y[j][0]+=1
    d[(j,i)]+=1


x.sort(reverse=True)
y.sort(reverse=True)
ans=0

for i in range(w):
    if i!=0 and x[i][0]<x[i-1][0]:
        break
    for j in range(h):
        if j==0 or y[j][0]==y[j-1][0]:
            if d[(y[j][1]),(x[i][1])] == 1:
                ans = x[i][0] + y[j][0] - 1
            else:
                ans = x[i][0] + y[j][0]
                print(ans)
                exit()
        else:
            break
print(ans)

Submission status: Accepted

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

for i in range(m):
    _h, _w = map(int, input().split())
    h_cnt[_h-1] += 1
    w_cnt[_w-1] += 1
    bomb.add(((_h-1),(_w-1))) #爆破対象の位置を判断

h_max = max(h_cnt)
w_max = max(w_cnt)

max_h = []
max_w = []

for i in range(h):
    if h_cnt[i] == h_max:
        max_h.append(i)

for i in range(w):
    if w_cnt[i] == w_max:
        max_w.append(i)

flg = True

for i in max_h:
    for j in max_w:
        if (i,j) not in bomb:
            flg = False
            break
print(h_max+w_max-1 if flg else h_max+w_max)

Submission status: Accepted

Solution code:
def resolve():
    H, W, M = list(map(int, input().split()))
    HW = [list(map(int, input().split())) for _ in range(M)]
    rowsum = [0 for _ in range(H)]
    colsum = [0 for _ in range(W)]
    for h, w in HW:
        rowsum[h-1] += 1
        colsum[w-1] += 1
    rmax = 0
    ridx = []
    for i, r in enumerate(rowsum):
        if rmax < r:
            rmax = r
            ridx = [i]
        elif rmax == r:
            ridx.append(i)
    maxr = [False for _ in range(H)]
    for i in ridx:
        maxr[i] = True
    
    cmax = 0
    cidx = []
    for j, c in enumerate(colsum):
        if cmax < c:
            cmax = c
            cidx = [j]
        elif cmax == c:
            cidx.append(j)
    maxc = [False for _ in range(W)]
    for i in cidx:
        maxc[i] = True
    
    cnt = 0
    for h, w in HW:
        if maxr[h-1] and maxc[w-1]:
            cnt += 1
    
    print(rmax + cmax if cnt < len(cidx)*len(ridx) else rmax + cmax - 1)

    
    


if '__main__' == __name__:
    resolve()

Submission status: Accepted

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

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

sum_row = [0]*H; sum_col = [0]*W
bomb_row = [0]*M; bomb_col = [0]*M
for i in range(M):
    h,w = map(int, input().split())
    sum_row[h-1] += 1
    sum_col[w-1] += 1
    
    bomb_row[i] = h-1
    bomb_col[i] = w-1

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

maxrow = max(sum_row)
maxrowlist = [False]*H
for row in range(H):
    if sum_row[row] == 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: Accepted

Solution code:
H, W, M = map(int, input().split())
S = [tuple(map(int, input().split())) for _ in range(M)]
ss = set(S)

Y = [0] * H
X = [0] * W

for i in range(M):
    y, x = S[i]
    y, x = y-1, x-1
    Y[y] += 1
    X[x] += 1

YM = max(Y)
XM = max(X)

YI = []
for i in range(H):
    if Y[i] == YM:
        YI.append(i)

XI = []
for i in range(W):
    if X[i] == XM:
        XI.append(i)

for y in YI:
    for x in XI:
        if (y+1, x+1) not in ss:
            print(YM+XM)
            exit()
else:
    print(YM+XM-1)

Submission status: Accepted

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=[]
bomb=set(bomb)
for i in range(1,h+1):
    if x[i]==maxx:
        r.append(i)
for j in range(1,w+1):
    if y[j]==maxy:
        c.append(j)
for j in r:
    for k in c:
        if (j,k) not in bomb:
            print(maxx+maxy)
            exit()
print(maxx+maxy-1)

Submission status: Accepted

Solution code:
h,w,m=map(int,input().split())
g=[0]*h
l=[0]*w
b=[]
for i in range(m):
  x,y=map(int,input().split())
  g[x-1]+=1
  l[y-1]+=1
  b.append((x-1,y-1))
mxg=max(g);mxl=max(l)
mg=set([i for i,x in enumerate(g) if x==mxg])
ml=set([i for i,x in enumerate(l) if x==mxl])
c=0
for x,y in b:
  if x in mg and y in ml:
    c+=1
print(mxg+mxl-(c==(len(mg)*len(ml))))

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
input = sys.stdin.readline
h,w,m=map(int, input().split())
hd=[0 for i in range(h+1)]
wd=[0 for i in range(w+1)]
hw=defaultdict(int)
hmax=0
wmax=0
for i in range(m):
    h0,w0=map(int, input().split())
    hd[h0]+=1
    hmax=max(hmax,hd[h0])
    wd[w0]+=1
    wmax=max(wmax,wd[w0])
    hw[(h0,w0)]+=1

max_h_list=[]
max_w_list=[]
for k in range(h+1):
    if hd[k]==hmax:
        max_h_list.append(k)

for k in range(w+1):
    if wd[k]==wmax:
        max_w_list.append(k)

ans=float('inf')

for x in max_h_list:
    for y in max_w_list:
        if hw[(x,y)]==1:
            ans=min(ans,1)
        else:
            ans=min(ans,0)
            break
print(hmax+wmax-ans)

Submission status: Accepted

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

H,W,M = map(int, input().split())
BH = [0 for i in range(H)]
BW = [0 for i in range(W)]
Bomb = set()
for _ in range(M):
  h,w = map(int, input().split())
  h,w = h-1,w-1
  BH[h] += 1
  BW[w] += 1
  Bomb.add((h,w))
  
BHMAX = max(BH)
BWMAX = max(BW)
HS = set()
WS = set()
for i in range(H):
  if BH[i] == BHMAX:
    HS.add(i)
for i in range(W):
  if BW[i] == BWMAX:
    WS.add(i)
    
ans = 0
cnt = 0
for h,w in Bomb:
  if h in HS and w in WS:
    ans = max(ans, BHMAX + BWMAX - 1)
    cnt += 1

if cnt < len(HS) * len(WS):
  ans = BHMAX + BWMAX
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

H,W,M = map(int,input().split())
dic = defaultdict(bool)
h_c = [ 0 for _ in range(H) ]
w_c = [ 0 for _ in range(W) ]

for _ in range(M):
  h,w = map(int,input().split())
  h -= 1
  w -= 1
  h_c[h] += 1
  w_c[w] += 1
  dic[(h,w)] = True

h_cans,w_cans = [],[]
h_max,w_max = max(h_c),max(w_c)
for i in range(H):
  if h_c[i] == h_max:
    h_cans.append(i)

for i in range(W):
  if w_c[i] == w_max:
    w_cans.append(i)

ans = h_max + w_max - 1
for h in h_cans:
  for w in w_cans:
    if not dic[(h,w)]: 
      print(ans+1)
      exit()

print(ans)

Submission status: Accepted

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

cntRow = [0] * H
cntCol = [0] * W
for h, w in bomb:
    cntRow[h] += 1
    cntCol[w] += 1

mxR = max(cntRow)
mxC = max(cntCol)

R = set([h for h in range(H) if cntRow[h] == mxR])
C = set([w for w in range(W) if cntCol[w] == mxC])

ans = mxR + mxC
if len([0 for h, w in bomb if h in R and w in C]) == len(R) * len(C):
    ans -= 1

print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
H, W, M = map(int,input().split())
q=set(tuple(map(lambda x :int(x)-1,input().split())) for _ in range(M))

X = [0] * H
Y = [0] * W

for i, j in q:
    X[i] += 1
    Y[j] += 1
    
def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]

xm = max(X)
ym = max(Y)
xms = my_index_multi(X, xm)
yms = my_index_multi(Y, ym)

ans = xm + ym - 1

bl = False
for i in xms:
    for j in yms:
        if not (i, j) in q:
            print(ans + 1)
            sys.exit()
            bl = True
            
print(ans)

Submission status: Accepted

Solution code:
H, W, M = list(map(int, input().split()))
HW = set(tuple(int(x) - 1 for x in input().split()) for _ in range(M))
h = [0] * H
w = [0] * W
for hi, wi in HW:
	h[hi] += 1
	w[wi] += 1
hm = max(h)
wm = max(w)

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

ans = hm + wm - 1
for h in hh:
	for w in ww:
		if (h, w) not in HW:
			ans += 1
			break 	
	else:
		continue
	break
print(ans)

Submission status: Accepted

Solution code:
H, W, M = map(int, input().split())
countH = [0 for h in range(H)]
countW = [0 for w in range(W)]
Bh = {}

for i in range(M):
  h, w = map(int, input().split())
  h, w = h-1, w-1
  if h in Bh:
    Bh[h].add(w)
  else:
    Bh[h] = {w}
  countH[h] += 1
  countW[w] += 1

maxh = 0
ch = []
maxw = 0
cw = []
for i in range(H):
  if maxh==countH[i]:
    ch.append(i)
  elif maxh<countH[i]:
    ch = [i]
    maxh = countH[i]
for i in range(W):
  if maxw==countW[i]:
    cw.append(i)
  elif maxw<countW[i]:
    cw = [i]
    maxw = countW[i]

#print(countH)
#print(countW)
#rint(Bh)
ans = maxh + maxw
flag = False
for h in ch:
  for w in cw:
    #print(h,w)
    if w not in Bh[h]:
      flag = True
      break
if flag:
  print(ans)
else:
  print(ans-1)

Submission status: Accepted

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def main():
	#a = int(input())
	h, w, m = tin()
	#s = input()
	hl=[0]*h
	wl=[0]*w
	is_on=set()
	for _ in range(m):
		hi,wi=tin()
		hl[hi-1]+=1
		wl[wi-1]+=1
		is_on.add(hi*mod+wi)
	max_h=max(hl)
	max_w=max(wl)
	t_return = max(hl)+max(wl)
	max_ids_hl=[i+1 for i,v in enumerate(hl) if v == max_h]
	max_ids_wl=[i+1 for i,v in enumerate(wl) if v == max_w]
	if len(max_ids_hl)*len(max_ids_wl) > len(is_on):
		return t_return
	for hi in max_ids_hl:
		for wi in max_ids_wl:
			if hi*mod + wi not in is_on:
				return t_return
	return t_return-1
	
	
		
		
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    height, width, target_count = [int(x) for x in input().split()]
    count_by_height = defaultdict(int)
    count_by_width = defaultdict(int)
    bomb_locations = set()
    for _ in range(target_count):
        h, w = [int(x) - 1 for x in input().split()]
        count_by_height[h] += 1
        count_by_width[w] += 1
        bomb_locations.add((h, w))
    max_h = max(v for v in count_by_height.values())
    max_w = max(v for v in count_by_width.values())
    max_h_index = [i for i, x in count_by_height.items() if x == max_h]
    max_w_index = [i for i, x in count_by_width.items() if x == max_w]
    no_cross = any((h, w) not in bomb_locations
                   for h in max_h_index for w in max_w_index)
    return max_h + max_w - (not no_cross)


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
H,W,M = map(int,input().split())
HW = [list(map(int, input().split())) for i in range(M)]
x = [0]*W
y = [0]*H
used = set()
for h,w in HW:
    h -= 1
    w -= 1
    y[h] += 1
    x[w] += 1
    used.add((h,w))

y_max = max(y)
x_max = max(x)
y_can = []
x_can = []
for i in range(H):
    if y[i] == y_max:
        y_can.append(i)
for i in range(W):
    if x[i] == x_max:
        x_can.append(i)

#候補地は爆弾があるり被りがあることを想定して-1しておく
ans = y_max+x_max-1
#print(ans, y_can,x_can)
flag = False
for yy in y_can:
    for xx in x_can:
        #print((yy,xx) in used,(yy,xx) ,used)
        if (yy,xx) in used:
            continue
        else:
            #候補地に爆弾がないことが確認できたので+1してループ抜け
            ans += 1
            flag = True
            break
    if flag:
        break
print(ans)

Submission status: Accepted

Solution code:
H, W, M = map(int, input().split())
Hs = [0] * H
Ws = [0] * W
boms = set()

for _ in range(M):
  h, w = map(lambda x: int(x) - 1, input().split())
  boms.add((h, w))
  Hs[h] += 1
  Ws[w] += 1
  
Hmax = max(Hs)
Wmax = max(Ws)
HMaxIndexs = set(i for i, h in enumerate(Hs) if h == Hmax)
WMaxIndexs = set(i for i, w in enumerate(Ws) if w == Wmax)

crossCount = len(HMaxIndexs) * len(WMaxIndexs)
crossBomCount = len(list(filter(lambda x: x[0] in HMaxIndexs and x[1] in WMaxIndexs, boms)))
print(Hmax + Wmax - 1) if crossCount == crossBomCount else print(Hmax + Wmax)

Submission status: Accepted

Solution code:
import sys


H, W, M = map(int, input().split())
bomb = [tuple(map(lambda x: int(x) - 1, s.split())) for s in sys.stdin.readlines()]
X = [0] * H
Y = [0] * W
for h, w in bomb:
    X[h] += 1
    Y[w] += 1
maxX = max(X)
maxY = max(Y)

R = [h for h, x in enumerate(X) if x == maxX]
C = [w for w, y in enumerate(Y) if y == maxY]

bomb = set(bomb)
for r in R:
    for c in C:
        if (r, c) not in bomb:
            print(maxX + maxY)
            exit()
print(maxX + maxY - 1)

Submission status: Accepted

Solution code:
from collections import defaultdict
h, w, m = map(int, input().split())
hw = [tuple(map(int, input().split())) for _ in range(m)]
tate, yoko = defaultdict(int), defaultdict(int)
for h, w in hw:
    tate[h] += 1
    yoko[w] += 1
tate = sorted(tate.items(), key=lambda x:x[1], reverse=True)
yoko = sorted(yoko.items(), key=lambda x:x[1], reverse=True)
tate_max, yoko_max = tate[0][1], yoko[0][1]
max_tates, max_yokos = [], []
for t in tate:
    if t[1] == tate_max:
        max_tates.append(t)
    else:
        break
for y in yoko:
    if y[1] == yoko_max:
        max_yokos.append(y)
    else:
        break

hw = set(hw)
for ta in max_tates:
    for yo in max_yokos:
        if (ta[0], yo[0]) not in hw:
            print(tate_max + yoko_max)
            exit()
print(tate_max + yoko_max - 1)

Submission status: Accepted

Solution code:
H, W, M = map(int, input().split())
boms = set()
Hs = [0] * H
Ws = [0] * W
for i in range(M):
  h, w = map(lambda x: int(x) - 1, input().split())
  boms.add((h, w))
  Hs[h] += 1
  Ws[w] += 1
  
Hmax = max(Hs)
HMaxIndexs = set()
for i, h in enumerate(Hs):
  if h == Hmax:
    HMaxIndexs.add(i)

Wmax = max(Ws)
WMaxIndexs = set()
for i, w in enumerate(Ws):
  if w == Wmax:
    WMaxIndexs.add(i)

count = 0
for bom in boms:
  if (bom[0] in HMaxIndexs) and (bom[1] in WMaxIndexs):
    count += 1
print(Hmax + Wmax - 1) if count == len(HMaxIndexs) * len(WMaxIndexs) else print(Hmax + Wmax)

Submission status: Accepted

Solution code:
import sys


H, W, M = map(int, input().split())
bomb = [tuple(map(lambda x: int(x) - 1, s.split())) for s in sys.stdin.readlines()]
X = [0] * H
Y = [0] * W
for h, w in bomb:
    X[h] += 1
    Y[w] += 1
maxX = max(X)
maxY = max(Y)

R = [h for h, x in enumerate(X) if x == maxX]
C = [w for w, y in enumerate(Y) if y == maxY]

ans = maxX + maxY
if len(R) * len(C) <= M:
    blank = set((r, c) for r in R for c in C)
    blank -= set(bomb)
    if len(blank) == 0:
        ans -= 1
print(ans)

Submission status: Accepted

Solution code:
import sys,math,collections,itertools
input = sys.stdin.readline

H,W,M=map(int,input().split())
bom = [] #ボムの場所
bomH =[0]*(H+1) 
bomW=[0]*(W+1)

for _ in range(M):
    h,w=map(int,input().split())
    bom.append((h,w))
    bomH[h]+=1
    bomW[w]+=1

#縦の爆弾と横の爆弾は独立。爆弾を置く場所に爆弾があると-1
max_bomH = max(bomH)
max_bomW = max(bomW)

cnt = 0#爆弾が置かれた場所のbomH,bomWがともにMaxの場所の数
for h,w in bom:
    if bomH[h]==max_bomH and bomW[w] == max_bomW:
        cnt += 1

print(max_bomH +max_bomW -(cnt == bomH.count(max_bomH) * bomW.count(max_bomW)))

Submission status: Accepted

Solution code:
from collections import defaultdict as dd
def solve():
  h, w, m = map(int, input().split())
  
  targets = set()
  rows = dd(int)
  cols = dd(int)
  for _ in range(m):
    r, c = map(int, input().split())
    rows[r] += 1
    cols[c] += 1
    targets.add((r, c))
    
  best_rows = []
  best_cols = []
  best_row = max(rows.values())
  best_col = max(cols.values())
  best = best_row + best_col - 1
  for k, v in rows.items():
    if v == best_row:
      best_rows.append(k)
  for k, v in cols.items():
    if v == best_col:
      best_cols.append(k)
  
  for row in best_rows:
    for col in best_cols:
      if (row, col) not in targets:
        print (best + 1)
        return
  print (best)
solve()

Submission status: Accepted

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)
bomb = []
 
for i in range(M):
    h,w = map(int,readline().split())
    bomb.append((h,w))
    h_cnt[h] += 1 
    w_cnt[w] += 1
    
maxh = max(h_cnt)
maxw = max(w_cnt)
maxhCnt = h_cnt.count(maxh)
maxwCnt = w_cnt.count(maxw)

total = maxhCnt*maxwCnt
ans = maxh+maxw
cnt = 0
for h,w in bomb:
    if h_cnt[h] == maxh and w_cnt[w] == maxw:
       cnt += 1

print(ans-1 if cnt == total else ans)

Submission status: Accepted

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(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 not in hw[x]:
            print(hm + wm)
            exit()
else:
    print(hm + wm - 1)