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:
import sys
from statistics import mode

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

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

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

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

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

if m==1:
    print('1')
    sys.exit()

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

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

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

Submission status: Wrong Answer

Solution code:
h,w,m=map(int,input().split())
import collections
h2l=[]
w2l=[]
for i in range(m):
    h2,w2=map(int,input().split())
    h2l.append(h2)
    w2l.append(w2)

ch=collections.Counter(h2l)
cw=collections.Counter(w2l)

ans=0
chcnt=1
cwcnt=1
chl=[]
cwl=[]
chl.append(ch.most_common()[0][0])
cwl.append(cw.most_common()[0][0])
for i in range(len(ch)-1):
    if ch.most_common()[i][1]==ch.most_common()[i+1][1]:
        chl.append(ch.most_common()[i+1][0])
        chcnt+=1
    else:
        break
for i in range(len(cw)-1):
    if cw.most_common()[i][1]==cw.most_common()[i+1][1]:
        cwl.append(cw.most_common()[i+1][0])
        cwcnt+=1
    else:
        break

for i in range(chcnt):
    for j in range(cwcnt):
        ansx=ch.most_common()[i][0]
        ansy=cw.most_common()[j][0]
        ansh=ch.most_common()[i][1]
        answ=cw.most_common()[j][1]
        for k in range(m):
            if ansx==h2l[k] and ansy==w2l[k]:
                ans=0
                break
            ans=ansh+answ

print(ans if ans>0 else ansh+answ-1)

Submission status: Wrong Answer

Solution code:
h,w,m=map(int,input().split())
import collections
h2l=[]
w2l=[]
for i in range(m):
    h2,w2=map(int,input().split())
    h2l.append(h2)
    w2l.append(w2)

ch=collections.Counter(h2l)
cw=collections.Counter(w2l)

ans=0
chcnt=1
cwcnt=1
for i in range(len(ch)-1):
    if ch.most_common()[i][1]==ch.most_common()[i+1][1]:
        chcnt+=1
    else:
        break
for i in range(len(cw)-1):
    if cw.most_common()[i][1]==cw.most_common()[i+1][1]:
        cwcnt+=1
    else:
        break

for i in range(chcnt):
    for j in range(cwcnt):
        ansx=ch.most_common()[i][0]
        ansy=cw.most_common()[j][0]
        ansh=ch.most_common()[i][1]
        answ=cw.most_common()[j][1]
        for k in range(m):
            if ansx==h2l[k] and ansy==w2l[k]:
                ans=0
                break
            ans=ansh+answ

print(ans if ans>0 else ansh+answ-1)

Submission status: Wrong Answer

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

X = [0] * H
Y = [0] * W
 
A = []
m = M
while m:
    m -= 1
    x, y = map(int,input().split())
    x, y = x -1, y -1
    A.append((x, y))
    X[x] += 1
    Y[y] += 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 = True
k = len(xms) * len(yms)
if k < M:
    z = 0
    for a, b in A:
        if a in xms:
            if b in yms:
                z += 1
                if z == k: ans = False; break
        
cnt = xm + ym         
print(cnt if ans else cnt - 1)

Submission status: Wrong Answer

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, h in enumerate(Ws) if w == Hmax)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

    HW[(h,w)] = 1

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

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

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

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

Submission status: Wrong Answer

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

for i in range(h):
    if tate[i] > T:
        T = tate[i]
for i in range(w):
    if yoko[i] > Y:
        Y = yoko[i]
ans = Y + T
for i in range(h):
    if tate[i] == T:
        for j in range(w):
            if yoko[j] == Y:
                for k in range(m):
                    if a[k] != i+1:
                        if b[k] != j+1:
                            break
                else:
                    continue
                break
        else:
            continue
        break
else:
    ans = ans -1
print(ans)

Submission status: Wrong Answer

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

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

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

    HW[h][w] = 1

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


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

Submission status: Wrong Answer

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

for i in range(h):
    if tate[i] > T:
        T = tate[i]
for i in range(w):
    if yoko[i] > Y:
        Y = yoko[i]
ans = Y + T - 1
for i in range(h):
    if tate[i] == T:
        for j in range(w):
            if yoko[j] == Y:
                for k in range(m):
                    if a[k] != i+1:
                        if b[k] != j+1:
                            ans = ans +1
                            break
                else:
                    continue
                break
        else:
            continue
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import sys, math, bisect



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

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

    value = max_x + max_y

    for j in ys:

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



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


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


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




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

Submission status: Wrong Answer

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

for i in range(h):
    if tate[i] > T:
        T = tate[i]
        t = i+1
for i in range(w):
    if yoko[i] > Y:
        Y = yoko[i]
        y = i+1
ans = Y + T
for i in range(m):
    if a[i] == t:
        if b[i] == y:
            ans = ans -1
print(ans)

Submission status: Wrong Answer

Solution code:
H, W, M = map(int, input().split())
hlist = [0] * H
wlist = [0] * W
hw = set()
for i in range(M):
    h, w = map(int, input().split())
    hlist[h - 1] += 1
    wlist[w - 1] += 1
    hw.add((h - 1, w - 1))

hmax = max(hlist)
wmax = max(wlist)
hmaxlist = [i for i, v in enumerate(hlist) if v == hmax]
wmaxlist = [i for i, v in enumerate(wlist) if v == wmax]
cnt = -1
for i in hmaxlist:
    bf = False
    for j in wmaxlist:
        if not hw in (i, j):
            cnt = 0
            bf = True
            break
    if bf:
        break

print(hmax + wmax + cnt)

Submission status: Wrong Answer

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

for i in range(h):
    if tate[i] > T:
        T = tate[i]
        t = i
for i in range(w):
    if yoko[i] > Y:
        Y = yoko[i]
        y = i
ans = Y + T
for i in range(m):
    if a[i] == t:
        if b[i] == y:
            ans = ans -1
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)
from numba import njit,i8


# @njit(i8[:,:](i8[:,:],i8[:,:],i8))
# def calcBombT(hw,tar,M):
#     for i in range(M):
#         tar[hw[i,0]-1,hw[i,1]-1] = 1
#     return tar


@njit(i8[:](i8[:,:],i8[:],i8))
def calcBombR(hw,row,M):
    for i in range(M):
        row[hw[i,0]-1] += 1
    return row


@njit(i8[:](i8[:,:],i8[:],i8))
def calcBombC(hw,col,M):
    for i in range(M):
        col[hw[i,1]-1] += 1
    return col


# @njit(i8(i8[:,:],i8[:],i8[:],i8,i8,i8,i8,i8,i8[:]))
def existNR(hw,indMR,indMC,NR,NC,Ntemp,cnt,check,temp):
    for i in range(NR):
        temp = np.array(np.where(hw[:,0] == indMR[i]+1)[0])
        Ntemp = temp.size
        cnt = 0
        Ntemp = loopjk(hw,indMR,indMC,NC,Ntemp,cnt,temp)
        if Ntemp == 0:
            check = 1
            break
    return check


@njit(i8(i8[:,:],i8[:],i8[:],i8,i8,i8,i8[:]))
def loopjk(hw,indMR,indMC,NC,Ntemp,cnt,temp):
    for j in range(NC):
        cnt = 0
        for k in range(Ntemp):
            if hw[temp[k],1] != indMC[j]+1:
                cnt += 1
            if cnt == Ntemp:
                Ntemp = 0
                break
        if Ntemp == 0:
            break
    return Ntemp


# @njit(i8(i8[:,:],i8[:],i8[:],i8,i8,i8,i8,i8,i8[:]))
def existNC(hw,indMR,indMC,NR,NC,Ntemp,cnt,check,temp):
    for j in range(NC):
        temp = np.array(np.where(hw[:,1] == indMC[j]+1)[0])
        Ntemp = temp.size
        cnt = 0
        Ntemp = loopik(hw,indMR,indMC,NR,Ntemp,cnt,temp)
        if Ntemp == 0:
            check = 1
            break
    return check


@njit(i8(i8[:,:],i8[:],i8[:],i8,i8,i8,i8[:]))
def loopik(hw,indMR,indMC,NR,Ntemp,cnt,temp):
    for i in range(NR):
        cnt = 0
        for k in range(Ntemp):
            if hw[temp[k],1] != indMR[i]+1:
                cnt += 1
            if cnt == Ntemp:
                Ntemp = 0
                break
        if Ntemp == 0:
            break
    return Ntemp


def main():
    H,W,M = map(int,input().split())
    hw = [list(map(int,input().split())) for i in range(M)]
    hw = np.array(hw)

    # tar = np.zeros((H,W),dtype = int)
    row = np.zeros(H,dtype = int)
    col = np.zeros(W,dtype = int)

    # tar = calcBombT(hw,tar,M)
    row = calcBombR(hw,row,M)
    col = calcBombC(hw,col,M)

    MR = row.max()
    MC = col.max()

    indMR = np.where(row == MR)[0]
    indMC = np.where(col == MC)[0]

    indMR = np.array(indMR)
    indMC = np.array(indMC)

    NR = len(indMR)
    NC = len(indMC)

    if NR > NC:
        check = existNC(hw,indMR,indMC,NR,NC,1,0,0,np.array([0]))
    else:
        check = existNR(hw,indMR,indMC,NR,NC,1,0,0,np.array([0]))

    if check:
        res = MR + MC
    else:
        res = MR + MC - 1

    print(res)



main()

Submission status: Wrong Answer

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

gh = {i:[] for i in range(h+1)}
gw = {i:[] for i in range(w+1)}

for _h, _w in hw:
    gh[_h].append(_w)
    gw[_w].append(_h)
    
ansh = 0
posh = -1
answ = 0
posw = -1
for k,v in gh.items():
    if len(v) > ansh:
        ansh = len(v)
        posh = k
for k,v in gw.items():
    if len(v) > answ:
        if posh in v:
            answ = len(v)-1
            posw = k
        else:
            answ = len(v)
            posw = k
            
print(ansh+answ)

Submission status: Wrong Answer

Solution code:
import sys, math, heapq, bisect



def solve(W_N, V_N, bombs):
    max_x = max(W_N)
    max_y = max(V_N)

    flag = False
    value = max_x + max_y

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

    return value-1

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

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


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


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

    print( solve(W_N, V_N, bombs) )

    """


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



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


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

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

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

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

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

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)
from numba import njit,i8


# @njit(i8[:,:](i8[:,:],i8[:,:],i8))
# def calcBombT(hw,tar,M):
#     for i in range(M):
#         tar[hw[i,0]-1,hw[i,1]-1] = 1
#     return tar


@njit(i8[:](i8[:,:],i8[:],i8))
def calcBombR(hw,row,M):
    for i in range(M):
        row[hw[i,0]-1] += 1
    return row


@njit(i8[:](i8[:,:],i8[:],i8))
def calcBombC(hw,col,M):
    for i in range(M):
        col[hw[i,1]-1] += 1
    return col


# @njit(i8(i8[:,:],i8[:],i8[:],i8,i8,i8,i8,i8,i8[:]))
def exist(hw,indMR,indMC,NR,NC,Ntemp,cnt,check,temp):
    for i in range(NR):
        temp = np.array(np.where(hw[:,0] == indMR[i]+1)[0])
        Ntemp = temp.size
        cnt = 0
        Ntemp = loopjk(hw,indMR,indMC,NC,Ntemp,cnt,temp)
        if Ntemp == 0:
            check = 1
            break
    return check


@njit(i8(i8[:,:],i8[:],i8[:],i8,i8,i8,i8[:]))
def loopjk(hw,indMR,indMC,NC,Ntemp,cnt,temp):
    for j in range(NC):
        for k in range(Ntemp):
            if hw[temp[k],1] != indMC[j]+1:
                cnt += 1
            if cnt == Ntemp:
                Ntemp = 0
                break
        if Ntemp == 0:
            break
    return Ntemp


def main():
    H,W,M = map(int,input().split())
    hw = [list(map(int,input().split())) for i in range(M)]
    hw = np.array(hw)

    # tar = np.zeros((H,W),dtype = int)
    row = np.zeros(H,dtype = int)
    col = np.zeros(W,dtype = int)

    # tar = calcBombT(hw,tar,M)
    row = calcBombR(hw,row,M)
    col = calcBombC(hw,col,M)

    MR = row.max()
    MC = col.max()

    indMR = np.where(row == MR)[0]
    indMC = np.where(col == MC)[0]

    indMR = np.array(indMR)
    indMC = np.array(indMC)

    NR = len(indMR)
    NC = len(indMC)

    check = exist(hw,indMR,indMC,NR,NC,1,0,0,np.array([0]))

    if check:
        res = MR + MC
    else:
        res = MR + MC - 1

    print(res)



main()

Submission status: Wrong Answer

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

gh = {i:[] for i in range(h+1)}
gw = {i:[] for i in range(w+1)}

for _h, _w in hw:
    gh[_h].append(_w)
    gw[_w].append(_h)
    
ansh = 0
posh = -1
answ = 0
posw = -1
for k,v in gh.items():
    if len(v) > ansh:
        ansh = len(v)
        posh = k
for k,v in gw.items():
    if posh in v:
        if len(v)-1 > answ:
            answ = len(v)-1
            posw = k
    else:
        if len(v) > answ:
            answ = len(v)
            posw = k
            
print(ansh+answ)

Submission status: Wrong Answer

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

for i in range(h):
    if tate[i] > T:
        T = tate[i]
for i in range(w):
    if yoko[i] > Y:
        Y = yoko[i]
ans = Y + T
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

h, w, m = list(map(int, input().split()))
bh = [0 for j in range(h)]
bw = [0 for j in range(w)]
bhw = [[0 for j in range(w)] for i in range(h)]
hmaxs = []
wmaxs = []

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

maxbh= max(bh)
maxbw =max(bw)
ans = 0
for i in range(h):
    if bh[i] == maxbh:
        for j in range(w):
            if bw[i] == maxbw:

                ans =max(bhw[i - 1][j - 1] + maxbh + maxbw ,ans)


print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)
from numba import njit,i8


# @njit(i8[:,:](i8[:,:],i8[:,:],i8))
# def calcBombT(hw,tar,M):
#     for i in range(M):
#         tar[hw[i,0]-1,hw[i,1]-1] = 1
#     return tar


@njit(i8[:](i8[:,:],i8[:],i8))
def calcBombR(hw,row,M):
    for i in range(M):
        row[hw[i,0]-1] += 1
    return row


@njit(i8[:](i8[:,:],i8[:],i8))
def calcBombC(hw,col,M):
    for i in range(M):
        col[hw[i,1]-1] += 1
    return col


# @njit(i8(i8[:,:],i8[:],i8[:],i8,i8,i8,i8,i8,i8[:]))
def exist(hw,indMR,indMC,NR,NC,Ntemp,cnt,check,temp):
    for i in range(NR):
        for j in range(NC):
            temp = np.array(np.where(hw[:,0] == indMR[i])[0])
            Ntemp = temp.size
            cnt = 0
            for k in range(Ntemp):
                if hw[temp[k],1] != indMC[j]:
                    cnt += 1
                if cnt == Ntemp:
                    check = 1
                    Ntemp = 0
                    break
            if Ntemp == 0:
                check = 1
                break
        if Ntemp == 0:
            check = 1
            break
    return check


def main():
    H,W,M = map(int,input().split())
    hw = [list(map(int,input().split())) for i in range(M)]
    hw = np.array(hw)

    # tar = np.zeros((H,W),dtype = int)
    row = np.zeros(H,dtype = int)
    col = np.zeros(W,dtype = int)

    # tar = calcBombT(hw,tar,M)
    row = calcBombR(hw,row,M)
    col = calcBombC(hw,col,M)

    MR = row.max()
    MC = col.max()

    indMR = np.where(row == MR)[0]
    indMC = np.where(col == MC)[0]

    indMR = np.array(indMR)
    indMC = np.array(indMC)

    NR = len(indMR)
    NC = len(indMC)

    check = exist(hw,indMR,indMC,NR,NC,1,0,0,np.array([0]))

    if check:
        res = MR + MC
    else:
        res = MR + MC - 1

    print(res)



main()

Submission status: Wrong Answer

Solution code:
import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)
from numba import njit,i8


# @njit(i8[:,:](i8[:,:],i8[:,:],i8))
# def calcBombT(hw,tar,M):
#     for i in range(M):
#         tar[hw[i,0]-1,hw[i,1]-1] = 1
#     return tar


@njit(i8[:](i8[:,:],i8[:],i8))
def calcBombR(hw,row,M):
    for i in range(M):
        row[hw[i,0]-1] += 1
    return row


@njit(i8[:](i8[:,:],i8[:],i8))
def calcBombC(hw,col,M):
    for i in range(M):
        col[hw[i,1]-1] += 1
    return col


# @njit(i8(i8[:,:],i8[:],i8[:],i8,i8,i8,i8,i8,i8[:]))
def exist(hw,indMR,indMC,NR,NC,Ntemp,cnt,check,temp):
    for i in range(NR):
        for j in range(NC):
            temp = np.array(np.where(hw[:,0] == indMR[i])[0])
            Ntemp = temp.size
            for k in range(Ntemp):
                if hw[temp[k],1] != indMC[j]:
                    cnt += 1
                if cnt == Ntemp:
                    check = 1
                    Ntemp = 0
                    break
            if Ntemp == 0:
                check = 1
                break
        if Ntemp == 0:
            check = 1
            break
    return check


def main():
    H,W,M = map(int,input().split())
    hw = [list(map(int,input().split())) for i in range(M)]
    hw = np.array(hw)

    # tar = np.zeros((H,W),dtype = int)
    row = np.zeros(H,dtype = int)
    col = np.zeros(W,dtype = int)

    # tar = calcBombT(hw,tar,M)
    row = calcBombR(hw,row,M)
    col = calcBombC(hw,col,M)

    MR = row.max()
    MC = col.max()

    indMR = np.where(row == MR)[0]
    indMC = np.where(col == MC)[0]

    indMR = np.array(indMR)
    indMC = np.array(indMC)

    NR = len(indMR)
    NC = len(indMC)

    check = exist(hw,indMR,indMC,NR,NC,1,0,0,np.array([0]))

    if check:
        res = MR + MC
    else:
        res = MR + MC - 1

    print(res)



main()

Submission status: Wrong Answer

Solution code:
import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)
from numba import njit,i8


# @njit(i8[:,:](i8[:,:],i8[:,:],i8))
# def calcBombT(hw,tar,M):
#     for i in range(M):
#         tar[hw[i,0]-1,hw[i,1]-1] = 1
#     return tar


@njit(i8[:](i8[:,:],i8[:],i8))
def calcBombR(hw,row,M):
    for i in range(M):
        row[hw[i,0]-1] += 1
    return row


@njit(i8[:](i8[:,:],i8[:],i8))
def calcBombC(hw,col,M):
    for i in range(M):
        col[hw[i,1]-1] += 1
    return col


# @njit(i8(i8[:,:],i8[:],i8[:],i8,i8,i8,i8,i8[:]))
def exist(hw,indMR,indMC,NR,NC,Ntemp,check,temp):
    for i in range(NR):
        for j in range(NC):
            temp = np.array(np.where(hw[:,0] == indMR[i])[0])
            Ntemp = temp.size
            for k in range(Ntemp):
                if hw[temp[k],1] != indMC[j]:
                    check = 1
                    Ntemp = 0
                    break
            if Ntemp == 0:
                check = 1
                break
        if Ntemp == 0:
            check = 1
            break
    return check


def main():
    H,W,M = map(int,input().split())
    hw = [list(map(int,input().split())) for i in range(M)]
    hw = np.array(hw)

    # tar = np.zeros((H,W),dtype = int)
    row = np.zeros(H,dtype = int)
    col = np.zeros(W,dtype = int)

    # tar = calcBombT(hw,tar,M)
    row = calcBombR(hw,row,M)
    col = calcBombC(hw,col,M)

    MR = row.max()
    MC = col.max()

    indMR = np.where(row == MR)[0]
    indMC = np.where(col == MC)[0]

    indMR = np.array(indMR)
    indMC = np.array(indMC)

    NR = len(indMR)
    NC = len(indMC)

    check = exist(hw,indMR,indMC,NR,NC,1,0,np.array([0]))

    if check:
        res = MR + MC
    else:
        res = MR + MC - 1

    print(res)



main()

Submission status: Wrong Answer

Solution code:
H, W, M = map(int, input().split())
bomb = [[0]*2 for _ in range(M)]
num_row = [0] * H
num_col = [0] * W
for i in range(M):
    h, w = map(int, input().split())
    bomb[i][0] = h - 1
    bomb[i][1] = w - 1
    num_row[h-1] += 1
    num_col[w-1] += 1

row_max_bomb = max(num_row)
col_max_bomb = max(num_col)
tmp_row = [i for i, v in enumerate(num_row) if v == row_max_bomb]
tmp_col = [i for i, v in enumerate(num_col) if v == col_max_bomb]

for hw in bomb:
    if hw[0] not in tmp_row and hw[1] not in tmp_col:
        print(row_max_bomb + col_max_bomb - 1)
        break
else:
    print(row_max_bomb + col_max_bomb)

Submission status: Wrong Answer

Solution code:
H,W,M=map(int,input().split())
MX=[0]*H
MY=[0]*W
MA=[0]*M
 
for i in range(M):
  x,y=map(int,input().split())
  x-=1
  y-=1
  MX[x]+=1
  MY[y]+=1
  MA[i]=(x,y)
xmax=max(MX)
ymax=max(MY)
ix=[x[0] for x in enumerate(MX) if x[1]==xmax]
iy=[x[0] for x in enumerate(MY) if x[1]==ymax]
 
i=0
j=0
for x,y in MA:
  if x in ix or y in iy:
    pass
  else:
    i+=1
  
  j+=1
  
  if len(ix)*len(iy)-j>M-i:
    print(xmax+ymax)
else:
  print(xmax+ymax-1)

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

h, w, m = list(map(int, input().split()))
bh = [0 for j in range(h)]
bw = [0 for j in range(w)]
bhw = [[0 for j in range(w)] for i in range(h)]
hmaxs = []
wmaxs = []

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

maxbh= max(bh)
maxbw =max(bw)

for i in range(h):
    if bh[i] == maxbh:
        hmaxs.append(i)

for i in range(w):
    if bw[i] == maxbw:
        wmaxs.append(i)

c =1

for i in hmaxs:
    for j in wmaxs:

        if bhw[i][j]==0:
            c=0
            break
    if c == 0:
        break


print(maxbh + maxbw -c)

Submission status: Wrong Answer

Solution code:
H,W,M=map(int,input().split())
MX=[0]*H
MY=[0]*W
MA=[0]*M

for i in range(M):
  x,y=map(int,input().split())
  x-=1
  y-=1
  MX[x]+=1
  MY[y]+=1
  MA[i]=(x,y)
xmax=max(MX)
ymax=max(MY)
ix=[x[0] for x in enumerate(MX) if x[1]==xmax]
iy=[x[0] for x in enumerate(MY) if x[1]==ymax]

i=0
for x,y in MA:
  if x in ix and y in iy:
    pass
  else:
    i+=1
  
  if len(ix)*len(iy)>M-i:
    print(xmax+ymax)
else:
  print(xmax+ymax-1)