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: Time Limit Exceeded

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


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


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


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


@njit(i8(i8[:,:],i8[:],i8[:],i8,i8,i8))
def exist(tar,indMR,indMC,NR,NC,check):
    for i in range(NR):
        for j in range(NC):
            if tar[indMR[i],indMC[j]] == 0:
                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]

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

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

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

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

    print(res)



main()

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
h_l = [[] for i in range(h + 1)]
w_l = [[] for i in range(w + 1)]
max_h = 0
max_h_l = []
max_w = 0
max_w_l = []
inputs = {}
for _ in range(m):
    h, w = map(int, input().split())
    inputs.setdefault(h, set([w]))
    inputs[h].add(w)

    if h_l[h] :
        h_l[h][1].add(w)
        h_l[h][0] += 1
    else:
        h_l[h] = [1, set([w])]
        
    if h_l[h][0] > max_h:
        max_h_l = [h]
        max_h = h_l[h][0]
    elif h_l[h][0] == max_h:
        max_h_l.append(h)
 
        
    if w_l[w]:
        w_l[w][1].add(h)
        w_l[w][0] += 1
    else:
        w_l[w] = [1, set([h])]
    if w_l[w][0] > max_w:
        max_w_l = [w]
        max_w = w_l[w][0]
    elif w_l[w][0] == max_w:
        max_w_l.append(w)

ans = max_h + max_w -1
flag = 0
for i in max_h_l:
    if flag == 1:
        break
    for j in max_w_l:
        if j not in inputs[i]:
            ans = max_h + max_w
            flag = 1
            break
        
print(ans)

Submission status: Time Limit Exceeded

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 Set

# 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: Set[Cod] = []
    columnSumList: List[int] = [0] * W
    rowSumList: List[int] = [0] * H
    for i in range(M):
        h, w = map(lambda x: int(x), input().split())
        item = (h, w)
        codList.append(item)
        columnSumList[w - 1] += 1
        rowSumList[h - 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: Time Limit Exceeded

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


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


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


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


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: Time Limit Exceeded

Solution code:
from collections import deque, Counter
import itertools

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

d_h = deque()
d_w = deque()
for h, w in HW:
    d_h.append(h)
    d_w.append(w)

# 出現頻度順にソート
c_h = Counter(d_h).most_common()
c_w = Counter(d_w).most_common()


# 登場頻度の一番高い回数
n_h = c_h[0][1]
n_w = c_w[0][1]

# 同率のものを数える
t_h = deque()
t_w = deque()

for k, v in c_h:
    if v == n_h:
        t_h.append(k)
    else:
        break

for k, v in c_w:
    if v == n_w:
        t_w.append(k)
    else:
        break

q = set(list(itertools.product(t_h, t_w)))

cnt = 0
ans = n_h + n_w
for h, w in q:
    if [h, w] not in HW:
        break
    cnt += 1
    if cnt == M:
        ans -= 1
        break
else:
    ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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 Set

# 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: Set[Cod] = []
    columnSumList: List[int] = [0] * W
    rowSumList: List[int] = [0] * H
    for i in range(M):
        args = input().split()
        item = (int(args[0]), int(args[1]))
        codList.append(item)
        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: Time Limit Exceeded

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 Set

# 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: Set[Cod] = []
    for i in range(M):
        args = input().split()
        codList.append((int(args[0]), int(args[1])))

    columnSumList: List[int] = [0] * W
    rowSumList: List[int] = [0] * H

    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: Time Limit Exceeded

Solution code:
from collections import Counter
H, W, M = map(int, input().split())
count_row = Counter()
count_col = Counter()
count_point = Counter()
for i in range(M):
    h, w = map(int, input().split())
    count_row[h] += 1
    count_col[w] += 1
    count_point[(h, w)] += 1

max_h, max_count_row = count_row.most_common()[0]
max_w, max_count_col = count_col.most_common()[0]
row_iter = [(h, c) for h, c in count_row.most_common() if c == max_count_row]
col_iter = [(w, c) for w, c in count_col.most_common() if c == max_count_col]
ans = 0
for h, row_c in row_iter:
    for w, col_c in col_iter:
        ans = max(ans, row_c+col_c-count_point[(h, w)])
print(ans)

Submission status: Time Limit Exceeded

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


def calcBomb(hw,tar,row,col,M):
    for i in range(M):
        h = hw[i,0]
        w = hw[i,1]
        tar[h-1,w-1] = 1
        row[h-1] += 1
        col[w-1] += 1
    return tar,row,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,row,col = calcBomb(hw,tar,row,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: Time Limit Exceeded

Solution code:
h,w,m=[int(x) for x in input().split()]

hnm=[0]*h
wnm=[0]*w
bomb=[]
for _ in range(m):
    hi,wi=[int(x)-1 for x in input().split()]
    hnm[hi]+=1
    wnm[wi]+=1
    bomb.append((hi,wi))

hmax=max(hnm)
wmax=max(wnm)
    
hidx=[]
for idx,val in enumerate(hnm):
    if hmax==val:
        hidx.append(idx)
    
widx=[]
for idx,val in enumerate(wnm):
    if wmax==val:
        widx.append(idx)
    
ofst=-1
for i in hidx:
    for j in widx:
        if not (i,j) in bomb:
            ofst=0
            break
                
print(hmax+wmax+ofst)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=[int(x) for x in input().split()]

hnm=[0]*h
wnm=[0]*w
bomb=[]
for _ in range(m):
    hi,wi=[int(x)-1 for x in input().split()]
    hnm[hi]+=1
    wnm[wi]+=1
    bomb.append((hi,wi))

sth=[[j,i] for i,j in enumerate(hnm)]
sth.sort(reverse=True)
stw=[[j,i] for i,j in enumerate(wnm)]
stw.sort(reverse=True)
    
hmax=-1
hidx=[]
for val,idx in sth:
    if hmax==-1:
        hmax=val
    elif hmax!=val:
        break
    hidx.append(idx)
    
wmax=-1
widx=[]
for val,idx in stw:
    if wmax==-1:
        wmax=val
    elif wmax!=val:
        break
    widx.append(idx)
    
ofst=-1
for i in hidx:
    for j in widx:
        if not (i,j) in bomb:
            ofst=0
            break
                
print(hmax+wmax+ofst)

Submission status: Time Limit Exceeded

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

# from typing import Optional
from typing import List
import array
import numpy as np

# from typing import Dict
from typing import Tuple

# from typing import Sequence


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

    codList = np.zeros((0, 2), dtype="int64")
    for i in range(M):
        args = input().split()
        codList = np.append(codList, np.array([[int(args[0]), int(args[1])]]), axis=0)

    columnSumList = np.zeros(W, dtype="int64")
    rowSumList = np.zeros(H, dtype="int64")

    maxColumnSum = 0
    maxColumIndexList: array[int] = []
    maxRowSum = 0
    maxRowIndexList: array[int] = []
    for item in codList:
        columnSum = columnSumList[item[1] - 1] + 1
        if columnSum > maxColumnSum:
            maxColumnSum = columnSum
            maxColumIndexList = [item[1]]
        elif columnSum == maxColumnSum:
            maxColumIndexList.append(item[1])

        rowSum = rowSumList[item[0] - 1] + 1
        if rowSum > maxRowSum:
            maxRowSum = rowSum
            maxRowIndexList = [item[0]]
        elif rowSum == maxRowSum:
            maxRowIndexList.append(item[0])

        columnSumList[item[1] - 1] = columnSum
        rowSumList[item[0] - 1] += 1

    result = maxColumnSum + maxRowSum
    for row in maxRowIndexList:
        for column in maxColumIndexList:
            data = np.array([row, column])
            isMatch = False
            for item in codList:
                if np.allclose(item, data):
                    isMatch = True

            if isMatch:
                continue
            else:
                print(result)
                return

    print(result - 1)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a

# Union Find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# 約数生成
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

inf = 10 ** 18
mod = 10 ** 9 + 7

h,w,m = lr()
dich = {}
dh = {}
dicw = {}
dw = {}
for i in range(m):
    y,x = lr()
    if y in dich:
        dich[y]+=1
        dh[y].append(x)
    else:
        dich[y]=1
        dh[y] = [x]
    if x in dicw:
        dicw[x]+=1
        dw[x].append(y)
    else:
        dicw[x]=1
        dw[x] = [y]
hlist = list(dich.items())
wlist = list(dicw.items())
hlist.sort(key=lambda x:x[1], reverse=True)
wlist.sort(key=lambda x:x[1], reverse=True)
flag = False
tmp = hlist[0][1]
ind = 0
while ind < len(hlist) and hlist[ind][1] == tmp:
    tmp2 = wlist[0][1]
    ind2 = 0
    while ind2 < len(wlist) and wlist[ind2][1] == tmp2:
        if not wlist[ind2][0] in dh[hlist[ind][0]]:
            flag = True
            break
        ind2+=1
    if flag:
        break
    ind+=1

if not flag:
    print(hlist[0][1]+wlist[0][1]-1)
else:
    print(hlist[0][1]+wlist[0][1])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
H, W, M = [int(i) for i in input().split()]
g = [0] * M
h = [0] * M
w = [0] * M
for i in range(M):
    h[i], w[i] = [int(j) for j in input().split()]
    g[i] = [h[i],w[i]]

ch = Counter(h)
cw = Counter(w)

chm = max(ch.values())
cwm = max(cw.values())
hi = [i for i in ch.keys() if ch[i] == chm]
wi = [i for i in cw.keys() if cw[i] == cwm]

for i in hi:
    for j in wi:
        if [i,j] not in g:
            print(ch[i] + cw[j])
            exit()
print(ch[hi[0]] + cw[wi[0]] - 1)

Submission status: Time Limit Exceeded

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

# スペース区切りの整数の入力
h, w, m = map(int, input().split())

h_list = [0]*h
w_list = [0]*w


h_w_list = [[0 for j in range(w)] for i in range(h)]

for zzz in range(m):
  # print("---")
  bom_h, bom_w = map(int, input().split())
  # print(bom_h, bom_w)
  h_list[bom_h-1] = h_list[bom_h-1] + 1
  w_list[bom_w-1] = w_list[bom_w-1] + 1

  h_w_list[bom_h-1][bom_w-1] = "target"

h_max_indexs = [i for i, v in enumerate(h_list) if v == max(h_list)]
# print(h_max_indexs)

w_max_indexs = [i for i, v in enumerate(w_list) if v == max(w_list)]
# print(w_max_indexs)


double_chance = False
# print("---")
for h_index in h_max_indexs:
  if double_chance:
    break
  for w_index in w_max_indexs:
    if double_chance:
      break
    chance = h_w_list[h_index][w_index] != "target"

    # print(h_index, w_index, chance)
    if chance:
      double_chance = True


if double_chance:
  result = h_list[h_index] + w_list[w_index]
else:
  result = h_list[h_index] + w_list[w_index] -1

print(result)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
h_l = [[] for i in range(h + 1)]
w_l = [[] for i in range(w + 1)]
max_h = 0
max_h_l = []
max_w = 0
max_w_l = []
inputs = []
for _ in range(m):
    h, w = map(int, input().split())
    inputs.append([h,w])
    if h_l[h] :
        h_l[h][1].append(w)
        h_l[h][0] += 1
    else:
        h_l[h] = [1, [w]]
        
    if h_l[h][0] > max_h:
        max_h_l = [h]
        max_h = h_l[h][0]
    elif h_l[h][0] == max_h:
        max_h_l.append(h)
 
        
    if w_l[w]:
        w_l[w][1].append(h)
        w_l[w][0] += 1
    else:
        w_l[w] = [1, [h]]
    if w_l[w][0] > max_w:
        max_w_l = [w]
        max_w = w_l[w][0]
    elif w_l[w][0] == max_w:
        max_w_l.append(w)
 
flag = 0
ans = 0
for i in max_h_l:
    if flag == 1:
        break
    for j in max_w_l:
        if flag == 1:
            break
        if j in h_l[i][1]:
            ans = max_h + max_w - 1
        else:
            ans = max_h + max_w
            flag = 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
H, W, M = map(int, input().split())
hsum = [0 for i in range(H)]
wsum = [0 for i in range(W)]
s = set()
for i in range(M):
    h, w = map(int, input().split())
    h, w = h-1, w-1
    s.add((h, w))
    hsum[h] += 1
    wsum[w] += 1

hmax = max(hsum)
wmax = max(wsum)

for i in range(H):
    for j in range(W):
        if hsum[i] == hmax and wsum[j] == wmax and (i, j) not in s:
            print(hmax + wmax)
            sys.exit()
print(hmax+wmax-1)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
h_l = [[] for i in range(h + 1)]
w_l = [[] for i in range(w + 1)]
max_h = 0
max_h_l = []
max_w = 0
max_w_l = []
inputs = []
for _ in range(m):
    h, w = map(int, input().split())
    inputs.append([h,w])
    if h_l[h] :
        h_l[h][1].append(w)
        h_l[h][0] += 1
    else:
        h_l[h] = [1, [w]]
        
    if h_l[h][0] > max_h:
        max_h_l = [h]
        max_h = h_l[h][0]
    elif h_l[h][0] == max_h:
        max_h_l.append(h)
 
        
    if w_l[w]:
        w_l[w][1].append(h)
        w_l[w][0] += 1
    else:
        w_l[w] = [1, [h]]
    if w_l[w][0] > max_w:
        max_w_l = [w]
        max_w = w_l[w][0]
    elif w_l[w][0] == max_w:
        max_w_l.append(w)
 
inputs.sort()
max_h_l.sort()

flag = 0
ans = 0
for i in max_h_l:
    if flag == 1:
        break
    for j in max_w_l:
        if flag == 1:
            break
        if [i,j] not in inputs:
            ans = max_h + max_w
            flag = 1
        else:
            ans = max_h + max_w - 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
 
mp = []
hp = [0] * h
wp = [0] * w
 
for i in range(m):
    l = list(map(int, input().split()))
    mp.append(l)
    hp[l[0]-1] += 1
    wp[l[1]-1] += 1
 
maxhp = max(hp)
maxwp = max(wp)
hpdex = [i for i, x in enumerate(hp) if x == maxhp]
wpdex = [i for i, x in enumerate(wp) if x == maxwp]
 
for i in range(len(hpdex)):
    for j in range(len(wpdex)):
        if [hpdex[i] + 1, wpdex[j] + 1] not in mp:
            print(maxhp + maxwp)
            exit()
 
print(maxhp + maxwp - 1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

for i in range(m):
  x,y = t[i]
  b[x-1][y-1] += 1

h_max = max(np.sum(b, axis=0))
w_max = max(np.sum(b, axis=1))

h_index = [n for n, v in enumerate(np.sum(b, axis=0)) if v == h_max]
w_index = [n for n, v in enumerate(np.sum(b, axis=1)) if v == w_max]

ans = h_max + w_max

for j in range(len(h_index)):
  for k in range(len(w_index)):
    if b[w_index[k]][h_index[j]] == 0:
      print(ans)
      exit()
print(ans-1)

Submission status: Time Limit Exceeded

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

h = [0] * (H + 1)
w = [0] * (W + 1)
boms = set()

for _ in range(M):
    x, y = map(int, input().split())
    h[x - 1] += 1
    w[y - 1] += 1
    boms.add((x, y))

h_max = max(h)
w_max = max(w)

h_idx = [x + 1 for x, y in enumerate(h) if y == h_max]
w_idx = [x + 1 for x, y in enumerate(w) if y == w_max]


def solve(h_max, w_max, boms):
    minus = []
    for x in h_idx:
        for y in w_idx:
            if (x, y) in boms:
                minus.append(-1)
            else:
                minus.append(0)
    return h_max + w_max + max(minus)


print(solve(h_max, w_max, boms))

Submission status: Time Limit Exceeded

Solution code:
def main():
    h, w, m = map(int, input().split())
    lsh = [0] * h
    lsw = [0] * w

    bom = []
    for _ in range(m):
        m1, m2 = map(int, input().split())
        lsh[m1 - 1] += 1
        lsw[m2 - 1] += 1
        bom.append((m1-1, m2-1))

    lshmax = max(lsh)
    lswmax = max(lsw)

    hin = [i for i in range(h) if lsh[i] == lshmax]
    win = [i for i in range(w) if lsw[i] == lswmax]

    for i in hin:
        for j in win:
            if (i, j) not in bom:
                print(lshmax + lswmax)
                exit(0)
    else:
        print(lshmax + lswmax -1)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

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

mp = set()
hp = [0] * h
wp = [0] * w

for i in range(m):
    a, b = map(int, input().split())
    mp.add((a,b))
    l = [a, b]
    hp[l[0]-1] += 1
    wp[l[1]-1] += 1

hpdex = [i for i, x in enumerate(hp) if x == max(hp)]
wpdex = [i for i, x in enumerate(wp) if x == max(wp)]

for i in hpdex:
    for j in wpdex:
        if (i+1, j+1) not in mp:
            print(max(hp) + max(wp))
            exit()

print(max(hp) + max(wp) - 1)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
h_l = [[] for i in range(h + 1)]
w_l = [[] for i in range(w + 1)]
max_h = 0
max_h_l = []
max_w = 0
max_w_l = []
inputs = []
for _ in range(m):
    h, w = map(int, input().split())
    inputs.append([h,w])
    if h_l[h] :
        h_l[h][1].append(w)
        h_l[h][0] += 1
    else:
        h_l[h] = [1, [w]]
        
    if h_l[h][0] > max_h:
        max_h_l = [h]
        max_h = h_l[h][0]
    if h_l[h][0] == max_h:
        max_h_l.append(h)
            
 
        
    if w_l[w]:
        w_l[w][1].append(h)
        w_l[w][0] += 1
    else:
        w_l[w] = [1, [h]]
    if w_l[w][0] > max_w:
        max_w_l = [w]
        max_w = w_l[w][0]
    if w_l[w][0] == max_w:
        max_w_l.append(w)
 
inputs.sort()
max_h_l.sort()

flag = 0
ans = 0
for i in max_h_l:
    if flag == 1:
        break
    for j in max_w_l:
        if flag == 1:
            break
        if [i,j] not in inputs:
            ans = max_h + max_w
            flag = 1
        else:
            ans = max_h + max_w - 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from numba import njit

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

h = np.zeros(H + 1, np.int64)
w = np.zeros(W + 1, np.int64)
boms = set()

for _ in range(M):
    x, y = map(int, input().split())
    h[x] += 1
    w[y] += 1
    boms.add((x, y))

h_max = h.max()
w_max = w.max()

h_idx = np.where(h == h_max)[0]
w_idx = np.where(w == w_max)[0]


@njit
def solve(h_max, w_max, boms):
    minus = []
    for x in h_idx:
        for y in w_idx:
            if (x, y) in boms:
                minus.append(-1)
            else:
                minus.append(0)
    return h_max + w_max + max(minus)


print(solve(h_max, w_max, boms))

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int,input().split())
g=[]
e1=[0]*h
e2=[0]*w
for c in range(m):
    a,b=map(int,input().split())
    g.append([a-1,b-1])
    e1[a-1]+=1
    e2[b-1]+=1
p1=max(e1)
p2=max(e2)
x=[c for c in range(h) if e1[c]==p1]
y=[c for c in range(w) if e2[c]==p2]
if len(x)*len(y)>m:
    print(p1+p2)
    exit()
for i in x:
    for j in y:
        if [i,j] not in g:
            print(p1+p2)
            exit()
print(p1+p2-1)

Submission status: Time Limit Exceeded

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

column_cnt = [0]*W
row_cnt = [0]*H

for i in range(M):
    row_cnt[bomb[i][0]-1] += 1
    column_cnt[bomb[i][1]-1] += 1

row_max_index = row_cnt.index(max(row_cnt))
column_max_index = column_cnt.index(max(column_cnt))

bomb_cnt_max = 0

for i in range(M):
    bomb_cnt = row_cnt[bomb[i][0]-1] + column_cnt[column_max_index]
    if [bomb[i][0], column_max_index+1] in bomb:
        bomb_cnt -= 1
    bomb_cnt_max = max(bomb_cnt_max, bomb_cnt)

    bomb_cnt = column_cnt[bomb[i][1]-1] + row_cnt[row_max_index]
    if [row_max_index+1, bomb[i][1]] in bomb:
        bomb_cnt -= 1
    bomb_cnt_max = max(bomb_cnt_max, bomb_cnt)

print(bomb_cnt_max)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

h = np.zeros(H + 1, np.int64)
w = np.zeros(W + 1, np.int64)
boms = set()

for _ in range(M):
    x, y = map(int, input().split())
    h[x] += 1
    w[y] += 1
    boms.add((x, y))

h_max = h.max()
w_max = w.max()

h_idx = np.where(h == h_max)[0]
w_idx = np.where(w == w_max)[0]

minus = []
for x in h_idx:
    for y in w_idx:
        if (x, y) in boms:
            minus.append(-1)
        else:
            minus.append(0)

print(h_max + w_max + max(minus))

Submission status: Time Limit Exceeded

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

# スペース区切りの整数の入力
h, w, m = map(int, input().split())

h_list = [0]*h
w_list = [0]*w

# h_w_list = [[0 for j in range(w)] for i in range(h)]
h_w_list = []

for zzz in range(m):
  # print("---")
  bom_h, bom_w = map(int, input().split())
  # print(bom_h, bom_w)
  h_list[bom_h-1] += 1
  w_list[bom_w-1] += 1

  # h_w_list[bom_h-1][bom_w-1] = 1
  h_w_list.append([bom_h, bom_w])

mh = max(h_list)
mw = max(w_list)
h_max_indexs = [i for i, v in enumerate(h_list) if v == mh]
w_max_indexs = [i for i, v in enumerate(w_list) if v == mw]

double_chance = False
# print("---")
for h_index in h_max_indexs:
  if double_chance:
    break
  for w_index in w_max_indexs:
    if double_chance:
      break
    # chance = h_w_list[h_index][w_index] != 1
    chance = h_w_list.count([h_index + 1, w_index + 1]) == 0
    # print(h_w_list)
    # print([h_index, w_index], chance)


    # print(h_index, w_index, chance)
    if chance:
      double_chance = True


if double_chance:
  result = mh + mw
else:
  result = mh + mw -1

print(result)

Submission status: Time Limit Exceeded

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

bom = []
for _ in range(m):
    m1, m2 = map(int, input().split())
    lsh[m1 - 1] += 1
    lsw[m2 - 1] += 1
    bom.append((m1-1, m2-1))

lshmax = max(lsh)
lswmax = max(lsw)

hin = [i for i in range(h) if lsh[i] == lshmax]
win = [i for i in range(w) if lsw[i] == lswmax]

jd = False

for i in hin:
    for j in win:
        if (i, j) not in bom:
            jd = True

if jd:
    print(lshmax + lswmax)
else:
    print(lshmax + lswmax -1)