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
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)

maxHCount = max(hList.values())
maxHList = [h for h, count in hList.items() if count==maxHCount]


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

#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 in maxHList:
  for maxW in maxWList:
    if (maxH, maxW) in pList:
      print(maxHCount + maxWList - 1)
      sys.exit(0)

print(maxHCount + maxWList)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
 
h,w,m = list(map(int,input().split()))

R = [0]*h
C = [0]*w

G = [[0]*w for _ in range(h)]


for _ in range(m):
  a,b = list(map(int,input().split()))
  a,b = a-1, b-1
  R[a] += 1
  C[b] += 1
  G[a][b] = 1
  
best = 0
for i in range(h):
  for j in range(w):
    best= max(best,R[i]+C[j] - G[i][j])
    
print(best)

Submission status: Runtime Error

Solution code:
import numpy as np

H,W,N = [int(i) for i in input().split()]
varriers = [[int(i)-1 for i in input().split()] for i in range(N)]
board = np.array([[0 for _ in range(W)] for i in range(H)], dtype=int)

for varrier in varriers:
    board[:, varrier[0]] += 1
    board[varrier[1], :] += 1
    board[varrier[1], varrier[0]] -= 1

print(board.max())

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)

maxHCount = max(hList.values())
maxHList = [h for h, count in hList.items() if count==maxHCount]


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

#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 in maxHList:
  for maxW in maxWList:
    if (maxH, maxW) in pList:
      print(maxHCount + maxWList - 1)
      sys.exit(0)

print(maxHCount + maxWList])

Submission status: Runtime Error

Solution code:
from sys import stdin, setrecursionlimit,stdout
from collections import deque, defaultdict,Counter
from math import log2, log, ceil,sqrt
from operator import *
input = stdin.readline
setrecursionlimit(int(2e5))
def getstr(): return input()[:-1]
def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint1(): return list(map(lambda x : int(x) - 1, input().split()))
jn = lambda x,l: x.join(map(str,l))
# swap_array function
def swaparr(arr, a,b):
    temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp
 
## gcd function
def gcd(a,b):
    if a == 0:
        return b
    return gcd(b%a, a)
 
## nCr function efficient using Binomial Cofficient
def nCr(n, k): 
    if(k > n - k): 
        k = n - k 
    res = 1
    for i in range(k): 
        res = res * (n - i) 
        res = res / (i + 1) 
    return res 
 
## upper bound function code -- such that e in a[:i] e < x;
def upper_bound(a, x, lo=0):
    hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if a[mid] < x:
            lo = mid+1
        else:
            hi = mid
    return lo
 

# sliding window ---

def maxSlidingWindow(nums,k):
    windowSum,maxSum = 0,0
    windowSum = sum(nums[:k])
    for end in range(k,len(nums)):
        windowSum += nums[end]-nums[end - k]
        maxSum = max(maxSum,windowSum)
    return maxSum


# Two Pointer 
def twoSum(nums, target):
        left = 0
        right = len(nums)-1
        while left <= right:
            if nums[left]+nums[right] == target:
                return [left,right]
            elif nums[left]+nums[right] < target:
                left +=  1
            else:
                right -= 1
        return 

# prefix sum

def prefix(arr):
    a = map(add,arr.insert(0,0))
    return a

# Modular Inverse
# y-1 = y(p-2)%p

# String Matching





## prime factorization
def primefs(n):
    ## if n == 1    ## calculating primes
    primes = {}
    while(n%2 == 0):
        primes[2] = primes.get(2, 0) + 1
        n = n//2
    for i in range(3, int(n**0.5)+2, 2):
        while(n%i == 0):
            primes[i] = primes.get(i, 0) + 1
            n = n//i
    if n > 2:
        primes[n] = primes.get(n, 0) + 1
    ## prime factoriazation of n is stored in dictionary
    ## primes and can be accesed. O(sqrt n)
    return primes
 
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p): 
    res = 1
    x = x % p  
    if (x == 0) : 
        return 0
    while (y > 0) : 
        if ((y & 1) == 1) : 
            res = (res * x) % p 
        y = y >> 1      
        x = (x * x) % p 
    return res 
 
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
    temp = a
    a = b
    b = temp
    return a,b
 
# find function with path compression included (recursive)
# def find(x, link):
#     if link[x] == x:
#         return x
#     link[x] = find(link[x], link);
#     return link[x];
 
# find function with path compression (ITERATIVE)
def find(x, link):
    p = x;
    while( p != link[p]):
        p = link[p];
    
    while( x != p):
        nex = link[x];
        link[x] = p;
        x = nex;
    return p;
 
 
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
    x = find(x, link)
    y = find(y, link)
    if size[x] < size[y]:
        x,y = swap(x,y)
    if x != y:
        size[x] += size[y]
        link[y] = x
 
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n): 
    prime = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime[p] == True): 
            for i in range(p * p, n+1, p):
                prime[i] = False
        p += 1
    return prime


# Binary Search functions
def binsearch(a, l, r, x):
    while l <= r:
        mid = l + (r-1)//2
        if a[mid]:
            return mid
        elif a[mid] > x:
            l = mid-1
        else:
            r = mid+1
    return -1


# Grafh related queries : 



graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000
 
 
def bfs(d, v):
    q = []
    q.append(v)
    visited[v] = 1
    while len(q) != 0:
        x = q[0]
        q.pop(0)
        for i in d[x]:
            if visited[i] != 1:
                visited[i] = 1
                q.append(i)
        print(x)
 
 
def make_graph(e):
    d = {}
    for i in range(e):
        x, y = mi()
        if x not in d:
            d[x] = [y]
        else:
            d[x].append(y)
        if y not in d:
            d[y] = [x]
        else:
            d[y].append(x)
    return d
 
 
def gr2(n):
    d = defaultdict(list)
    for i in range(n):
        x, y = mi()
        d[x].append(y)
    return d
 
 
def connected_components(graph):
    seen = set()
 
    def dfs(v):
        vs = set([v])
        component = []
        while vs:
            v = vs.pop()
            seen.add(v)
            vs |= set(graph[v]) - seen
            component.append(v)
        return component
 
    ans = []
    for v in graph:
        if v not in seen:
            d = dfs(v)
            ans.append(d)
    return ans

# End graph related queries
 
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e6 + 5)
def spf_sieve():
    spf[1] = 1;
    for i in range(2, MAXN):
        spf[i] = i;
    for i in range(4, MAXN, 2):
        spf[i] = 2;
    for i in range(3, ceil(MAXN ** 0.5), 2):
        if spf[i] == i:
            for j in range(i*i, MAXN, i):
                if spf[j] == j:
                    spf[j] = i;
    ## function for storing smallest prime factors (spf) in the array
 
################## un-comment below 2 lines when using factorization #################
spf = [0 for i in range(MAXN)] 
spf_sieve() 
def factoriazation(x):
    ret = {};
    while x != 1:
        ret[spf[x]] = ret.get(spf[x], 0) + 1;
        x = x//spf[x]
    return ret
    ## this function is useful for multiple queries only, o/w use
    ## primefs function above. complexity O(log n)
 
## taking integer array input


# LCM of two numbers :
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
 
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
y, n = "Yes", "No"
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12,
       'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24,
       'z': 25}


# used for testing your code 

# inputs=open('input.txt','r') 
# L I S
def solve():
    h,w,m = getints()
    l = [[-1]*w for i in range(h)]
    for _ in range(m):
        hi,wi = getints()
        l[hi-1][wi-1] = 0
    count = 0
    for i in range(h):
        for j in range(w):
            pr = 0
            for k in range(h):
                if k != j:
                    if l[k][j] == 0:
                        pr+=1
            for pl in range(w):
                if pl != i:
                    if l[i][pl] == 0:
                        pr+=1
            count = max(count,pr + 1 ) if l[i][j] == 0 else max(count,pr)
    print(count)




if __name__ == "__main__":
    # solve()
    # for t in range(getint()):
    #     print("Case #{}: ".format(t + 1), end="")
    #     solve()
    
    solve()

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 LH:
  for j in LW:
    if MAP[ i ][ j ] == 0:
      is_true = True
      break
      
if not is_true: #間違っていることはわかっている
  print( int( a + b ))
else:
  print( int( a + b - 1 ))

Submission status: Runtime Error

Solution code:
H, W, M = map(int, input().split())
S = [[0]*W for _ in range(H)]
lh = [0]*H
lw = [0]*W
l = []
for _ in range(M):
  h, w = map(lambda x: int(x)-1, input().split())
  lh[h] += 1
  lw[w] += 1
  l.append((h, w))

maxh = max(lh)
maxw = max(lw)
cnt = 0

ch = lh.count(maxh)
cw = lw.count(maxw)
for y, x in l:
  if lh[y] == maxh and lw[x] == maxw:
    cnt += 1

ans = maxh+maxw
if cnt == ch*cw:
  ans -= 1

print(ans)

Submission status: Runtime Error

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

targets = []
n_target_h = [0] * H
n_target_w = [0] * W

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

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 not (h, w) in targets:
            ans = h_max + w_max
            break
    else:
        continue
    break

print(ans)

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
h_max = 0
w_max = 0

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(h_max, n_target_h[h - 1])
    w_max = max(w_max, n_target_w[w - 1])

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:
import sys
sys.setrecursionlimit(2147483647)
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(int, input().split())
#h = np.zeros(H)
#w = np.zeros(W)
m = np.zeros((3*10**4, 3*10**4))
#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 = map(int,input().split())
ch,cw = map(int,input().split())
dh,dw = map(int,input().split())
s = [0]*h
for i in range(h):
	s[i] =  input()
	s[i] = list(s[i])
 
def clear_maze(sx, sy, gx, gy, maze):
 
	INF = 100000000
 
	field_x_length = len(maze)
	field_y_length = len(maze[0])
	distance = [[INF for i in range(field_y_length)] for j in range(field_x_length)]
#	distance = [[INF]*field_y_length]*field_x_length
 
	def bfs():
		count = 0
		queue = []
 
		queue.insert(0, (sx, sy))
 
		distance[sx][sy] = 0
 
		while len(queue):
			x, y = queue.pop()
 
			if x == gx and y == gy:
				break
 
			for i in range(0, 24):
				if i <= 3:
					nx, ny = x + [1, 0, -1, 0][i], y + [0, 1, 0, -1][i]
				if i >= 4:
					nx, ny = x + [-1, 1, 1, -1, -2, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, -2, -2][i-4], y + [1, 1, -1, -1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, -2, -2, -2, -1, 0, 1][i-4]
				if (i <= 3 and 0 <= nx and nx < field_x_length and 0 <= ny and ny < field_y_length and maze[nx][ny] != '#' and distance[nx][ny] == INF):
					count += 1
					queue.insert(0, (nx, ny))
					distance[nx][ny] = distance[x][y]
				if (i >= 4 and count ==0 and 0 <= nx and nx < field_x_length and 0 <= ny and ny < field_y_length and maze[nx][ny] != '#' and distance[nx][ny] == INF):
					queue.insert(0, (nx, ny))
					distance[nx][ny] = distance[x][y] + 1
			count = 0
		return distance[gx][gy]
	return bfs()
if clear_maze(ch-1, cw-1, dh-1, dw-1, s) == 100000000:
	print(-1)
else:
	print(clear_maze(ch-1, cw-1, dh-1, dw-1, s))

Submission status: Runtime Error

Solution code:
h,w,m=map(int,input().split())
bh=[0]*h
bw=[0]*w
ah=[]
aw=[]
ch=[]
cw=[]
for i in range(m):
    hi,wi=map(int,input().split())
    bh[hi-1]+=1
    bw[wi-1]+=1
    ah.append(hi)
    aw.append(wi)
ans=0
for i in range(h):
    if bh[i]==max(bh):
        ch.append(i+1)
    if bw[i]==max(bw):
        cw.append(i+1)
if len(cw)*len(ch)>m:
    print(max(bh)+max(bw))
else:
    cnt=0
    for i in range(m):
        if ah[i] in ch and aw[i] in cw:
            cnt+=1
    if cnt==len(ch)*len(cw):
        print(max(bh)+max(bw)-1)
    else:
        print(max(bh)+max(bw))

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((3*10**5, 3*10**5))
#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:
def main():
    H, W, M = map(int, input().split())
    bombs = [list(map(int, input().split())) for i in range(M)]
    
    if M == 1:
        return 1
    
    hw = [0] * H
    wh = [0] * W
    
    for i in range(len(bombs)):
        [h, w] = bombs[i]
        hw[h] += 1
        wh[w] += 1
    
    hw_tuple_list = []
    for i in range(len(hw)):
        hw_tuple_list.append((sum(hw[i]), i+1))
    
    wh_tuple_list = []
    for i in range(len(wh)):
        wh_tuple_list.append((sum(wh[i]), i+1))

    h_list = sorted(hw_tuple_list, reverse = True)
    w_list = sorted(wh_tuple_list, reverse = True)

    h_max = h_list[0][0]
    h_idx_list = []
    for i in range(len(h_list)):
        if h_max > h_list[i][0]:
            break
        h_idx_list.append(h_list[i][1])

    w_max = w_list[0][0]
    w_idx_list = []
    for i in range(len(w_list)):
        if w_max > w_list[i][0]:
            break
        w_idx_list.append(w_list[i][1])
    
    cnt = 1
    for i in range(len(h_idx_list)):
        for j in range(len(w_idx_list)):
            if hw[h_idx_list[i] - 1][w_idx_list[j] - 1] != 1:
                cnt = 0
                break
    return w_max + h_max - cnt

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

Submission status: Runtime Error

Solution code:
H,W,M = map(int,input().split())
P = [0]*(H+1)
Q = [0]*(W+1)
A = []
for k in range(M):
  a,b = map(int,input().split())
  P[a] += 1
  Q[b] += 1
  A.append(a,b)
Pindex = [i for i, v in enumerate(P) if v == max(P)]
Qindex = [i for i, v in enumerate(Q) if v == max(Q)]
Pmax = P[Pindex[0]]
Qmax = Q[Qindex[0]]
ans = Pmax+Qmax-1
key = 0
for x in Pindex:
  for y in Qindex:
    if (x,y) not in A:
      ans += 1
      key = 1
      break
  if key == 1:
    break
print(ans)

Submission status: Runtime Error

Solution code:
H, W, M = map(int, input().split())
S = [[0]*W for _ in range(H)]
dich = {i: 0 for i in range(H)}
dicw = {i: 0 for i in range(W)}
l = []
for _ in range(M):
  h, w = map(lambda x: int(x)-1, input().split())
  dich[h] += 1
  dicw[w] += 1
  l.append((h, w))

maxh = max(dich.values())
maxw = max(dicw.values())
cnt = 0

ch = sum([1 for i in dich.values() if i == maxh])
cw = sum([1 for i in dicw.values() if i == maxw])
for y, x in l:
  if dich[y] == maxh and dicw[x] == maxw:
    cnt += 1

ans = maxh+maxw
if cnt == ch*cw:
  ans -= 1

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

(H,W,M) = map(int, input().split())
# hsum = [0]*H
# wsum = [0]*W
grid = np.zeros((H,W), dtype='int')
for _ in range(M):
    h,w = map(int, input().split())
    grid[h-1][w-1] = 1

row = grid.sum(axis=0)
col = grid.sum(axis=1)

cm = col.max()
rm = row.max()
if grid[col == cm, row == rm].min() == 0:
    print(cm+rm)
else:
    print(cm+rm-1)

Submission status: Runtime Error

Solution code:
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)]
    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
        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
        
    for i in range(len(w)):
        if w[i][0] < max_w:
            w[:i]
            break
    
    flag = True
    for x in X:
        if not((x[0] in h[:,1]) and (x[1] in w[:,1])):
            flag = False
            break
    
    if flag:
        ans -= 1
    
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
H,W,M = map(int,input().split())
P = [0]*(H+1)
Q = [0]*(W+1)
#A = [[0 for j in range(W+1)] for i in range(H+1)]
for k in range(M):
  a,b = map(int,input().split())
  P[a] += 1
  Q[b] += 1
  #A[a][b] = 1
#print(P,Q,A,Pmax,Qmax,ans)
Pindex = [i for i, v in enumerate(P) if v == max(P)]
Qindex = [i for i, v in enumerate(Q) if v == max(Q)]
Pmax = P[Pindex[0]]
Qmax = Q[Qindex[0]]
ans = Pmax+Qmax-1
key = 0
for x in Pindex:
  for y in Qindex:
    if A[x][y] == 0:
      ans += 1
      key = 1
      break
  if key == 1:
    break
print(ans)

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 = [0] * W
    Hs = [0] * H
    for i in range(W):
        Ws[i] = W_N_bomb[i][0]
    for i in range(H):
        Hs[i] = H_N_bomb[i][0]

    pos_x= bisect.bisect_left(Ws, Ws[-1])
    pos_y= bisect.bisect_left(Hs, Hs[-1])
    #print(W_N_bomb, H_N_bomb, pos_x, pos_y)
    flag = False
    value = Ws[-1] + Hs[-1]
    for i in range(pos_x, W):
        for j in range(pos_y, H):
            pos_i = W_N_bomb[i][1]
            pos_j = H_N_bomb[j][1]
            if(bombs[pos_j][pos_i]==0):
                flag=True
                break
    if(flag==True):
        print(value)
    else:
        print(value-1)

Submission status: Runtime Error

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

# from typing import Optional
from typing import List

# from typing import Dict
from typing import Tuple

# from typing import Sequence

Cod = Tuple[int, int]


def main():
    args = input().split()
    H = int(args[0])
    W = int(args[1])
    M = int(args[2])

    codList: List[Cod] = []
    for i in range(M):
        args = input().split()
        codList.append((int(args[0]), int(args[1])))

    columnSumList: List[int] = [0] * (H + 1)
    rowSumList: List[int] = [0] * (W + 1)

    for item in codList:
        columnSumList[item[1] - 1] += 1
        rowSumList[item[0] - 1] += 1

    maxColumnSum = max(columnSumList)
    maxColumIndexList = [i for i, x in enumerate(columnSumList) if x == maxColumnSum]
    maxRowSum = max(rowSumList)
    maxRowIndexList = [i for i, x in enumerate(rowSumList) if x == maxRowSum]

    result = maxColumnSum + maxRowSum
    for row in maxRowIndexList:
        for column in maxColumIndexList:
            if (row + 1, column + 1) in codList:
                continue
            else:
                print(result)
                return

    print(result - 1)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
from sys import flags

h, w, m = map(int, input().split())
row = [0]*h
col = [0]*w
bomb = set()
for x in sys.stdin.readlines():
    H, W = map(int, input().split())
    bomb.add((H-1, W-1))
    row[H-1] += 1
    col[W-1] += 1

maxrow = max(row)
maxcol = max(col)
ans = maxcol + maxrow - 1

p, q = [], []
for i in range(m):
    if bomb[i][0] == maxrow:
        p.append(i)
    if bomb[i][1] == maxcol:
        q.append(i)

for i in p:
    for j in q:
        if (i, j) not in bomb:
            print(ans + 1)
            exit()
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

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

h_list = [0]+[0 for i in range(H)]
w_list = [0]+[0 for i in range(W)] 
target_set = set()
for i in range(M):
    h,w = map(int, input().split())
    h_list[h] += 1
    w_list[w] += 1
    target_set.add((h,w))

h_np = np.array(h_list)
w_np = np.array(w_list)

max_h_value = max(h_list)
max_w_value = max(w_list)

ans = max_h_value + max_w_value - 1

h_max_index = [i for i, x in enumerate(h_list) if x == max(h_list)]
w_max_index = [i for i, x in enumerate(w_list) if x == max(w_list)]



for i in h_max_index:
    for j in w_max_index:
        if (i,j) not in target_set:
            print(ans)
            exit()

print(ans-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((3*10**5, 3*10**5))
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:
# -*- coding: utf-8 -*-
import numpy as np

H, W, M = [int(x) for x in input().split()]
init = np.zeros((H,W))

for i in range(M):
  x, y = [int(x)-1 for x in input().split()]
  init[x,y] = 1

row = np.sum(init, axis=1)
col = np.sum(init, axis=0)

max_H = np.where(row == row.max())[0]
max_W = np.where(col == col.max())[0]

before_point = 0
max_point = row[max_H[0]] + col[max_W[0]]

if len(max_H) != 1 or len(max_W) != 1:
  for h in max_H:
    for w in max_W:
      point = row[h] + col[w]
      point -= init[h,w]
      if point > before_point:
        max_point = point
      before_point = point

print(int(max_point))

Submission status: Runtime Error

Solution code:
import numpy as np
import math

h,w,m = (int(x) for x in input().split())

sumH = [0 for i in range(h)]
sumW = [0 for i in range(w)]
bomH = [0 for i in range(m)]
bomW = [0 for i in range(m)]

for i in range(m):
    hi,wi = (int(x) for x in input().split())
    sumH[ hi-1 ] += 1
    sumW[ wi-1 ] += 1
    bomH[ i ] = hi
    bomW[ i ] = wi

maxH = max(H)
maxW = max(W)
gHs = []
gWs = []
for i in range( h ):
    if maxH == sumH[i]:
        gHs.append(i)
for i in range( w ):
    if maxW == sumW[i]:
        gWs.append(i)

great = maxH + maxW
flag = 1
break_loop = False
for i in range( len(gHs) ):
    x = gHs[i]
    for j in range( len(gWs) ):
        y = gWs[j]
        for k in range(m):
            if bomH[k] == x and bomW[k] == y:
                flag = 0
                break_loop = True
                break
        if break_loop:
            break
    if break_loop:
        break

print( great - flag )

Submission status: Runtime Error

Solution code:
h, w, m = map(int, input().split(' '))
grid = {}
h_b = set()

h_max = 0
w_max = 0
h_max_grid = 0
w_max_grid = 0

h_b = set()
w_b = set()

for i in range(m):
    hi, wi = map(int, input().split(' '))
    grid[(hi, wi)] = 1
    grid[(hi, 0)] = grid.get((hi, 0), 0) + 1
    grid[(0, wi)] = grid.get((0, wi), 0) + 1

    if h_max < grid[(hi,0)]:
        h_max = grid[(hi,0)]

    if w_max < grid[(0,wi)]:
        w_max = grid[(0,wi)]

    t = h_max + w_max
    grid[(0,0)] = t if grid.get((0,0), 0) < t else grid.get((0,0), 0)
    h_b.add(hi)
    w_b.add(wi)

flg = False
for hb in list(h_b):
    for wb in list(w_b):
        if (grid[(hb, 0)] + grid[(0, wb)]) != grid[(0,0)]:
            continue
        
        if grid[(hb, wb)] != 1:
            flg = True
            break
    else:
        continue
    break

if flg:
    print(grid[(0,0)])
else:
    print(grid[(0,0)]-1)

Submission status: Runtime Error

Solution code:
def main():
    H, W, M = map(int, input().split())
    # H, W, M = (3 * 10 ** 5, 3 * 10 ** 5, 3 * 10 ** 5)

    row = [0] * (H + 1)
    row_set = dict()
    column = [0] * (W + 1)
    column_set = dict()
    # import random

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

        # h, w = (random.randrange(3*10**5), random.randrange(3*10**5))
        row[h] += 1
        if h in row_set:
            row_set[h].add(w)
        else:
            row_set.update({h: set({w})})

        column[w] += 1
        if w in column_set:
            column_set[w].add(h)
        else:
            column_set.update({w: set({h})})

    flatten_max_rows = []
    maxR = -1
    for i, v in enumerate(row[1:]):
        if v > maxR:
            flatten_max_rows = []
            if v != 0:
                flatten_max_rows += list(row_set[i+1])
            maxR = v
        elif v == maxR:
            flatten_max_rows += list(row_set[i+1])
    flatten_max_rows_set = set(flatten_max_rows)

    maxC = max(column)
    for i, v in enumerate(column[1:]):
        if v == maxC:
            if i+1 not in flatten_max_rows_set:
                print(maxR + maxC)
                exit()

    print(maxR + maxC - 1)
main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(500005)
readline = sys.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]
    
    ans = hb + wb
    for mh in max_h:
        for mw in max_w:
            if HW[mh][mw] == 0:
                print(ans)
                exit()
    print(ans - 1)

if __name__ == "__main__":
    main()