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:
from collections import Counter
H, W, M = map(int, input().split())

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


S = Counter(h)
T = Counter(w)

if (M == 1):
    print(1)

else:
    if (S.most_common()[0][1] > T.most_common()[0][1]):
        smc = S.most_common()[0][0]
        for i in range(M):
            if (h[i] == smc):
                w[i] = 'boom'

        w1 = [s for s in w if s != 'boom']
        T = Counter(w1)
        tmc = T.most_common()[0][0]
        for i in range(len(w1)):
            if (w1[i] == tmc):
                w1[i] = 'boom' 

        w2 = [s for s in w1 if s != 'boom']
        print(M-len(w2))

    else:
        tmc = T.most_common()[0][0]
        for i in range(M):
            if (w[i] == tmc):
                h[i] = 'boom'

        h1 = [s for s in h if s != 'boom']
        S = Counter(h1)
        smc = S.most_common()[0][0]
        for i in range(len(h1)):
            if (h1[i] == smc):
                h1[i] = 'boom' 

        h2 = [s for s in h1 if s != 'boom']
        print(M-len(h2))

Submission status: Runtime Error

Solution code:
import numpy as np

def main():
    H,W,M = map(int,input().split())
    h = [[0,i] for i in range(H)]
    w = [[0,i] for i in range(W)]
    HW = np.zeros((H,W))
    X = []
    for i in range(M):
        tmp_0,tmp_1 = map(int,input().split())
        tmp_0 -= 1
        tmp_1 -= 1
        h[tmp_0][0] += 1
        w[tmp_1][0] += 1
        HW[tmp_0][tmp_1] += 10
        X.append([tmp_0,tmp_1])
    
    list.sort(h, reverse=True)
    list.sort(w, reverse=True)
    max_h = h[0][0]
    max_w = w[0][0]
    ans = max_h + max_w
    for i in range(len(h)):
        if h[i][0] < max_h:
            h[:i]
            break
        HW[h[i][1]] += 1
        
    for i in range(len(w)):
        if w[i][0] < max_w:
            w[:i]
            break
        HW[:,w[i][1]] += 1
    
    flag = True
    for x in X:
        if not(x[0] in h and x[1] in w):
            flag = False
            break
    
    if flag:
        ans -= 1
    
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
h, w, n = [int(i) for i in input().split()]
import numpy as np
maps = np.zeros((h, w))
for _ in range(n):
    tmp = [(int(i) -1) for i in input().split()]
    maps[tmp[0], tmp[1]] = 1
sum_row = [np.sum(maps[i, :]) for i in range(h)]
arg_max = np.argmax(sum_row)

col_max = 0
for i in range(w):
    tmp = np.sum(maps[:, i])
    if int(maps[arg_max, i]) == 1:
        tmp -= 1
    if col_max < tmp:
        col_max = tmp

print(int(np.max(sum_row) + col_max))

Submission status: Runtime Error

Solution code:
import numpy as np

H, W, M = map(long, input().split())
h = np.zeros(H)
w = np.zeros(W)
m = np.zeros((H, W))
ans = 3
'''
for _ in range(M):
    tmp_h, tmp_w = map(int, input().split())
    h[tmp_h-1] += 1
    w[tmp_w-1] += 1
    m[tmp_h-1, tmp_w-1] = 1.

h_max = np.max(h)
h_max_index = list(zip(*np.where(h == h_max)))
w_max = np.max(w)
w_max_index = list(zip(*np.where(w == w_max)))

for i in h_max_index:
    for j in w_max_index:
        if m[i[0]][j[0]] == 1.:
            tmp = h_max + w_max -1
        else:
            tmp = h_max + w_max
        if ans < tmp:
            ans = tmp
'''
print(int(ans))

Submission status: Runtime Error

Solution code:
H,W,M = map(int,input().split())
Hs = []
Ws = []
Hs_count = []
Ws_count = []
zahyo = []
for i in range(M):
    h,w = map(int,input().split())
    zahyo.append(f"{h} {w}")
    Hs.append(h)
    Ws.append(w)
for i in range(1,H+1):
    Hs_count.append(Hs.count(i))
for i in range(1,W+1):
    Ws_count.append(Ws.count(i))

n = len(maxIndex_H)*len(maxIndex_W)
for i in maxIndex_H:
    for j in maxIndex_W:
        if zahyo.count(f"{i+1} {j+1}") == 1:
            n -= 1

if n <= 0:
    print(max(Hs_count)+max(Ws_count)-1)
else:
    print(max(Hs_count)+max(Ws_count))

Submission status: Runtime Error

Solution code:
h, w, n = [int(i) for i in input().split()]
import numpy as np
maps = np.zeros((h, w))
for _ in range(n):
    tmp = [(int(i) -1) for i in input().split()]
    maps[tmp[0], tmp[1]] = 1
sum_row = [np.sum(maps[i, :]) for i in range(h)]
arg_max = np.argmax(sum_row)

col_max = 0
print()
for i in range(w):
    tmp = np.sum(maps[:, i])
    if int(maps[arg_max, i]) == 1:
        tmp -= 1
    if col_max < tmp:
        col_max = tmp

print(int(np.max(sum_row) + col_max))

Submission status: Runtime Error

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

B=np.zeros((H,W))
for _ in range(M):
    inp=[int(i) for i in input().split()]
    B[inp[0]-1][inp[1]-1]=1
BT=B.T
maxmax=0
for i in range(H):
    for j in range(W):
        sumsum=np.sum(B[i])+np.sum(BT[j])-B[i][j]
        if maxmax<sumsum:
            maxmax=sumsum
print(int(maxmax))

Submission status: Runtime Error

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

targets = [[0] * W for _ in range(H)]
n_target_h = [0] * H
n_target_w = [0] * W

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

h_max = max(n_target_h)
w_max = max(n_target_w)

bomb_h = [i + 1 for i, h in enumerate(n_target_h) if h == h_max]
bomb_w = [i + 1 for i, w in enumerate(n_target_w) if w == w_max]

ans = h_max + w_max - 1
for h in bomb_h:
    for w in bomb_w:
        if targets[h - 1][w - 1] == 0:
            ans = h_max + w_max
            break
    else:
        continue
    break

print(ans)

Submission status: Runtime Error

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

ListH = [0 for i in range(H)]
ListW = [0 for i in range(W)]

for m in range(M):
  h,w = map(int,input().split())
  ListH[h-1] = ListH[h-1] + 1
  ListW[w-1] = ListW[w-1] + 1

ListH.srot(reserse = True)
ListW.srot(reserse = True)

print(ListH(0) + ListW(0))

Submission status: Runtime Error

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
max_vh = numpy.argmax(hight)
max_vw = numpy.argmax(width)
f = 1
for i in range(m):
    if(matrix[max_vh][i] == 0):
        f = 0
        break
 
print(hight[max_vh] + width[max_vw]-f)

# 3 2 22
# 3 2
# 1 2

Submission status: Runtime Error

Solution code:
import collections
H,W,M=map(int,input().split())
h=[None]*M
w=[None]*M
for i in range(M):
    h[i],w[i]=map(int,input().split())
c1=collections.Counter(h)
c2=collections.Counter(w)
ans=0
cnt=0
while cnt<len(c1)-1:
    cnt2=0
    while cnt2<len(c2)-1:
        flag=True
        for x in range(M):
            if h[x]==c1.most_common()[cnt][0] and w[x]==c2.most_common()[cnt2][0]:
                ans=max((ans,c1.most_common()[cnt][1]+c2.most_common()[cnt2][1]-1))
                flag=False
                break
        if flag:
            ans=max((ans,c1.most_common()[cnt][1]+c2.most_common()[cnt2][1]))
        if c2.most_common()[cnt][1]>c2.most_common()[cnt+1][1]:
            break
        cnt2+=1
    if c1.most_common()[cnt][1]>c1.most_common()[cnt+1][1]:
        break
    cnt+=1
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

H, W, M = map(int, input().split())
h = np.zeros(H)
w = np.zeros(W)
m = np.zeros((H, W))
ans = 3
'''
for _ in range(M):
    tmp_h, tmp_w = map(int, input().split())
    h[tmp_h-1] += 1
    w[tmp_w-1] += 1
    m[tmp_h-1, tmp_w-1] = 1.

h_max = np.max(h)
h_max_index = list(zip(*np.where(h == h_max)))
w_max = np.max(w)
w_max_index = list(zip(*np.where(w == w_max)))

for i in h_max_index:
    for j in w_max_index:
        if m[i[0]][j[0]] == 1.:
            tmp = h_max + w_max -1
        else:
            tmp = h_max + w_max
        if ans < tmp:
            ans = tmp
'''
print(int(ans))

Submission status: Runtime Error

Solution code:
import numpy as np

[H, W, M] = map(int, input().split())
mp = np.zeros((H+1, W+1), dtype=np.int64)
#for i in range(M):
#    [h, w] = map(int, input().split())
#    mp[h][w] = 1

#colsum = np.sum(mp, axis=0)
#rowsum = np.sum(mp, axis=1)

#maxcol = max(colsum)
#maxrow = max(rowsum)

#colchk = np.where(colsum==maxcol)
#rowchk = np.where(rowsum==maxrow)

#for i in range(len(rowchk[0])):
#    for j in range(len(colchk[0])):
#        if mp[rowchk[0][i]][colchk[0][j]] == 0:
#            print(int(max(colsum)+max(rowsum)))
#            quit()

#print(int(max(colsum)+max(rowsum))-1)

Submission status: Runtime Error

Solution code:
#E
import numpy as np

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

num = np.zeros((h,w))

for i in range(m):
    a,b = map(int,input().split())
    num[a-1,b-1] = 1

Sumw = (np.sum(num , axis = 1))
point1  = np.argmax(Sumw)
Maxw = max(Sumw)

num[point1] = 0

Sumh = (np.sum(num , axis = 0))
Maxh = max(Sumh)

print(int(Maxw+Maxh))

Submission status: Runtime Error

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

ListH = [0 for i in range(H)]
ListW = [0 for i in range(W)]

for m in range(M):
  h,w = map(int,input().split())
  ListH[h-1] = ListH[h-1] + 1
  ListW[w-1] = ListW[w-1] + 1

ListH.srot()
ListW.srot()

print(ListH(H-1) + ListW(W-1))

Submission status: Runtime Error

Solution code:
import collections
H,W,M=map(int,input().split())
h=[None]*M
w=[None]*M
for i in range(M):
    h[i],w[i]=map(int,input().split())
c1=collections.Counter(h)
c2=collections.Counter(w)
ans=0
cnt=0
while cnt<len(c1)-1:
    cnt2=0
    while cnt2<len(c2)-1:
        flag=True
        for x in range(M):
            if h[x]==c1.most_common()[cnt][0] and w[x]==c2.most_common()[cnt2][0]:
                ans=max((ans,c1.most_common()[cnt][1]+c2.most_common()[cnt2][1]-1))
                flag=False
                break
        if flag:
            ans=max((ans,c1.most_common()[cnt][1]+c2.most_common()[cnt2][1]-1))
        if c2.most_common()[cnt][0]>c2.most_common()[cnt+1][0]:
            break
        cnt2+=1
    if c1.most_common()[cnt][0]>c1.most_common()[cnt+1][0]:
        break
    cnt+=1
print(ans)

Submission status: Runtime Error

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

def main():
    H, W, M = map(int, readline().rstrip().split())
    HW = [[0] * W for _ in range(H)]
    HB = [0] * H
    WB = [0] * W
    for _ in range(M):
        h, w = map(int, readline().rstrip().split())
        h -= 1
        w -= 1
        HW[h][w] = 1
        HB[h] += 1
        WB[w] += 1
    hb = max(HB)
    max_h = [i for i, x in enumerate(HB) if x == hb]
    wb = max(WB)
    max_w = [i for i, x in enumerate(WB) if x == wb]
    ch = False
    for mh in max_h:
        for mw in max_w:
            if HW[mh][mw] == 0:
                ch = True
                break
        if ch:
            break

    ans = hb + wb
    if not ch:
        ans -= 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

ListH = [0 for i in range(H)]
ListW = [0 for i in range(W)]

for m in range(M):
  h,w = map(int,input().split())
  ListH[h] = ListH[h-1] + 1
  ListW[w] = ListW[w-1] + 1

ListH.srot()
ListW.srot()

print(ListH(H-1) + ListW(W-1))

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
ch, cw = map(int, input().split())
dh_, dw_ = map(int, input().split())

mvs = [(-1,0),(1,0),(0,1),(0,-1)]
fs = []

for i in range(H):
    r = list(input())
    fs.append(r)

INF = 1<<50
dps = [[INF]*W for i in range(H)]
dps[ch-1][cw-1] = 0
from collections import deque
q = deque([])
q.append((ch-1,cw-1))
while len(q)>0:
    h,w = q.popleft()
    cnt = dps[h][w]
    # 先に魔法を使っておく
    for dh in range(-2,3):
        for dw in range(-2,3):
            hn = h + dh
            wn = w + dw
            if (dh,dw) in mvs:
                if 0<=hn<H and 0<=wn<W:
                    if dps[hn][wn] <= cnt:continue
                    if fs[hn][wn] == ".":
                        q.append((hn, wn))
                        dps[hn][wn] = min(dps[h][w], cnt)
            else:
                if 0<=hn<H and 0<wn<W:
                    if dps[hn][wn] <= cnt+1:continue
                    if fs[hn][wn] == ".":
                        q.appendleft((hn,wn))
                        dps[hn][wn] = min(dps[hn][wn], cnt + 1)

# for r in dps:
#     print(*r)
ans = dps[dh_-1][dw_-1]
print(ans if ans != INF else -1)

Submission status: Runtime Error

Solution code:
import sys


def read():
    return sys.stdin.readline().rstrip()


def main():
    h, w, m = map(int, read().split())
    bitboard = 0
    for _ in range(m):
        hi, wi = map(int, read().split())
        bitboard += 1 << (w * (hi - 1) + (wi - 1))
    ans = 0
    for hi in range(h):
        for wi in range(w):
            b = bitboard & (((2**w - 1) << hi * w) | (sum(1 << (w * i) for i in range(h)) << wi))
            ans = max(bin(b).count("1"), ans)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
h, w, m = map(int, input().split())
mapa = [[0]*w for i in range(h)]
strings = [0]*h
rows = [0]*w
for i in range(m):
    h1, w1 = map(int, input().split())
    mapa[h1-1][w1-1] = 1
    strings[h1-1] += 1
    rows[w1-1] += 1
maxs = max(strings)
maxr = max(rows)
summa = maxs + maxr - 1
fl = True
for i in range(h):
    if fl and strings[i] == maxs:
        for j in range(w):
            if rows[j] == maxr:
                if mapa[i][j] != 1:
                    summa = maxs + maxr
                    fl = False
                    break
print(summa)

Submission status: Runtime Error

Solution code:
import numpy as np

H,W,M=input().split()
H=int(H)
W=int(W)
M=int(M)
MA=np.zeros((H,W),dtype=int)
MX=[0]*H
MY=[0]*W
for i in range(M):
  x,y=input().split()
  MA[int(x)-1,int(y)-1]=1

MX=np.sum(MA,axis=1)
MY=np.sum(MA,axis=0)

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]

ii=min([MA[x,y] for x in ix for y in iy])

print(xmax+ymax-ii)

Submission status: Runtime Error

Solution code:
import numpy as np

[H, W, M] = map(int, input().split())
mp = np.zeros((H+1, W+1), dtype=np.int64)
#for i in range(M):
#    [h, w] = map(int, input().split())
#    mp[h][w] = 1

colsum = np.sum(mp, axis=0)
rowsum = np.sum(mp, axis=1)

#maxcol = max(colsum)
#maxrow = max(rowsum)

#colchk = np.where(colsum==maxcol)
#rowchk = np.where(rowsum==maxrow)

#for i in range(len(rowchk[0])):
#    for j in range(len(colchk[0])):
#        if mp[rowchk[0][i]][colchk[0][j]] == 0:
#            print(int(max(colsum)+max(rowsum)))
#            quit()

print(int(max(colsum)+max(rowsum))-1)

Submission status: Runtime Error

Solution code:
from heapq import heapify, heappop, heappush
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
input = sys.stdin.readline

H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())
Ch -= 1
Cw -= 1
Dh -= 1
Dw -= 1
S = [input().rstrip() for _ in [0]*H]
print(S[0])

INF = 10**10
D = [[INF]*W for _ in [0]*H]
used = set()

q = []
heappush(q, (0, Ch*W+Cw))

dirs = [(-1, 0), (1, 0), (0, 1), (0, -1)]
wps = [
    [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), (-1, -1), (-1, 1)],
    [(2, 2), (2, -1), (2, 0), (2, 1), (2, 2), (1, -1), (1, 1)],
    [(-2, 2), (-1, 2), (0, 2), (1, 2), (2, 2), (-1, 1), (1, 1)],
    [(-2, -2), (-1, -2), (0, -2), (1, -2), (2, -2), (-1, -1), (1, -1)]
]

while q:
    d, ij = heappop(q)
    i, j = divmod(ij, W)
    if D[i][j] > d:
        D[i][j] = d
    else:
        continue
    if i == Dh and j == Dw:
        break

    for k, (di, dj) in enumerate(dirs):
        _i = i + di
        _j = j + dj
        if 0 <= _i < H and 0 <= _j < W:
            if S[_i][_j] == '.':
                if D[_i][_j] <= d:
                    continue
                heappush(q, (d, _i*W+ _j))
            else:
                for di, dj in wps[k]:
                    _i = i + di
                    _j = j + dj
                    if 0 <= _i < H and 0 <= _j < W and S[_i][_j] == '.':
                        if D[_i][_j] <= d+1:
                            continue
                        heappush(q, (d+1, _i*W+_j))

ans = D[Dh][Dw]
print(ans if ans < INF else -1)

Submission status: Runtime Error

Solution code:
import numpy as np

H, W, M = map(int, input().split())
h = np.zeros(H)
w = np.zeros(W)
m = np.zeros(H, W)
ans = 3
'''
for _ in range(M):
    tmp_h, tmp_w = map(int, input().split())
    h[tmp_h-1] += 1
    w[tmp_w-1] += 1
    m[tmp_h-1, tmp_w-1] = 1.

h_max = np.max(h)
h_max_index = list(zip(*np.where(h == h_max)))
w_max = np.max(w)
w_max_index = list(zip(*np.where(w == w_max)))

for i in h_max_index:
    for j in w_max_index:
        if m[i[0]][j[0]] == 1.:
            tmp = h_max + w_max -1
        else:
            tmp = h_max + w_max
        if ans < tmp:
            ans = tmp
'''
print(int(ans))

Submission status: Runtime Error

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

ListH = [0 for i in range(H)]
ListW = [0 for i in range(W)]

for m in range(M):
  h,w = map(int,input().split())
  ListH[h] = ListH[h] + 1
  ListW[w] = ListW[w] + 1

ListH.srot()
ListW.srot()

print(ListH(H-1) + ListW(W-1))

Submission status: Runtime Error

Solution code:
import numpy as np

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

#colsum = np.sum(mp, axis=0)
#rowsum = np.sum(mp, axis=1)

#maxcol = max(colsum)
#maxrow = max(rowsum)

#colchk = np.where(colsum==maxcol)
#rowchk = np.where(rowsum==maxrow)

#for i in range(len(rowchk[0])):
#    for j in range(len(colchk[0])):
#        if mp[rowchk[0][i]][colchk[0][j]] == 0:
#            print(int(max(colsum)+max(rowsum)))
#            quit()

print(int(max(colsum)+max(rowsum))-1)

Submission status: Runtime Error

Solution code:
import numpy as np
H, W, M = list(map(int, input().split()))
row = np.zeros(H)
col = np.zeros(W)
#maps = np.zeros((M, 2), dtype=np.int64)
for i in range(M):
    h, w =list(map(int, input().split()))
    row[h-1] += 1
    col[w-1] += 1
    maps[i] = [h - 1, w - 1]

rowMax = row.max()
colMax = col.max()
rowMaxs = np.where(row == rowMax)
colMaxs = np.where(col == colMax)
d = 1
'''
for i in rowMaxs[0]:
    for j in colMaxs[0]:
        if not np.any(np.all(maps == [i, j], axis = 1)):
            d = 0
            break
    if d == 0:
        break
'''

print(int(rowMax + colMax - d))

Submission status: Runtime Error

Solution code:
h,w,m=map(int,input().split())
bomb=[list(map(int,input().split())) for _ in range(m)]
a=[0 for i in range(h+1)]
b=[0 for _ in range(w+1)]
for i in range(m):
    a[bomb[i][0]]+=1
    b[bomb[i][1]]+=1
am=max(a)
bm=max(b)
aa=set([i for i, x in enumerate(a) if x==am])
bb=set([i for i, x in enumerate(b) if x==bm])
c=[0 for i in range(m)]
d=[0 for i in range(m)]
for x in bomb:
        c[x[0]-1]+=1
        d[x[1]-1]+=1
cm=max(c)
dm=max(d)
aaa=len(aa)
bbb=len(bb)
if cm<bbb or dm<aaa:
    print(am+bm)
else:
    print(am+bm-1)

Submission status: Runtime Error

Solution code:
import numpy as np
from collections import Counter

H, W, M = [int(n) for n in input().split()]
h = []
w = []
table = np.zeros((H, W))
for m in range(M):
    tmp = [int(n) for n in input().split()]
    h.append(tmp[0])
    w.append(tmp[1])
    table[tmp[0]-1][tmp[1]-1] = 1

h_counter = Counter(h)
w_counter = Counter(w)

most_common_h_list = []
most_common_w_list = []

most_common_h_count = 0
for k, v in h_counter.most_common():
    if v < most_common_h_count:
        break
    most_common_h_list.append(k)
    most_common_h_count = v

most_common_w_count = 0
for k, v in w_counter.most_common():
    if v < most_common_w_count:
        break
    most_common_w_list.append(k)
    most_common_w_count = v

ans = 0
finished = False
for hh in most_common_h_list:
    if finished:
        break
    for ww in most_common_w_list:
        if table[hh-1][ww-1] == 1:
            tt = most_common_h_count + most_common_w_count - 1
            if tt > ans:
                ans = tt
        else:
            tt = most_common_h_count + most_common_w_count
            if tt > ans:
                ans = tt
            finished = True
            break

print(ans)

Submission status: Runtime Error

Solution code:
def resolve():
    h,w,m = map(int, input().split())
    mine = []
    mineh = [0]*h
    minew = [0]*w
    for i in range(m):
        a,b = map(int, input().split())
        a -= 1
        b -= 1
        mine.append((a,b))
        mineh[a] += 1
        minew[b] += 1
    mine = set(mine)
    maxh = max(mineh)
    hnum = []
    maxw = max(minew)
    wnum = []
    for i in range(h):
        if mineh[i] == maxh:
            hnum.append(i)
        if minew[i] == maxw:
            wnum.append(i)
    hnum = set(hnum)
    wnum = set(wnum)
    NO = 0
    for i, j in mine:
        if i in hnum and j in wnum:
            NO += 1
    if len(hnum)*len(wnum) > NO:
        print(maxw+maxh)
    else:
        print(maxw+maxh-1)
resolve()

Submission status: Runtime Error

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

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


S = Counter(h)
T = Counter(w)

if (M == 1):
    print(1)

else:

    if (S.most_common()[0][1] > T.most_common()[0][1]):
        smc = S.most_common()[0][0]
        for i in range(M):
            if (h[i] == smc):
                w[i] = 'boom'

        w1 = [s for s in w if s != 'boom']
        T = Counter(w1)
        tmc = T.most_common()[0][0]
        for i in range(len(w1)):
            if (w1[i] == tmc):
                w1[i] = 'boom' 

        w2 = [s for s in w1 if s != 'boom']
        print(M-len(w2))

    else:
        tmc = T.most_common()[0][0]
        for i in range(M):
            if (w[i] == tmc):
                h[i] = 'boom'

        h1 = [s for s in h if s != 'boom']
        S = Counter(h1)
        smc = S.most_common()[0][0]
        for i in range(len(h1)):
            if (h1[i] == smc):
                h1[i] = 'boom' 

        h2 = [s for s in h1 if s != 'boom']
        print(M-len(h2))

Submission status: Runtime Error

Solution code:
import numpy as np

H, W, N = list(map(int, input().rstrip().split(' ')))

dist = np.zeros([H, W])

for _ in range(N):
    h, w = list(map(int, input().rstrip().split(' ')))
    dist[h - 1, w - 1] = 1
        
w_sum = dist.sum(axis=0)
h_sum = dist.sum(axis=1)

new_dist = np.tile(w_sum, H).reshape(H, W) + np.tile(h_sum, W).reshape(W, H).T - dist

#print(dist)
#print(new_dist)

print(int(new_dist.max()))

Submission status: Runtime Error

Solution code:
h,w,m=map(int, input().split())
r = [0]*h
c = [0]*w

for i in range(m):
    x,y=map(int, input().split())
    r[x]+=1
    c[y]+=1

print(r,c,max(r)+max(c))

Submission status: Runtime Error

Solution code:
#abc176-e
import sys
input = sys.stdin.readline
H,W,M=list(map(int,input().split()))

point_bom = [[0]*(W+1) for _ in range(H+1)]
for _ in range(M):
    h,w = map(int,input().split())
    point_bom[h][w]=1
#point_bom = np.array(point_bom)
#row_bom = point_bom.sum(axis=0)
#col_bom = point_bom.sum(axis=1)
row_bom = []
for rb in point_bom:
    row_bom.append(sum(rb))
col_bom = [sum(x) for x in zip(*point_bom)]
    
max_row = max(row_bom)
max_col = max(col_bom)

ind_max_row =[]
ind_max_col = []

for i in range(len(row_bom)):
    if row_bom[i] == max_row:
         ind_max_row.append(i)
for i in range(len(col_bom)):
    if col_bom[i] == max_col:
        ind_max_col.append(i)

flag = 1#爆破対象が置かれる　おかれている＝１　いない＝０
for mr in ind_max_row:
    for mc in ind_max_col:
        if point_bom[mr][mc] == 0:
            flag = 0
            break
    if flag == 0:
        break
print(max_row+max_col - flag)

Submission status: Runtime Error

Solution code:
def main(n, m, c, lst):
	lst1 = [[0]*m for _ in range(n)]

	for i in lst:
		lst1[i[0]][i[1]] = 1

	col, row = [], []
	for i in range(n):
		if i == 0 or i == n-1:
			if lst1[i][0] == 1 and lst1[i][m-1] == 1:
				col.append(sum(lst1[i])-2)
			elif lst1[i][0] == 1 or lst1[i][m-1] == 1:
				col.append(sum(lst1[i])-1)
			else:
				col.append(sum(lst1[i]))
		else:
			col.append(sum(lst1[i]))

	for j in range(m):
		summ = 0
		for i in range(n):
			summ += lst1[i][j]
		row.append(summ)

	return max(row) + max(col)


n, m, c = list(map(int, input().split()))
lst = []
for i in range(c):
	a, b = list(map(int, input().split()))
	lst.append([a-1, b-1])
print(main(n, m, c, lst))

Submission status: Runtime Error

Solution code:
import sys
h,w,m=map(int, input().split())
hlist=[0]*h
wlist=[0]*w
hw=[[0]*w for i in range(h)]
for i in range(m):
  x,y=map(int, input().split())
  hlist[x-1]+=1
  wlist[y-1]+=1
  hw[x-1][y-1]=1
maxh=max(hlist)
maxw=max(wlist)
hlist1=[]
wlist1=[]
for i in range(h):
  if hlist[i]==maxh:
    hlist1.append(i)
for i in range(w):
  if wlist[i]==maxw:
    wlist1.append(i)
for i in hlist1:
  for j in wlist1:
    if hw[i][j]==0:
      print(maxh+maxw)
      sys.exit()
print(max(maxh+maxw-1,-1))

Submission status: Runtime Error

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

ListH = [0 for i in range(H)]
ListW = [0 for i in range(W)]

for m in range(M):
  h,w = map(int,input().split())
  ListH[h] = ListH[h] + 1
  ListW[w] = ListW[w] + 1

maxcount = 0
tmpcount = 0

for pi in range(H):
  for pj in range(W):
    tmpcount =  ListH[pi] + ListW[pj]

    if maxcount < tmpcount:
      maxcount = tmpcount
print(maxcount)

Submission status: Runtime Error

Solution code:
import sys
H, W, M = map(int, input().split())
hList = {i+1: 0 for i in range(H)}
wList = {i+1: 0 for i in range(W)}
pList = set()
#print(hList, wList, pList)
for _ in range(M):
  h, w = list(map(int, input().split()))
  #print(h, w)
  hList[h] += 1
  wList[w] += 1
  pList.add((h, w))
#print(hList, wList, pList)

maxCount = max(hList.values())
maxHList = [h for h, count in hList.items() if count==maxCount]
if maxCount==0:
  print(0)
  sys.exit(0)


maxCount = max(wList.values())
maxWList = [w for w, count in wList.items() if count==maxCount]

#counterList = []
#for maxH in maxHList:
#  for maxW in maxWList:
#    counterList.append(hList[maxH] + wList[maxW] - (1 if ((maxH, maxW) in pList) else 0))
#print(max(counterList))
#print(max(((hList[maxH] + wList[maxW] - (1 if ((maxH, maxW) in pList) else 0)) for maxH in maxHList for maxW in maxWList)))
print(max(((hList[maxH] + wList[maxW] - (1 if ((maxH, maxW) in pList) else 0)) for maxH in maxHList for maxW in maxWList)))
for maxH, maxW in (maxH, maxW for maxH in maxHList for maxW in maxWList):
	if (maxH, maxW) in pList:
		print(hList[maxH] + wList[maxW] - 1)
		sys.exit(0)

print(hList[maxH] + wList[maxW])

Submission status: Runtime Error

Solution code:
import numpy as np

def main():
    H,W,M = map(int,input().split())
    h = [[0,i] for i in range(H+1)]
    w = [[0,i] for i in range(W+1)]
    HW = np.zeros((H+1,W+1))
    for i in range(M):
        tmp_0,tmp_1 = map(int,input().split())
        tmp_0 -= 1
        tmp_1 -= 1
        h[tmp_0][0] += 1
        w[tmp_1][0] += 1
        HW[tmp_0][tmp_1] += 10
    
    list.sort(h, reverse=True)
    list.sort(w, reverse=True)
    max_h = h[0][0]
    max_w = w[0][0]
    ans = max_h + max_w
    for i in range(len(h)):
        if h[i][0] < max_h:
            break
        HW[h[i][1]] += 1
        
    for i in range(len(w)):
        if w[i][0] < max_w:
            break
        HW[:,w[i][1]] += 1
    
    if 2 not in HW:
        ans -= 1
    
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#abc176-e
import sys
input = sys.stdin.readline
H,W,M=list(map(int,input().split()))

point_bom = [[0]*(W+1) for _ in range(H+1)]
for _ in range(M):
    h,w = map(int,input().split())
    point_bom[h][w]=1
#point_bom = np.array(point_bom)
#row_bom = point_bom.sum(axis=0)
#col_bom = point_bom.sum(axis=1)
row_bom = []
for rb in point_bom:
    row_bom.append(sum(rb))
col_bom = [sum(x) for x in zip(*point_bom)]
    
max_row = max(row_bom)
max_col = max(col_bom)

ind_max_row =[]
ind_max_col = []

for i in range(len(row_bom)):
    if row_bom[i] == max_row:
         ind_max_row.append(i)
for i in range(len(col_bom)):
    if col_bom[i] == max_col:
        ind_max_col.append(i)

flag = 1#爆破対象が置かれる　おかれている＝１　いない＝０
for mr in ind_max_row:
    for mc in ind_max_col:
        if point_bom[mc][mr] == 0:
            flag = 0
            break
    if flag == 0:
        break
print(max_row+max_col - flag)

Submission status: Runtime Error

Solution code:
import numpy as np

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

colsum = np.sum(mp, axis=0)
rowsum = np.sum(mp, axis=1)

#maxcol = max(colsum)
#maxrow = max(rowsum)

#colchk = np.where(colsum==maxcol)
#rowchk = np.where(rowsum==maxrow)

#for i in range(len(rowchk[0])):
#    for j in range(len(colchk[0])):
#        if mp[rowchk[0][i]][colchk[0][j]] == 0:
#            print(int(max(colsum)+max(rowsum)))
#            quit()

print(int(max(colsum)+max(rowsum))-1)

Submission status: Runtime Error

Solution code:
import sys
H, W, M = map(int, input().split())
hList = {i+1: 0 for i in range(H)}
wList = {i+1: 0 for i in range(W)}
pList = set()
#print(hList, wList, pList)
for _ in range(M):
  h, w = list(map(int, input().split()))
  #print(h, w)
  hList[h] += 1
  wList[w] += 1
  pList.add((h, w))
#print(hList, wList, pList)

maxCount = max(hList.values())
maxHList = [h for h, count in hList.items() if count==maxCount]
if maxCount==0:
  print(0)
  sys.exit(0)

#h決め打ちでwを見る
for w in wList:
  

maxCount = max(wList.values())
maxWList = [w for w, count in wList.items() if count==maxCount]

#counterList = []
#for maxH in maxHList:
#  for maxW in maxWList:
#    counterList.append(hList[maxH] + wList[maxW] - (1 if ((maxH, maxW) in pList) else 0))
#print(max(counterList))
#print(max(((hList[maxH] + wList[maxW] - (1 if ((maxH, maxW) in pList) else 0)) for maxH in maxHList for maxW in maxWList)))
print(max(((hList[maxH] + wList[maxW] - (1 if ((maxH, maxW) in pList) else 0)) for maxH in maxHList for maxW in maxWList)))
for maxH, maxW in (maxH, maxW for maxH in maxHList for maxW in maxWList):
	if (maxH, maxW) in pList:
		print(hList[maxH] + wList[maxW] - 1)
		sys.exit(0)

print(hList[maxH] + wList[maxW])