Problem:


Score : 500 points

Problem Statement

We have a two-dimensional grid with H \times W squares. There are M targets to destroy in this grid - the position of the i-th target is \left(h_i, w_i \right).

Takahashi will choose one square in this grid, place a bomb there, and ignite it. The bomb will destroy all targets that are in the row or the column where the bomb is placed. It is possible to place the bomb at a square with a target.

Takahashi is trying to maximize the number of targets to destroy. Find the maximum number of targets that can be destroyed.

Constraints

All values in input are integers.

1 \leq H, W \leq 3 \times 10^5

1 \leq M \leq \min\left(H\times W, 3 \times 10^5\right)

1 \leq h_i \leq H

1 \leq w_i \leq W

\left(h_i, w_i\right) \neq \left(h_j, w_j\right) \left(i \neq j\right)

Input

Input is given from Standard Input in the following format:

H W M
h_1 w_1
\vdots
h_M w_M

Output

Print the answer.

Sample Input 1

2 3 3
2 2
1 1
1 3

Sample Output 1

3

We can destroy all the targets by placing the bomb at \left(1, 2\right).

Sample Input 2

3 3 4
3 3
3 1
1 1
1 2

Sample Output 2

3

Sample Input 3

5 5 10
2 5
4 3
2 3
5 5
2 2
5 4
5 3
5 1
3 5
1 4

Sample Output 3

6



Sample input:
2 3 3
2 2
1 1
1 3


Sample output:
3

Submission status: Runtime Error

Solution code:
import sys, math, heapq, bisect



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

    max_x = 0
    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 = 0
    ys = []
    for j in range(H):
        if(W_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 i in xs:
        for j in ys:
            if(i not in bombs[j] ):
                return value
    return value-1

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

                    return value

    return value-1
    """

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

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


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


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

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

    """


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



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


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

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

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

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

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

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

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

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

read = sys.stdin.buffer.read

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

hw = []
# w = np.zeros(3*10**5,dtype=np.int64)
# h = np.zeros(3*10**5,dtype=np.int64)
w = [0] * 3*10**5
h = [0] * 3*10**5

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

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

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

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

Submission status: Runtime Error

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 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 = [  [0] * W 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][x-1] = 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: Runtime Error

Solution code:
import sys, math, heapq, bisect





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 = [  [0] * W 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][x-1] = 1


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


    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 bombs[i][j] == 0 ):
                    print(value)
                    sys.exit()

    print(value-1)
    """


    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: Runtime Error

Solution code:
import sys, math, heapq, bisect





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

    H_N_bomb = [ [0, i]  for i in range(H) ]
    W_N_bomb = [ [0, i]  for i in range(W) ]
    bombs = [  [0] * W for _ in range(H) ]


    for i in range(N_bombs):
        y, x = map(int, input().strip().split() )

        W_N_bomb[x-1][0] += 1
        H_N_bomb[y-1][0] += 1
        bombs[y-1][x-1] = 1



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



    Ws = [ W_N_bomb[i][0] for i in range(W)]
    Hs = [ H_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 = H_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, H_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 = H_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: Runtime Error

Solution code:
h, w, m = map(int, input().split())
dp= [[0]*w for i in range(h)]
dpcol = [0]*w
dprow = [0]*h
ypos = []
xpos = []
anscol = 0
ansrow = 0
ans = 0

for i in range(m):
    H, W = map(lambda x: int(x) - 1, input().split())
    dp[H][W] = 1
    dpcol[W] += 1
    dprow[H] += 1

for i in range(w): #列の最大値
    if dpcol[i] > anscol:
        anscol = dpcol[i]
        xpos = [i]
    elif dpcol[i] == anscol:
        xpos.append(i)

for i in range(h):
    if dprow[i] > ansrow:
        ansrow = dprow[i]
        ypos = [i]
    elif dprow[i] == ansrow:
        ypos.append(i)

for i in ypos:
    for j in xpos:
        if dp[i][j] == 1:
            ans = anscol + ansrow - 1
        else:
            ans = anscol + ansrow
            print(ans)
            exit()
print(ans)

Submission status: Runtime Error

Solution code:
temparr = input()
temparr = temparr.split()
row = int(temparr[0])
col = int(temparr[1])
m = int(temparr[2])
dicts= {}
dicts1 = {}
tempcol = [0 for _ in range(col)]
dp = [tempcol[::] for _ in range(row)]
for ii in range(m):
    temparr = input()
    temparr = temparr.split()
    x = int(temparr[0]) - 1
    y = int(temparr[1]) - 1
    dp[x][y] = 1 
    
rowarr = []
colarr = []

for i in range(row):
    rowarr.append(sum(dp[i]))

for i in range(col):
    total = 0 
    for j in range(row):
        total += dp[j][i]
    colarr.append(total)

curmax = 0 
# print(dp)
# print(rowarr)
# print(colarr)
for i in range(row):
    for j in range(col):
        newtotal = 0 
        if dp[i][j] == 0 :
            newtotal = rowarr[i]
            newtotal += colarr[j]
            if newtotal > curmax:
                curmax = newtotal
        else:
            newtotal = rowarr[i]
            newtotal += colarr[j]
            newtotal -= 1
            if newtotal > curmax:
                curmax = newtotal
print(curmax)

Submission status: Runtime Error

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

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

hw = []

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

for _ in range(M):
    a,b = list(map(int,input().split()))
    h[a - 1] += 1
    w[b - 1] += 1
    hw[a - 1][b - 1] += 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 time.time()-t1>2.1:
            print(hm+wm)
            exit()
        if hw[x][y] == 0:
            print(hm + wm)
            exit()
else:
    print(hm + wm - 1)
"""

Submission status: Runtime Error

Solution code:
H, W, M = map(int, input().split())
B = []
Bom = [[0] * W for _ in range(H)]
BomC = [0] * W
BomR = [0] * H
for _ in range(M):
    h, w = map(int, input().split())
    h -= 1
    w -= 1
    Bom[h][w] = 1
    BomC[w] += 1
    BomR[h] += 1
    B.append((h, w))
C = set()
R = set()
cm = max(BomC)
rm = max(BomR)

for w, c in enumerate(BomC):
    if c == cm:
        C.add(w)
for h, r in enumerate(BomR):
    if r == rm:
        R.add(h)
flag = True
for c in C:
    for r in R:
        if Bom[r][c]:
            continue
        else:
            flag = False
            break
if flag:
    print(cm + rm - 1)
else:
    print(cm + rm)

Submission status: Runtime Error

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

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

hw = []

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

for _ in range(M):
    a,b = list(map(int,input().split()))
    h[a - 1] += 1
    w[b - 1] += 1
    hw[a - 1][b - 1] += 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 time.time()-t1>2.1:
            print(hm+wm)
            exit()
        if hw[x][y] == 0:
            print(hm + wm)
            exit()
else:
    print(hm + wm - 1)

Submission status: Runtime Error

Solution code:
test=1
Copy
H,W,M = list(map(int,input().split()))
 
h = [0] * H
w = [0] * W
 
hw = []
 
for _ in range(H):
    hw.append([0] * W)

    
for _ in range(M):
    a,b = list(map(int,input().split()))
    h[a - 1] += 1
    w[b - 1] += 1
    hw[a - 1][b - 1] += 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]

Submission status: Runtime Error

Solution code:
import itertools


import numpy as np


def solver(H, W, M, HW):
    A = np.zeros(H, dtype=np.int64)
    B = np.zeros(W, dtype=np.int64)

    for h, w in HW:
        A[h - 1] += 1
        B[w - 1] += 1

    maxA = np.max(A)
    maxB = np.max(B)

    # 最大値を取るindex
    ind_A = np.arange(H)[A == maxA]
    ind_B = np.arange(W)[B == maxB]

    # 解の候補の数
    nn = len(ind_A) * len(ind_B)

    # Mより候補の数のほうが多いとok
    # 少ないときはM以下なので可算
    ans = maxA + maxB

    if nn <= M:
        for h, w in HW:
            if A[h - 1] == maxA and B[w - 1] == maxB:
                nn -= 1

    print(ans if nn else ans - 1)


def main():
    H, W, M = map(int, input().split())
    HW = [list(map(int, input().split())) for _ in range(M)]

    solver(H, W, M, np.array(HW))


main()

Submission status: Runtime Error

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

C = []
R = []
ans = 0

for w, c in enumerate(BomC):
    C.append((c, w))
for h, r in enumerate(BomR):
    R.append((r, h))
C.sort(reverse=True)
R.sort(reverse=True)
cm = max(BomC)
rm = max(BomR)

for r, h in R:
    if r < rm - 1:
        break
    for c, w in C:
        if c < cm - 1:
            break
        ans = max(ans, r + c - Bom[h][w])

print(ans)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

read = sys.stdin.buffer.read

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

hw = np.zeros((3*10**5,3*10**5),dtype=np.int64)
w = np.zeros(3*10**5,dtype=np.int64)
h = np.zeros(3*10**5,dtype=np.int64)
 
for i in range(M):
    h_i, w_j = map(int, input().split())
    hw[h_i-1, w_j-1] = 1
    h[h_i-1] += 1
    w[w_j-1] += 1

a = 0
d = 0
for i in np.where(h==h.max())[0]:
    for j in np.where(w==w.max())[0]:
        mxh_v = h[i]
        mxw_v = w[j]
        d = hw[i, j]
        # print(i,j,a, mxh_v , mxw_v , d)
        a = max(a, mxh_v + mxw_v - d)
    if d == 0:
        break
print(a)

Submission status: Runtime Error

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

xy = [list(map(int, input().split())) for _ in range(M)]
x, y = [list(i) for i in zip(*xy)]

x_score = [0]*(H+1)
y_score = [0]*(W+1)
total = 0

for i in range(M):
	x_score[x[i]-1] += 1
	y_score[y[i]-1] += 1


x_max = max(x_score)
y_max = max(y_score)
total = x_max +  y_max
xx = [i for i, v in enumerate(x_score) if v == x_max]
yy = [i for i, v in enumerate(y_score) if v == y_max]
count = 0
for v in xx:
	for p in yy:
		if [x[v],y[p]] not in xy:
			count = 1
			break
if count == 0:
	total -= 1
print(total)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

read = sys.stdin.buffer.read

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

hw = np.zeros((H+1,W+1),dtype=np.int64)
w = np.zeros(3*10**5,dtype=np.int64)
h = np.zeros(3*10**5,dtype=np.int64)
 
for i in range(M):
    h_i, w_j = map(int, input().split())
    hw[h_i-1, w_j-1] = 1
    h[h_i-1] += 1
    w[w_j-1] += 1

a = 0
d = 0
for i in np.where(h==h.max())[0]:
    for j in np.where(w==w.max())[0]:
        mxh_v = h[i]
        mxw_v = w[j]
        d = hw[i, j]
        # print(i,j,a, mxh_v , mxw_v , d)
        a = max(a, mxh_v + mxw_v - d)
    if d == 0:
        break
print(a)

Submission status: Runtime Error

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

xy = [list(map(int, input().split())) for _ in range(M)]
x, y = [list(i) for i in zip(*xy)]

x_score = [0]*(H+1)
y_score = [0]*(W+1)
total = 0

for i in range(M):
	x_score[x[i]] += 1
	y_score[y[i]] += 1



total = max(x_score) +  max(y_score)
xx = [i for i, v in enumerate(x_score) if v == max(x_score)]
yy = [i for i, v in enumerate(y_score) if v == max(y_score)]
count = 0
for v in xx:
	for p in yy:
		if [x[v],y[p]] not in xy:
			count = 1
			break
if count == 0:
	total -= 1
print(total)

Submission status: Runtime Error

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

xy = [list(map(int, input().split())) for _ in range(M)]
x, y = [list(i) for i in zip(*xy)]

x_score = [0]*(H+1)
y_score = [0]*(W+1)
total = 0

for i in range(M):
	x_score[x[i]] += 1
	y_score[y[i]] += 1



total = max(x_score) +  max(y_score)
xx = [i for i, v in enumerate(x_score) if v == max(x_score)]
yy = [i for i, v in enumerate(y_score) if v == max(y_score)]
count = 0
for i,v in enumerate(xx):
	for j,p in enumerate(yy):
		if [x[v],y[p]] not in xy:
			count = 1
			break
if count == 0:
	total -= 1
print(total)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

read = sys.stdin.buffer.read

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

hw = np.zeros((H+1,W+1),dtype=np.int64)
w = np.zeros(3*10**5,dtype=np.int64)
h = np.zeros(3*10**5,dtype=np.int64)
 
for i in range(M):
    h_i, w_j = list(map(int, input().split()))
    hw[h_i, w_j] = 1
    h[h_i] += 1
    w[w_j] += 1

a = 0
d = 0
for i in np.where(h==h.max())[0]:
    for j in np.where(w==w.max())[0]:
        mxh_v = h[i]
        mxw_v = w[j]
        d = hw[i, j]
        # print(i,j,a, mxh_v , mxw_v , d)
        a = max(a, mxh_v + mxw_v - d)
    if d == 0:
        break
print(a)

Submission status: Runtime Error

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
  MX[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]

flg=False
for x,y in MA:
  if (x in ix)&(y in iy):
    print(xmax+ymax)
    exit()
print(xmax+ymax-1)

Submission status: Runtime Error

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,i4


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


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


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


def exist(hwl,indMR,indMC,NR,NC,check):
    for i in range(NR):
        for j in range(NC):
            if [indMR[i]+1,indMC[j]+1] in hwl:
                continue
            else:
                check = 1
                break
    return check


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

    tar = np.array([[0 for j in range(W)] for i in range(H)])
    row = np.array([0 for i in range(H)])
    col = np.array([0 for j in range(W)])

    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]

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

    check = exist(hwl,indMR,indMC,NR,NC,0)

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

    print(res)



main()

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
 
H,W,M = list(map(int, input().split()))

hw = np.zeros((H+1,W+1),dtype=np.int32)
w = np.zeros(W+1,dtype=np.int32)
h = np.zeros(H+1,dtype=np.int32)
 
for i in range(M):
    h_i, w_j = list(map(int, input().split()))
    hw[h_i, w_j] = 1
    h[h_i] += 1
    w[w_j] += 1

a = 0
d = 0
for i in np.where(h==h.max())[0]:
    for j in np.where(w==w.max())[0]:
        mxh_v = h[i]
        mxw_v = w[j]
        d = hw[i, j]
        # print(i,j,a, mxh_v , mxw_v , d)
        a = max(a, mxh_v + mxw_v - d)
    if d == 0:
        break
print(a)

Submission status: Runtime Error

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

C = []
R = []
ans = 0

for i, c in enumerate(BomC):
    C.append((c, i))
for i, c in enumerate(BomR):
    R.append((c, i))
C.sort(reverse=True)
R.sort(reverse=True)
cm = C[0][0]
rm = R[0][0]

for r, h in R:
    if r < rm -1:
        break
    for c, w in C:
        if c < cm - 1:
            break
        ans = max(ans, r + c - Bom[h][w])

print(ans)

Submission status: Runtime Error

Solution code:
import itertools

import numpy as np

H, W, M = map(int, input().split())
HW = [list(map(int, input().split())) for _ in range(M)]
F = np.zeros([H, W], dtype=np.bool)

for h, w in HW:
    F[h - 1, w - 1] = True

# 各方向の和を計算
A = np.sum(F, axis=0)
B = np.sum(F, axis=1)

maxA = np.max(A)
maxB = np.max(B)

# 最大値を取るindex
ind_A = np.arange(W)[A == maxA]
ind_B = np.arange(H)[B == maxB]

cnt = 0
ans = maxA + maxB
for h, w in itertools.product(ind_B, ind_A):
    if not F[h, w]:
        break

    cnt += 1
    if cnt == M:
        ans -= 1
        break
else:
    ans -= 1

print(ans)

Submission status: Runtime Error

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
  MX[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]

for x,y in MA:
  if (x not in ix)&(y not in iy):
    print(xmax+ymax)
    exit()
    
print(xmax+ymax-1)

Submission status: Runtime Error

Solution code:
import itertools

import numpy as np

H, W, M = map(int, input().split())
HW = [list(map(int, input().split())) for _ in range(M)]
F = np.zeros([H, W], dtype=np.int64)

print("h")
"""
for h, w in HW:
    F[h - 1, w - 1] = 1

# 各方向の和を計算
A = np.sum(F, axis=0)
B = np.sum(F, axis=1)

maxA = np.max(A)
maxB = np.max(B)

# 最大値を取るindex
ind_A = np.arange(W)[A == maxA]
ind_B = np.arange(H)[B == maxB]

cnt = 0
ans = maxA + maxB
for h, w in itertools.product(ind_B, ind_A):
    if F[h, w] == 0:
        break

    cnt += 1
    if cnt == M:
        ans -= 1
        break
else:
    ans -= 1
print(ans)
"""

Submission status: Runtime Error

Solution code:
import itertools

import numpy as np

H, W, M = map(int, input().split())
HW = [list(map(int, input().split())) for _ in range(M)]
F = np.zeros([H, W], dtype=np.int64)

for h, w in HW:
    F[h - 1, w - 1] = 1

print("h")
"""
# 各方向の和を計算
A = np.sum(F, axis=0)
B = np.sum(F, axis=1)

maxA = np.max(A)
maxB = np.max(B)

# 最大値を取るindex
ind_A = np.arange(W)[A == maxA]
ind_B = np.arange(H)[B == maxB]

cnt = 0
ans = maxA + maxB
for h, w in itertools.product(ind_B, ind_A):
    if F[h, w] == 0:
        break

    cnt += 1
    if cnt == M:
        ans -= 1
        break
else:
    ans -= 1
print(ans)
"""

Submission status: Runtime Error

Solution code:
import itertools

import numpy as np

H, W, M = map(int, input().split())
HW = [list(map(int, input().split())) for _ in range(M)]
F = np.zeros([H, W], dtype=np.int64)

for h, w in HW:
    F[h - 1, w - 1] = 1

# 各方向の和を計算
A = np.sum(F, axis=0)
B = np.sum(F, axis=1)

maxA = np.max(A)
maxB = np.max(B)
print('h')

"""
# 最大値を取るindex
ind_A = np.arange(W)[A == maxA]
ind_B = np.arange(H)[B == maxB]

cnt = 0
ans = maxA + maxB
for h, w in itertools.product(ind_B, ind_A):
    if F[h, w] == 0:
        break

    cnt += 1
    if cnt == M:
        ans -= 1
        break
else:
    ans -= 1
print(ans)
"""

Submission status: Runtime Error

Solution code:
import itertools

import numpy as np

H, W, M = map(int, input().split())
HW = [list(map(int, input().split())) for _ in range(M)]
F = np.zeros([H, W], dtype=np.int64)

for h, w in HW:
    F[h - 1, w - 1] = 1

# 各方向の和を計算
A = np.sum(F, axis=0)
B = np.sum(F, axis=1)

maxA = np.max(A)
maxB = np.max(B)

# 最大値を取るindex
ind_A = np.arange(W)[A == maxA]
ind_B = np.arange(H)[B == maxB]

cnt = 0
ans = maxA + maxB
"""
for h, w in itertools.product(ind_B, ind_A):
    if F[h, w] == 0:
        break

    cnt += 1
    if cnt == M:
        ans -= 1
        break
else:
    ans -= 1
"""
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

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

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

L = MAP.sum( axis = 0 )
a = L.max()
LW = [ i for i, v in enumerate( L ) if v == a ]

L = MAP.sum( axis = 1 )
b = L.max()
LH = [ i for i, v in enumerate( L ) if v == b ]

maxV = 0
is_true = False
for i in LW:
  for j in LH:
    if MAP[ i ][ j ] == 0:
      is_true = True
      break
      
if is_true:
  print( int( a + b ))
else:
  print( int( a + b - 1 ))

Submission status: Runtime Error

Solution code:

def tc():
    h, w, m = map(int, input().split())
    targets = [tuple(map(int, input().split())) for _ in range(m)]

    rows = [0] * h
    cols = [0] * w
    grid = [[0] * w for _ in range(h)]

    for x, y in targets:
        x -= 1
        y -= 1
        rows[x] += 1
        cols[y] += 1
        grid[x][y] -= 1

    for i in range(h):
        for j in range(w):
            grid[i][j] += rows[i] + cols[j]

    print(max(max(r) for r in grid))


tc()

Submission status: Runtime Error

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

hw = [[0] * (W+1) for _ in range(H+1)]
 
w = [0] * (W+1)
h = [0] * (H+1)
mxw = []
mxw_v = 0
mxh = []
mxh_v = 0
 
 
for i in range(M):
    h_i, w_j = list(map(int, input().split()))
    hw[h_i][w_j] = 1
    h[h_i] += 1
    w[w_j] += 1
    if mxw_v < w[w_j]:
        mxw_v = w[w_j]
        mxw = [w_j]
    elif mxw_v == w[w_j]:
        mxw.append(w_j)
 
    if mxh_v < h[h_i]:
        mxh_v = h[h_i]
        mxh = [h_i]
    elif mxh_v == h[h_i]:
        mxh.append(h_i)
a = 0
d = 0
for i in mxh:
    for j in mxw:
        d = hw[i][j]
        a = max(a, mxh_v + mxw_v - d)
    if d == 0:
        break
print(a)

Submission status: Runtime Error

Solution code:
import itertools

import numpy as np

H, W, M = map(int, input().split())
HW = [list(map(int, input().split())) for _ in range(M)]
F = np.zeros([H, W], dtype=np.int64)

for h, w in HW:
    F[h - 1, w - 1] = 1

# 各方向の和を計算
A = np.sum(F, axis=0)
B = np.sum(F, axis=1)

maxA = np.max(A)
maxB = np.max(B)

# 最大値を取るindex
ind_A = np.arange(W)[A == maxA]
ind_B = np.arange(H)[B == maxB]

cnt = 0
ans = maxA + maxB
for h, w in itertools.product(ind_B, ind_A):
    if F[h, w] == 0:
        break

    cnt += 1
    if cnt == M:
        ans = maxA + maxB - 1
        break
else:
    ans = maxA + maxB - 1

print(ans)

Submission status: Runtime Error

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

hw = [[0] * (W+1) for _ in range(H+1)]
 
w = [0] * (W+1)
h = [0] * (H+1)
mxw = []
mxw_v = 0
mxh = []
mxh_v = 0
 
 
for i in range(M):
    h_i, w_j = list(map(int, input().split()))
    hw[h_i][w_j] = 1
    h[h_i] += 1
    w[w_j] += 1
    if mxw_v < w[w_j]:
        mxw_v = w[w_j]
        mxw = [w_j]
    elif mxw_v == w[w_j]:
        mxw.append(w_j)
 
    if mxh_v < h[h_i]:
        mxh_v = h[h_i]
        mxh = [h_i]
    elif mxh_v == h[h_i]:
        mxh.append(h_i)
a = 0
for i in mxh:
    for j in mxw:
        d = hw[i][j]
        a = max(a, mxh_v + mxw_v - d)
print(a)