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: Wrong Answer

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

hcount = [0 for i in range(H+1)]
wcount = [0 for i in range(W+1)]
hw = [list(map(int,input().split())) for i in range(M)]

for i in range(M):
    h,w = hw[i]
    hcount[h]+=1
    wcount[w]+=1

ansh = max(hcount)
answ = max(wcount)#おいてない前提
counth=0
countw=0
for i in range(H+1):
    if hcount[i]==ansh:
        counth+=1

for i in range(W):
    if wcount[i]==answ:
        countw+=1

countM = 0
for i in range(M):
    if hcount[hw[i][0]]+wcount[hw[i][1]] == ansh+answ:
        countM+=1

print(ansh+answ if countM < counth*countw else ansh +answ -1)

Submission status: Wrong Answer

Solution code:
H, W, M = map(int, input().split())
hn = {}
wn = {}
flag ={}
for i in range(M):
  h, w = map(int, input().split())
  if (h,w) not in flag: 
    if h in hn:
      hn[h] += 1
    else:
      hn[h] = 1
    if w in wn:
      wn[w] += 1
    else:
      wn[w] = 1
  flag[(h,w)] = 1

hrank = sorted(hn.items(), key=lambda t:-t[1])
wrank = sorted(wn.items(), key=lambda t:-t[1])

#print(hrank,wrank)
if (hrank[0][0],wrank[0][0]) in flag:
  print(hrank[0][1]+wrank[0][1]-1)
else:
  print(hrank[0][1]+wrank[0][1])

Submission status: Wrong Answer

Solution code:
import copy
import sys
H, W, M= map(int,input().split())
height = [0]*H
width = [0]*W
object = []
for s in sys.stdin.readlines():
    h, w = map(int, s.split())
    height[h-1] += 1
    width[w-1] += 1
    object.append([h-1, w-1])
h_max = max(height)
w_max = max(width)
cand_w = []
cand_h = []
for i in range(W):
   if width[i] == w_max:
      cand_w.append(i)
for i in range(H):
   if height[i] == h_max:
      cand_h.append(i)
ans = h_max + w_max - 1
p = len(cand_h)*len(cand_w)
if M >= p:
   for i in range(len(cand_h)):
      for j in range(len(cand_w)):
         if not [i,j] in object:
            ans += 1
else:
   ans += 1
print(ans)

Submission status: Wrong Answer

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

boms=[[0 for j in range(W)] for i in range(H)]
for i in range(M):
  h,w=map(int,input().split())
  boms[h-1][w-1]=1
  row_boms[h-1]+=1
  col_boms[w-1]+=1

row_max=max(row_boms)
col_max=max(col_boms)

rowmax_h=[]
colmax_w=[]
for i in range(H):
  if row_boms[i]==row_max:
    rowmax_h+=[i]
for i in range(W):
  if col_boms[i]==col_max:
    colmax_w+=[i]
rows=len(rowmax_h)
cols=len(colmax_w)
tf=0
for i in range(rows):
  for j in range(cols):
    if boms[i][j]==0:
      tf=1
      break
if tf==1:
  print(row_max+col_max)
else:
  print(row_max+col_max-1)

Submission status: Wrong Answer

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

hc = [0] * H
wc = [0] * W
#hwc = [[0] * W for _ in range(H)]
for _ in range(M):
    h, w = map(lambda x: int(x) - 1,input().split())
    hc[h] += 1
    wc[w] += 1
    #hwc[h][w] += 1

result = 0
for h in range(H):
    for w in range(W):
        #result = max(result, hc[h] + wc[w] - hwc[h][w])
        result = max(result, hc[h] + wc[w])
print(result)

Submission status: Wrong Answer

Solution code:
import collections

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

gyou = collections.defaultdict(int)
retu = collections.defaultdict(int)
q = collections.deque()

for i in range(m):
    y,x = map(int,input().split())
    gyou[y] += 1
    retu[x] += 1
    q.append((y,x))

ans = max(gyou.values()) + max(retu.values())

y = max(gyou)
x = max(retu)
if (y,x) in q:
    ans -=1

print(ans)

Submission status: Wrong Answer

Solution code:
import copy
import math
import time
import statistics
import math
import itertools
import bisect
import sys
from decimal import *
from collections import deque


def get_int():
    return int(input())

def get_string():
    return input()

def get_int_list():
    return [int(x) for x in input().split()]

def get_string_list():
    return input().split()

def get_int_multi():
    return map(int, input().split())

def get_string_char_list():
    return list(str(input()))

# print("{} {}".format(a, b))
# a_list = [0] * a
sys.setrecursionlimit(10 ** 6)

def main():
    start = time.time()

    h, w, m = get_int_list()

    a = [0] * (h+1)
    b = [0] * (w+1)
    c = []

    for _ in range(m):
        h2, w2 = get_int_multi()
        c.append([h2,w2])
        a[h2] += 1
        b[w2] += 1

    ans = 0

    wk = -1
    max_r = -1
    for i in range(h+1):
        if wk < a[i]:
            wk = a[i]
            max_r = i

    for c_idx in range(len(c)):
        if c[c_idx][0] == max_r:
            ans += 1
            b[c[c_idx][1]] -= 1


    wk = -1
    for i in range(w+1):
        if wk < b[i]:
            wk = b[i]


    print(ans+wk)

    #print(time.time() - start)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import collections

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

h = []
w = []
 
for _ in range(M):
    hi, wi = list(map(int, input().split()))
    h.append(hi)
    w.append(wi)

#print(h)
#print(w)


# 重複要素のmaxを求める
hmax = collections.Counter(h).most_common()[0]

# 重複要素のmaxを求める
wmax = collections.Counter(w).most_common()[0]

#print(hraw)
#print(wraw)

bomb = False
for i in range(len(h)):
    if h[i]==hmax[0] and w[i]==wmax[0]:
        bomb = True
        break

cnt = hmax[1] + wmax[1]
if bomb:
    cnt -= 1

print(cnt)

Submission status: Wrong Answer

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

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

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

hh,ww=[],[]

for i in range(1,h+1):
  if hlist[i]==hcnt:
    hh+=[i]

for i in range(1,w+1):
  if wlist[i]==wcnt:
    ww+=[i]

for i in cnt:
  if i[0] not in hh and i[1] not in ww:
    print(hcnt+wcnt)
    exit()
  
print(hcnt+wcnt-1)

Submission status: Wrong Answer

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

boms=[[0 for j in range(W)] for i in range(H)]
for i in range(M):
  h,w=map(int,input().split())
  boms[h-1][w-1]=1
  row_boms[h-1]+=1
  col_boms[w-1]+=1

row_max=max(row_boms)
col_max=max(col_boms)

rowmax_h=[]
colmax_w=[]
for i in range(H):
  if row_boms[i]==row_max:
    rowmax_h+=[i]
for i in range(W):
  if col_boms[i]==col_max:
    colmax_w+=[i]
rows=len(rowmax_h)
cols=len(colmax_w)
tf=0
for i in range(rows):
  for j in range(cols):
    if boms[i][j]==0:
      tf=1
      break
if tf==1:
  print(row_max+col_max)
else:
  print(row_max+col_max-1)

Submission status: Wrong Answer

Solution code:
H, W, M = list(map(int, input().split()))
bombs = []
h = {}
w = {}
for i in range(M):
    x, y = list(map(int, input().split()))
    if x in h:
        h[x] += 1
    else:
        h[x] = 1
    if y in w:
        w[y] += 1
    else:
        w[y] = 1
    bombs.append([x, y])

ans = 0
max = 0
cnt = 0
for x in range(H, 0, -1):
    if x not in h:
        break
    for y in range(W, 0, -1):
        if y not in w:
            break
        ans = h[x] + w[y]
        if [x, y] in bombs:
            ans -= 1
        if ans > max:
            max = ans
    if cnt > 10:
        break
    cnt += 1

print(max)

Submission status: Wrong Answer

Solution code:
def E():
    h,w,m = list(map(int, input().split()))
    h_l = []
    w_l = []
    bomb = []
    for _ in range(m):
        h_t,w_t = list(map(int, input().split()))
        h_l.append(h_t)
        w_l.append(w_t)
        bomb.append(str(h_t-1)+":"+str(w_t-1))
    import collections
    c_h = collections.Counter(h_l).most_common()
    c_w = collections.Counter(w_l).most_common()
    max_h = c_h[0][1]
    max_w = c_w[0][1]
    flg = -1
    for i in range(len(c_h)):
        if c_h[i][1] != max_h:
            break
        for j in range(len(c_w)):
            if c_w[j][1] != max_w:
                break
            if str(c_h[i][0])+":"+str(c_w[j][0]) in bomb:
                flg = 0
                break
    print(max_h + max_w + flg)
E()

Submission status: Wrong Answer

Solution code:
import copy
import sys
H, W, M= map(int,input().split())
height = [0]*H
width = [0]*W
object = []
for s in sys.stdin.readlines():
    h, w = map(int, s.split())
    height[h-1] += 1
    width[w-1] += 1
    object.append([h-1, w-1])
h_max = max(height)
w_max = max(width)
cand_w = []
cand_h = []
for i in range(W):
   if width[i] == w_max:
      cand_w.append(i)
for i in range(H):
   if height[i] == h_max:
      cand_h.append(i)
ans = h_max + w_max - 1
p = len(cand_h)*len(cand_w)
if M > p:
   for i in range(len(cand_h)):
      for j in range(len(cand_w)):
         if not [i,j] in object:
            ans += 1
else:
   ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
H,W,M = list(map(int , input().split()))
HList = []
WList = []
for q in range(M):
    x,y=[int(q) for q in input().split()]
    HList.append(x)
    WList.append(y)
HiH = 0
hh = 0
hhh = []
HiW = 0
ww = 0
www = []
Hi = 0
for i in range (M):
    hl = HList.count(1+i)
    if (hl>HiH):
        HiH = hl
        hh = 1+i
        hhh.clear()
        hhh.append(hh)
    elif (hl == HiH):
        hhh.append(hl)
for j in range (M):
    wl = WList.count(1+j)
    if (wl>HiW):
        HiW = wl
        ww = 1+j
        www.clear()
        www.append(ww)
    elif (wl == HiW):
        www.append(wl)
Hi = HiH + HiW
wwww = len(www)
hhhh = len(hhh)
lose = 1

print(Hi-lose)

Submission status: Wrong Answer

Solution code:
import collections
h, w, m = map(int, input().split())
L_g = []
L_r = []
L = []
for i in range(m):
    a, b = map(int, input().split())
    L_g.append(a)
    L_r.append(b)
    L.append((a, b))
c1 = collections.Counter(L_g)
c2 = collections.Counter(L_r)

L1 = c1.most_common()
L2 = c2.most_common()

s = L1[0][1]
bL1 = []
d = 0
for i in range(len(L1)):
    if L1[i][1] == s:
        bL1.append(L1[i])
        d += 1
        if d == 2:
          break
    else:
        break
s = L2[0][1]
bL2 = []
d = 0
for i in range(len(L2)):
    if L2[i][1] == s:
        bL2.append(L2[i])
        d += 1
        if d == 2:
          break
    else:
        break
bL = []
for i in range(len(bL1)):
    for j in range(len(bL2)):
        bL.append((bL1[i][0], bL2[j][0]))
flag = 0
for e in bL:
    if e not in L:
        bom = e
        flag = 1
        break
if flag == 0:
    bom = bL[0]

t = L1[0][1]+L2[0][1]

for e in L:
    if e == bom:
        t -= 1
print(t)

Submission status: Wrong Answer

Solution code:
H,W,M=map(int,input().split())
HS=[0 for _ in range(H)]
WS=[0 for _ in range(W)]
S=[[0 for _ in range(W)] for _ in range(H)]
answers=[]
for _ in range(M):
    h,w=map(int,input().split())
    answers.append([h-1,w-1])
    S[h - 1][w - 1] += 1
    HS[h-1]+=1
    WS[w-1]+=1
placeh=HS.index(max(HS))
placew=WS.index(max(WS))
answer=max(HS)+max(WS)
if S[placeh][placew]>=1:
    answer-=1
print(answer)

Submission status: Wrong Answer

Solution code:
H, W, M = map(int, input().split())
Hdst = [0]*H
Wdst = [0]*W
DST = [[0] * 2 for i in range(M)]
for i in range(M):
    hi, wi = map(int, input().split())
    Hdst[hi-1] += 1
    Wdst[wi-1] += 1
    DST[i] = [hi-1, wi-1]

Hdstmax = max(Hdst)
Wdstmax = max(Wdst)
Hbomb = [i for i, x in enumerate(Hdst) if x == Hdstmax]
Wbomb = [i for i, x in enumerate(Wdst) if x == Wdstmax]


if len(Hbomb)*len(Wbomb) == M:
    print(Hdstmax + Wdstmax)
else:
    print(Hdstmax + Wdstmax - 1)

Submission status: Wrong Answer

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

lh = []
lw = []

for i in range(m):
  h1,w1 = map(int, input().split())
  lh.append(h1)
  lw.append(w1)

if h == 1 or w == 1:
  print(m)
else:
  from collections import Counter as co
  ch=co(lh).most_common()

  tmp = ch[0][0]
  lh_c = [i for i, x in enumerate(lh) if x == tmp]
  for j in sorted(lh_c, reverse=True):
    lw.pop(j)
  if len(lw) == 0:
    print(ch[0][1])
  else:
    cw = co(lw).most_common()
    print(ch[0][1]+cw[0][1])

Submission status: Wrong Answer

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

MAX_H = max(LH)
MAX_W = max(LW)

MAX_SH = {i:1 for i in range(H) if LH[i] == MAX_H}
MAX_SW = {i:1 for i in range(W) if LW[i] == MAX_W}

flag = 1

for h, w in L:
    if  h in MAX_SH and w in MAX_SW:
        MAX_SH[h] = 0
        MAX_SW[w] = 0

ans = MAX_H + MAX_W

if not (1 in MAX_SH.keys() or 1 in MAX_SW.keys()):
    ans -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
import collections

h,w,m = map(int,input().split())
hS = []
wS = []
S = set()

for i in range(m):
    a = list(map(int,input().split()))
    S.add(str(a[0]) + '-' + str(a[1]))
    hS.append(a[0])
    wS.append(a[1])
    

ans = 0

ch = collections.Counter(hS)
cw = collections.Counter(wS)

#print(ch.most_common(1))
chm = ch.most_common(1)
#print(cw.most_common(1))
cwm = cw.most_common(1)
if str(chm[0][0]) + '-' + str(cwm[0][0]) in S:
    print(chm[0][1]+cwm[0][1] -1)
else:
    print(chm[0][1]+cwm[0][1])

Submission status: Wrong Answer

Solution code:
H,W,M = map(int,input().split())
T = [0] * M
H_cnt = {}
W_cnt = {}
HW = {}
for i in range(M):
    h,w = map(int,input().split())
    if not h in H_cnt:
        H_cnt[h] = 0
    if not w in W_cnt:
        W_cnt[w] = 0
    H_cnt[h] += 1
    W_cnt[w] += 1
    HW[(h,w)] = 1

HL = [[h,cnt] for h,cnt in H_cnt.items()]
WL = [[w,cnt] for w,cnt in W_cnt.items()]

HL.sort(key=lambda x: x[1],reverse=True)
WL.sort(key=lambda x: x[1],reverse=True)

HS = {}
max_cnth = 0
for h,cnt in HL:
    if max_cnth<=cnt:
        HS[h] = cnt
        max_cnth = max(max_cnth,cnt)
        continue
    break

WS = {}
max_cntw = 0
for w,cnt in WL:
    if max_cntw<=cnt:
        WS[w] = cnt
        max_cntw = max(max_cntw,cnt)
        continue
    break

cnt = 0
ans = 0

# ここの判定を高速にする
cross_cnt = 0
for hw in HW.keys():
    h,w = hw
    if h in HS and w in WS:
        cross_cnt+=1
if max_cnth * max_cntw==cross_cnt:
    print(max_cnth+max_cntw)
    exit()
print(max_cnth+max_cntw-1)


#for h,hcnt in HS:
#    for w,wcnt in WS:
#        if (h,w) in HW:
#            cnt = hcnt+wcnt-1
#            ans = max(ans,cnt)
#            continue
#        cnt = hcnt+wcnt
#        ans = max(ans,cnt)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from collections import Counter as C

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

hs, ws = [], []
hw = []

for i in range(m):
    hi, w = map(int, input().split())
    hs.append(hi)
    ws.append(w)
    hw.append([hi, w])


ch = C(hs).most_common()
cw = C(ws).most_common()
anss = []
pre_ans = -float('inf')
mrow = ch[0][0]
mcow = cw[0][0]

for num1, num2 in zip(ch, cw):
    row, cow = num1[0], num2[0]
    ans = num1[1] + num2[1]
    if [row, cow] in hw:
        ans -= 1
    anss.append(ans)
    # if (mrow > row and mcow > cow):
    #    break


print(max(anss))

Submission status: Wrong Answer

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


def main():
    [h, w, m] = [int(e) for e in si().split()]
    bh, bw, g = dict(), dict(), dict()
    for i in range(1, h+1):
        bh[i] = 0
    for j in range(1, w+1):
        bw[j] = 0
    mh, mw, mr, mc, ans = 0, 0, 0, 0, 0
    for _ in range(m):
        [r, c] = [int(e) for e in si().split()]
        g[(r, c)] = 1
        bh[r] += 1
        bw[c] += 1
        if bh[r] > mh:
            mh = bh[r]
            mr = r
        if bw[c] > mw:
            mw = bw[c]
            mc = c
        if (mr, mc) not in g:
            ans = max(ans, mh+mw)
        else:
            ans = max(ans, mh+mw-g[(mr, mc)])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
h,w,m=map(int,input().split())
bom=[[0 for i in range(w)] for j in range(h)]
hl=[0]*h
wl=[0]*w

for i in range(m):
    ha,wa=map(int,input().split())
    bom[ha-1][wa-1]=1
    hl[ha-1]+=1
    wl[wa-1]+=1

hm=max(hl)
wm=max(wl)
hi=[i for i, v in enumerate(hl) if v == hm]
wi=[i for i, v in enumerate(wl) if v == wm]

ans=hm+wm-1

if len(hi)*len(wi)>=3*(10**5):
    ans+=1
else:
    for i in hi:
        for j in wi:
            if bom[i][j]==0:
                ans+=1
                break
        else:
            continue
        break


print(ans)

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    H, W, M = in_nn()

    hcnt = [0] * H
    wcnt = [0] * W

    hw = []

    for i in range(M):
        h, w = in_nn()
        h, w = h - 1, w - 1
        hw.append((h, w))

    for i in range(M):
        h, w = hw[i]
        hcnt[h] += 1
        wcnt[w] += 1

    max_h = max(hcnt)
    max_w = max(wcnt)

    ans = max_h + max_w - 1

    h_index = []
    w_index = []

    for i in range(H):
        if hcnt[i] == max_h:
            h_index.append(i)

    for i in range(W):
        if wcnt[i] == max_w:
            w_index.append(i)

    hw_set = set(hw)
    # print(h_index)
    # print(w_index)

    hn = len(h_index)
    wn = len(w_index)

    f = False
    for i in range(hn):
        for j in range(wn):
            if (h_index[i], w_index[j]) not in hw_set:
                f = True

        if f or hn * i > 10**6:
            ans += 1
            break

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
H,W,M = map(int,input().split())
T = [0] * M
H_cnt = {}
W_cnt = {}
HW = {}
for i in range(M):
    h,w = map(int,input().split())
    if not h in H_cnt:
        H_cnt[h] = 0
    if not w in W_cnt:
        W_cnt[w] = 0
    H_cnt[h] += 1
    W_cnt[w] += 1
    HW[(h,w)] = 1

HL = [[h,cnt] for h,cnt in H_cnt.items()]
WL = [[w,cnt] for w,cnt in W_cnt.items()]

HL.sort(key=lambda x: x[1],reverse=True)
WL.sort(key=lambda x: x[1],reverse=True)

HS = {}
max_cnth = 0
for h,cnt in HL:
    if max_cnth<=cnt:
        HS[h] = cnt
        max_cnth = max(max_cnth,cnt)
        continue
    break

WS = {}
max_cntw = 0
for w,cnt in WL:
    if max_cntw<=cnt:
        WS[w] = cnt
        max_cntw = max(max_cntw,cnt)
        continue
    break

cnt = 0
ans = 0

# ここの判定を高速にする
cross_cnt = 0
for hw in HW.keys():
    h,w = hw
    if h in HS and w in WS:
        cross_cnt+=1
if max_cnth * max_cntw==cross_cnt:
    print(max_cnth+max_cntw)
    exit()
print(max_cnth+max_cntw-1)


#for h,hcnt in HS:
#    for w,wcnt in WS:
#        if (h,w) in HW:
#            cnt = hcnt+wcnt-1
#            ans = max(ans,cnt)
#            continue
#        cnt = hcnt+wcnt
#        ans = max(ans,cnt)

print(ans)

Submission status: Wrong Answer

Solution code:
H, W, M = map(int, input().split())
hn = {}
wn = {}
flag ={}
for i in range(M):
  h, w = map(int, input().split())
  if h in hn:
    hn[h] += 1
  else:
    hn[h] = 1
  if w in wn:
    wn[w] += 1
  else:
    wn[w] = 1
  flag[(h,w)]=1
hrank = sorted(hn.items(), key=lambda t:-t[1])
wrank = sorted(wn.items(), key=lambda t:-t[1])

#print(hrank,wrank)
if (hrank[0][0],wrank[0][0]) in flag:
  print(hrank[0][1]+wrank[0][1]-1)
else:
  print(hrank[0][1]+wrank[0][1])

Submission status: Wrong Answer

Solution code:
h,w,m=map(int,input().split())
H=[0]*h
W=[0]*w
M=[list(map(int,input().split())) for _ in range(m)]
for y,x in M:
  H[y-1]+=1
  W[x-1]+=1
mh=max(H)
mw=max(W)
Y=[]
X=[]
for y in H:
  if y==mh:
    Y.append(y)
for x in W:
  if x==mw:
    X.append(x)
a=-1
for y,x in M:
  if y in Y:
    if x not in X:
      a=0
      break
print(mh+mw+a)

Submission status: Wrong Answer

Solution code:

from collections import defaultdict

H,W,M=map(int,input().split())
AB=[tuple(map(int,input().split()))for i in range(M)]
AB=[(a-1,b-1)for a,b in AB]
st=set(AB)

Wc=defaultdict(lambda :0)
Hc=defaultdict(lambda :0)
for a,b in AB:
    Hc[a]+=1
    Wc[b]+=1

Hm=max(Hc.values())
for i in Hc.keys():
    if Hc[i]==Hm:
        x=i
        break
Wm=max(Wc.values())
for j in Wc.keys():
    if Wc[j]==Wm:
        y=j
        break

'''
ans=0
for i in Hc.keys():
    for j in Wc.keys():
        add = -1 if (i,j) in st else 0
        tmp= Hc[i] + Wc[j] + add
        ans=max(ans,tmp)
'''
ans=Hc[x] + Wc[y] +(-1 if (x,y) in st else 0)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy
h,w,m = map(int,input().split())
hight = [0]*h
width = [0]*w
matrix =[0]*h
for i in range(h):
    matrix[i] = [0]*w
for i in range(m):
    hi,wi = map(int,input().split())
    hight[hi-1]+=1
    width[wi-1]+=1
    matrix[hi-1][wi-1] += 1
# print(matrix)
max_vh = numpy.argmax(hight)
max_vw = numpy.argmax(width)
print(hight[max_vh]+width[max_vw]-matrix[max_vh][max_vw])


# 3 2 2
# 3 2
# 1 2

Submission status: Wrong Answer

Solution code:
H,W,M=map(int,input().split())
h=[]
w=[]
s=set([])
for i in range(M):
    a,b=map(int,input().split())
    h.append(a)
    w.append(b)
    s.add((a,b))

h_dict={}
w_dict={}
for i in range(M):
    if h[i] not in h_dict:
        h_dict[h[i]]=1
    else:
        h_dict[h[i]]+=1
for i in range(M):
    if w[i] not in w_dict:
        w_dict[w[i]]=1
    else:
        w_dict[w[i]]+=1

h_dict=sorted(h_dict.items(), key=lambda x: -x[1])
w_dict=sorted(w_dict.items(), key=lambda x: -x[1])

if (h_dict[0][0],w_dict[0][0]) in s:
    print(h_dict[0][1]+w_dict[0][1]-1)
else:
    print(h_dict[0][1]+w_dict[0][1])

Submission status: Wrong Answer

Solution code:
H, W, M = map(int, input().split())
bH = [0 for _ in range(H)]
bW = [0 for _ in range(W)]
bom = []
for _ in range(M):
  h, w = map(int, input().split())
  bom.append([h, w])
  bH[h-1] = bH[h-1] + 1
  bW[w-1] = bW[w-1] + 1
mH = max(bH)
mW = max(bW)
if [bH.index(mH)+1, bW.index(mW)+1] in bom:
  print(mH + mW - 1)
else:
  print(mH + mW)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline


def main():
    H, W, M = map(int, input().split())
    row = [0] * (H)
    col = [0] * (W)
    bomb = set()
    for _ in range(M):
        h, w = map(int, input().split())
        bomb.add((h, w))
        row[h-1] += 1
        col[w-1] += 1
    row_max = max(row)
    row_index_list = [i for i, v in enumerate(row) if v == row_max]
    col_max = max(col)
    col_index_list = [i for i, v in enumerate(col) if v == col_max]
    ans = row_max + col_max - 1
    for x in row_index_list:
        for y in col_index_list:
            if not (x + 1, y + 1) in bomb:
                ans += 1
                break
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

maxr, maxc = Counter(), Counter()

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

res = 0

for r in maxr.keys():
    for c in maxc.keys():
        res = max(res, maxr[r]+maxc[c] - 1)
print(res)

Submission status: Wrong Answer

Solution code:
#E
import numpy as np
H,W,M=map(int,input().split())
arr=np.array([[0 for _ in range(W)] for i in range(H)])
h_arr=[]
w_arr=[]
for _ in range(M):
    h,w=map(int,input().split())
    arr[h-1][w-1]=int(1)
h_sum=np.sum(arr,axis=1)
w_sum=np.sum(arr,axis=0)

mh=max(h_sum)
mw=max(w_sum)

h=list(h_sum).index(mh)
w=list(w_sum).index(mw)
ans=mh+mw
if arr[h][w]==1:
    ans-=1
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from collections import Counter as C

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

hs, ws = [], []
hw = []

for i in range(m):
    hi, w = map(int, input().split())
    hs.append(hi)
    ws.append(w)
    hw.append([hi, w])


ch = C(hs).most_common()
cw = C(ws).most_common()
anss = []
pre_ans = -float('inf')
mrow = ch[0][0]
mcow = cw[0][0]

for num1, num2 in zip(ch, cw):
    row, cow = num1[0], num2[0]
    # if (mrow > row and mcow > cow):
    #    break
    ans1 = hs.count(row)
    ans2 = ws.count(cow)
    ans = ans1 + ans2

    if [row, cow] in hw:
        ans -= 1

    anss.append(ans)


print(max(anss))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# Input#
# n = int(input())
h, w, m = map(int, input().split())
tar_list = []

h_count = [0] * (h + 1)
w_count = [0] * (w + 1)

h_max = 0
w_max = 0

h_maxlist = []
w_maxlist = []

ans = 0
ans_tmp = 0

for i in range(m):
    h_in, w_in = map(int, input().split())

    h_count[h_in] += 1
    if h_count[h_in] > h_max:
        h_max = h_count[h_in]
        h_maxlist = []
    if h_count[h_in] == h_max:
        h_maxlist.append(h_in)
        tar_list.append((h_in, w_in))

    w_count[w_in] += 1
    if w_count[w_in] > w_max:
        w_max = w_count[w_in]
        w_maxlist = []
    if w_count[w_in] == w_max:
        w_maxlist.append(w_in)
        tar_list.append((h_in, w_in))


for i in range(len(h_maxlist)):
    tar_h = h_maxlist[i]
    for j in range(len(w_maxlist)):
        tar_w = w_maxlist[j]
        if (tar_h, tar_w) in tar_list:
            ans_tmp = h_count[tar_h] + w_count[tar_w] - 1
            ans = max(ans, ans_tmp)
        else:
            ans_tmp = h_count[tar_h] + w_count[tar_w]
            ans = max(ans, ans_tmp)


print("{}".format(ans))

Submission status: Wrong Answer

Solution code:
import collections
H, W, M = map(int,input().split())
xy = [map(int, input().split()) for j in range(M)]
x, y = [list(i) for i in zip(*xy)]
x_d = collections.Counter(x)
y_d = collections.Counter(y)

x_max = x_d.most_common()[0][1]
y_max = y_d.most_common()[0][1]

a = x_d.most_common()[0][0]
b = y_d.most_common()[0][0]

c = [i for i , n in enumerate(x) if n == a]
d = [i for i , n in enumerate(y) if n == b]

if set(c)& set(d) == {}:
    ans = x_max+y_max
else:
    ans = x_max+y_max - 1

print(ans)

Submission status: Wrong Answer

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

def main():
    inputs = list(map(lambda x: int(x), input().split(' ')))
    
    height = {}
    width = {}
    bombs = []

    for i in range(inputs[2]):
        h, w = tuple(map(lambda x :int(x), input().split(' ')))
        bombs.append([h, w])
        if not h in height:
            height[h] = 0
        if  not w in width:
            width[w] = 0
        height[h] += 1
        width[w] += 1

    max_h = max(height, key=height.get)
    max_w = max(width, key=width.get)
    if [max_h, max_w] in bombs:
        print(height[max_h] + width[max_w] - 1)
    else:
        print(height[max_h] + width[max_w])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

Hm=max(Hs)
Wm=max(Ws)
Hi=[]
for i in range(H):
    if Hs[i]==Hm:
        Hi+=[i]

Wi=[]
for i in range(W):
    if Ws[i]==Wm:
        Wi+=[i]

result=Hm+Wm-1
for i in Hi:
    for j in Wi:
        if not([Hm,Wm] in B):
            result+=1
            break
print(result)

Submission status: Wrong Answer

Solution code:
h,w,m = map(int,input().split())
hli = [0]*h
wli = [0]*w
li = []
for i in range(m):
    a,b = map(int,input().split())
    li.append([a,b])
    hli[a-1] += 1
    wli[b-1] += 1
a = max(hli)
b = max(wli)
ans1 = hli.index(a) + 1
ans2 = wli.index(b) + 1
hli2 = []
wli2 = []
if m == 1:
    print(1)
    exit()
for i in range(h):
    if hli[i] == a:
        hli2.append(a)
for i in range(w):
    if wli[i] == b:
        wli2.append(b)
for i in range(len(hli2)):
    for j in range(len(wli2)):
        if [hli2[i]+1,wli2[j]+1] not in li:
            print(a + b)
            exit()
print(a + b - 1)

Submission status: Wrong Answer

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

boms=[[0 for j in range(W)] for i in range(H)]
for i in range(M):
  h,w=map(int,input().split())
  boms[h-1][w-1]=1
  row_boms[h-1]+=1
  col_boms[w-1]+=1

row_max=max(row_boms)
col_max=max(col_boms)

rowmax_h=[]
colmax_w=[]
for i in range(H):
  if row_boms[i]==row_max:
    rowmax_h+=[i]
for i in range(W):
  if col_boms[i]==col_max:
    colmax_w+=[i]
rows=len(rowmax_h)
cols=len(colmax_w)
for i in range(rows):
  for j in range(cols):
    if boms[i][j]==0:
      tf=1
else:
  tf=0
if tf==1:
  print(row_max+col_max)
else:
  print(row_max+col_max-1)

Submission status: Wrong Answer

Solution code:
#from collections import deque

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

cnt_h = [0] * h
cnt_w = [0] * w

for h1,w1 in hw:
    cnt_h[h1-1] += 1
    cnt_w[w1-1] += 1

lh = []
lw = []

for i,e in enumerate(cnt_h):
    lh.append((e,i))

for i,e in enumerate(cnt_w):
    lw.append((e,i))

lh.sort(reverse = True)
lw.sort(reverse = True)

mh = lh[0][0]
mw = lw[0][0]

idx_h = 0
idx_w = 0

cnt = 0
for i,j in lh:
    if i == mh:
        cnt += 1
    else:
        break

idx_h = cnt

cnt = 0
for i,j in lw:
    if i == mw:
        cnt += 1
    else:
        break

idx_w = cnt

ans = mh + mw

v = [[False] * w for _ in range(h)]
for h1,w1 in hw:
    v[h1-1][w1-1] = True

if (idx_h-1) * (idx_w-1) > m:
    print(ans)
    exit()

for i in range(idx_h):
    for j in range(idx_w):
        if not v[lh[i][1]][lw[j][1]]:
            print(ans)
            exit()

Submission status: Wrong Answer

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
h,w,m=map(int,input().split())
l={}
u={}
hw=[list(map(int,input().split())) for _ in range(m)]
sl=set()
su=set()
for i in range(m):
    hr,wr=hw[i]
    if hr-1 in u:
        u[hr-1]+=1
    else:
        u[hr-1]=1
    if wr-1 in l:
        l[wr-1]+=1
    else:
        l[wr-1]=1
    su.add(hr-1)
    sl.add(wr-1)
resl=0
resu=0
cl=[]
cu=[]
for i in l:
    if resl<l[i]:
        resl=l[i]
        cl=[i]
    elif resl==l[i]:
        cl.append(i)
for i in u:
    if resu<u[i]:
        resu=u[i]
        cu=[i]
    elif resu==u[i]:
        cu.append(i)
ans=0
for i in cu:
    for j in cl:
        if i in su and j in sl:
            res=l[j]+u[i]-1
        else:
            res=l[j]+u[i]
        ans=max(res,ans)
print(ans)