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 collections
#import math

#f = open('./input_E.txt')
#
##f.readline()
##int(f.readline())
#H,W,M=map(int, f.readline().split())
#bakuha = []
#bakuha_H=[]
#bakuha_W=[]
#for i in range(M):
#    hh,ww=map(int, f.readline().split())
#    bakuha_H.append(hh)
#    bakuha_W.append(ww)
#    bakuha.append([hh,ww])
##    bakuha.append(list(map(int, f.readline().split())))
    

H,W,M=map(int, input().split())
bakuha = []
bakuha_H=[]
bakuha_W=[]
for i in range(M):
    hh,ww=map(int, input().split())
    bakuha_H.append(hh)
    bakuha_W.append(ww)
    bakuha.append([hh,ww])
#    bakuha.append(list(map(int, input().split())))



count_H = collections.Counter(bakuha_H)
H_most = count_H.most_common()
H_most_num = H_most[0][1]
H_only_most = [x for x in H_most if x[1]==H_most_num]
H_kouho = [x[0] for x in H_only_most]

count_W = collections.Counter(bakuha_W)
W_most = count_W.most_common()
W_most_num = W_most[0][1]
W_only_most = [x for x in W_most if x[1]==W_most_num]
W_kouho = [x[0] for x in W_only_most]

ans = H_most_num + W_most_num



#爆破対称のある座標にしか置けなかったらマイナス1
H_kouho_index = [i for i in range(M) if bakuha_H[i] in H_kouho]
W_kouho_index = [i for i in range(M) if bakuha_W[i] in W_kouho]
#
#ans_minus=True
#if len(H_kouho_index) <= len(W_kouho_index):
#    for h_index in H_kouho_index:
#        if h_index not in W_kouho_index:
#            ans_minus = False
#            break
#else:
#    for w_index in W_kouho_index:
#        if w_index not in H_kouho_index:
#            ans_minus = False
#            break
#if H_kouho_index == W_kouho_index:

#if ans_minus:
#    ans-=1

#bakuha_array = np.array(bakuha)
#for h_kouho in H_kouho:
#    for w_kouho in W_kouho:
#        bakuha_array = bakuha_array[bakuha_array[0]==h_kouho]
#
##    bakuha_array = bakuha_array[bakuha_array[1]==w_kouho]


ryouhou = 0
for bb in bakuha:
    x,y = bb
    if x in H_kouho and y in W_kouho:
        ryouhou+=1

h_by_w = len(H_kouho)*len(W_kouho)

if ryouhou == h_by_w:
    ans -= 1
    

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
import sys

input = sys.stdin.readline

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


targets = set()
xs = []
ys = []
for _ in range(m):
    y, x = map(int, input().split())
    # y -= 1
    # x -= 1
    ys.append(y)
    xs.append(x)
    targets.add((y, x))

x_stat = collections.Counter(xs)
y_stat = collections.Counter(ys)

xs = sorted(set(xs), key=lambda obj: x_stat[obj], reverse=True)
ys = sorted(set(ys), key=lambda obj: y_stat[obj], reverse=True)

mx_x = x_stat[xs[0]]
mx_y = y_stat[ys[0]]
while xs and x_stat[xs[-1]] < x_stat[xs[0]] - 1:
    xs.pop()
while ys and y_stat[ys[-1]] < y_stat[ys[0]] - 1:
    ys.pop()
best_res = 0
# print(xs, x_stat)
# print(ys, y_stat)
for x in xs:
    for y in ys:
        cur = x_stat[x] + y_stat[y] - ((y, x) in targets)
        best_res = max(cur, best_res)
print(best_res)

Submission status: Time Limit Exceeded

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(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 = 0
for h in bomb_h:
    for w in bomb_w:
        if (h, w) in targets:
            ans = max(h_max + w_max - 1, ans)
        else:
            ans = h_max + w_max

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
H,W,M=map(int,input().split())
S=[[0 for _ in range(W)] for _ in range(H)]
answers=np.array([[0 for a in range(W)] for b in range(H)])
for _ in range(M):
    h,w=map(int,input().split())
    answers[h-1,:]+=1
    answers[:,w-1]+=1
    answers[h-1,w-1]-=1
answer=[]
for a in answers:
    answer.append(max(a))
print(max(answer))

Submission status: Time Limit Exceeded

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

max_h=[0 for i in range(h+1)]
max_w=[0 for i in range(w+1)]
grid=[[0 for i in range(w)] for j in range(h)]
pro=[list(map(int,input().split())) for i in range(m)]
pro.sort()
for i in range(m):
    h=pro[i][0]
    w=pro[i][1]
    max_h[h]+=1
    max_w[w]+=1
    grid[h-1][w-1]=1
h_max=[i for i, x in enumerate(max_h) if x == max(max_h)]
w_max=[i for i, x in enumerate(max_w) if x == max(max_w)]

ans=0
for i in h_max:
    for j in w_max:
        cnt=0
        cnt +=list(zip(*grid))[j-1].count(1)
        cnt += grid[i-1].count(1)
        if grid[i-1][j-1]==1:
            cnt -= 1
        
        if cnt > ans:
            ans=cnt
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
nh = [0 for i in range(h)]
nw = [0 for j in range(w)]
bomb = []
for i in range(m):
    inp = list(map(int, input().split()))
    nh[inp[0] - 1] += 1
    nw[inp[1] - 1] += 1
    bomb.append([inp[0]-1, inp[1] - 1])
maxh = max(nh)
maxw = max(nw)
counth = 0
countw = 0
hlis = []
wlis = []
for j in range(h):
    if nh[j] == maxh:
        counth += 1
        hlis.append(j)
for i in range(w):
    if nw[i] == maxw:
        countw += 1
        wlis.append(i)
if counth * countw > m:
    print(maxh + maxw)
    exit()
else:
    for j in hlis:
        for i in wlis:
            flag = False
            for k in range(len(bomb)):
                if bomb[k] == [j, i]:
                    flag = True
            if not flag:
                print(maxh + maxw)
                exit()
    else:
        print(maxh + maxw - 1)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline


def main():
    H, W, M = map(int, input().split())
    row = [0] * (H)
    col = [0] * (W)
    bomb = set()
    for _ in range(M):
        h, w = map(int, input().split())
        bomb.add((h, w))
        row[h-1] += 1
        col[w-1] += 1
    row_max = max(row)
    row_index_list = [i for i, v in enumerate(row) if v == row_max]
    col_max = max(col)
    col_index_list = [i for i, v in enumerate(col) if v == col_max]
    res = 0
    for x in row_index_list:
        for y in col_index_list:
            ans = row[x] + col[y]
            if (x + 1, y + 1) in bomb:
                ans -= 1
            res = max(ans, res)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
H, W, M = map(int, input().split())
maze = [[0] * W for i in range(H)]
for i in range(M):
    x, y = map(int, input().split())
    maze[x-1][y-1] = 1

maze = np.array(maze)
X = np.sum(maze, axis=0)
ax = np.where(X == np.max(X))
Y = np.sum(maze, axis=1)
ay = np.where(Y == np.max(Y))
X = X.max()
Y = Y.max()
Q = 0
for i in range(len(ax[0])):
    for j in range(len(ay[0])):
        if maze[ay[0][j]][ax[0][i]] == 0:
            Q = X + Y
            break
    else:
        if Q < X + Y - 1:
            Q = X + Y - 1
print(Q)

Submission status: Time Limit Exceeded

Solution code:
import collections as c

h,w,m = map(int,input().split())
x = []
y = []
z = []
for i in range(m):
    s,t = map(int,input().split())
    x.append(s)
    y.append(t)
    z.append(tuple([s,t]))
a = c.Counter(x)
b = c.Counter(y)
a = a.most_common()
b = b.most_common()
c = []
d = []
e = a[0][1]
f = b[0][1]

for sa in a:
    if sa[1] == e:
        c.append(sa[0])
    else:
        break
for sb in b:
    if sb[1] == f:
        d.append(sb[0])
    else:
        break

fl = 0
ans = e+f-1
for s in c:
    for t in d:
        if tuple([s,t]) not in z:
            fl = 1
            ans += 1
            break
    if fl == 1:
        break
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def mis():
    return map(int, input().split())

def main():
    H, W, M = mis()
    B = set()
    BH = [0]*H
    BW = [0]*W
    for _ in range(M):
        h, w = mis()
        h -= 1
        w -= 1
        BH[h] += 1
        BW[w] += 1
        B.add((h, w))
    #
    maxbh = max(BH)
    maxbw = max(BW)
    cmaxbh = BH.count(maxbh)
    cmaxbw = BW.count(maxbw)
    if cmaxbh*cmaxbw > M:
        print(maxbh + maxbw)
        return
    for h in range(H):
        if BH[h]==maxbh:
            for w in range(W):
                if BW[w]==maxbw:
                    if (h, w) not in B:
                        print(maxbh + maxbw)
                        return
    print(maxbh + maxbw - 1)

main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
H,W,M=map(int,input().split())
h=[0]*H
w=[0]*W
xy=[]
for i in range(M):
    x,y=map(int,input().split())
    xy.append([x,y])
    h[x-1]+=1
    w[y-1]+=1
ans=max(h)+max(w)
hm=list(*np.where(h == np.max(h)))
wm=list(*np.where(w == np.max(w)))
#for j in range(M):
#    x = xy[j][0]
#    y = xy[j][1]
#    for n in hm:
#        if x==n+1:
#            for m in wm:
#                if y==m+1:
#                    break
for n in hm:
    for m in wm:
        if [n+1,m+1] in xy:
#        for j in range(M):
#            x = xy[j][0]
#            y = xy[j][1]
#            if x == n + 1 and y == m + 1:
            continue
        else:
            print(ans)
            exit()
print(ans-1)

Submission status: Time Limit Exceeded

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

column = [0] * H
row = [0] * W

for item in P:
  column[item[0] -1] += 1
  row[item[1] -1] += 1
  
co_max = sorted(column, reverse=True)[0]
ro_max = sorted(row, reverse=True)[0]

column_max = [i for i, x in enumerate(column) if x == co_max]
row_max = [i for i, x in enumerate(row) if x == ro_max]

flag = 0
for i in column_max:
  for j in row_max:
    if [i+1, j+1] not in P:
      flag = 1
      break
if flag == 1:
  print(max(column) + max(row))
elif flag == 0:
  print(max(column) + max(row) -1)

Submission status: Time Limit Exceeded

Solution code:
H,W,M=map(int,input().split())
BD=[[0 for _ in range(W)] for _ in range(H)]
DB=[[0 for _ in range(H)] for _ in range(W)]

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

maxH,maxW=0,0
C,R=0,0
for i in range(H):
  s=sum(BD[i])
  if s>maxH:
    maxH=s
    R=i
for j in range(W):
  DB[j][R]=0

for j in range(W):
  s=sum(DB[j])
  if s>maxW:
    maxW=s
    C=j
print(maxH+maxW)

Submission status: Time Limit Exceeded

Solution code:

from collections import defaultdict

H,W,M=map(int,input().split())
AB=[tuple(map(int,input().split()))for i in range(M)]
AB=[(a-1,b-1)for a,b in AB]
st=set(AB)

Wc=defaultdict(lambda :0)
Hc=defaultdict(lambda :0)
for a,b in AB:
    Hc[a]+=1
    Wc[b]+=1

ans=0
for i in Hc.keys():
    for j in Wc.keys():
        add = -1 if (i,j) in st else 0
        tmp= Hc[i] + Wc[j] + add
        ans=max(ans,tmp)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# Q5
import collections
import sys

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

H, W, M = LI()
h_w_list = []
for i in range(M):
    h_w_list.append(LI())

count_h = collections.Counter([h for h, w in h_w_list])
count_w = collections.Counter([w for h, w in h_w_list])

h_indices = []
h_bomb_max = 0
for i, h_index_count in enumerate(count_h.most_common()):
    if i == 0:
        h_bomb_max = h_index_count[1]

    if h_bomb_max == h_index_count[1]:
        h_indices.append(h_index_count[0])
    else:
        break

w_indices = []
w_bomb_max = 0
for i, w_index_count in enumerate(count_w.most_common()):
    if i == 0:
        w_bomb_max = w_index_count[1]

    if w_bomb_max == w_index_count[1]:
        w_indices.append(w_index_count[0])
    else:
        break

max_point = False
for h in h_indices:
    for w in w_indices:
        if [h, w] in h_w_list:
            continue
        else:
            print(h_bomb_max + w_bomb_max)
            sys.exit()
print(h_bomb_max + w_bomb_max - 1)

Submission status: Time Limit Exceeded

Solution code:
h,w,m = map(int,input().split())
hs = {}
ws = {}
S = set()

for i in range(m):
    a = list(map(int,input().split()))
    S.add(str(a[0]) + '-' + str(a[1]))
    if a[0] in hs:
        hs[a[0]] += 1
    else:
        hs[a[0]] = 1
    if a[1] in ws:
        ws[a[1]] += 1
    else:
        ws[a[1]] = 1
    
#print(hs,ws)
max_kvh = [kv for kv in hs.items() if kv[1] == max(hs.values())]
max_kvw = [kv for kv in ws.items() if kv[1] == max(ws.values())]
#print(max_kvh,max_kvw)

ans = 0
for i in max_kvh:
    for j in max_kvw:
        if str(i[0]) + '-' + str(j[0]) in S:
            ans = i[1]+j[1]-1
        else:
            ans = i[1]+j[1]
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

H, W, M = map(int, input().split())
bom = [[0]*W for _ in range(H)]
row = [0] * H
col = [0] * W

for _ in range(M):
    h, w = map(int, input().split())
    bom[h-1][w-1] += 1
    row[h-1] += 1
    col[w-1] += 1

ans = 0

for i in range(H):
    cnt = row[i]
    r = bom[i]

    tmp = np.array(col) - np.array(r)
    maxi = np.max(tmp)

    ans = max(ans, (cnt + maxi))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def my_index_multi(l, x):
    return [k for k, _x in enumerate(l) if _x == x]

h,w,m = map(int,input().split())
l_h = [0]*(h+1)
l_w = [0]*(w+1)
a = min(h*w,3*10**5)
l_hw = [[0 for _ in range(a)] for __ in range(h+1)]

for x in range(m):
    h,w = map(int,input().split())
    l_h[h] += 1
    l_w[w] += 1
    l_hw[h][x] += w

m_h = max(l_h)
m_w = max(l_w)

ans = m_h + m_w

l_h_2 = my_index_multi(l_h,m_h)
l_w_2 = my_index_multi(l_w,m_w)

for i in l_h_2:
    flag = False
    for j in l_w_2:
        if j not in l_hw[i]:
            flag = True
            break
    if flag:
        break
else:
    ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
H,W,M=map(int,input().split())
S=[[0 for _ in range(W)] for _ in range(H)]
answers=np.array([[0 for a in range(W)] for b in range(H)])
for _ in range(M):
    h,w=map(int,input().split())
    S[h-1][w-1]+=1
    answers[h-1,:]+=1
    answers[:,w-1]+=1
    answers[h-1,w-1]-=1
answer=[]
for a in answers:
    answer.append(max(a))
print(max(answer))

Submission status: Time Limit Exceeded

Solution code:
import itertools
import numpy as np

# 別変数の整数複数個
H, W, M = [int(x) for x in input().strip().split(" ")]


B = [(v[0] -1 , v[1] - 1) for v in [
    [int(x) for x in input().strip().split(" ")]
    for _ in range(M)
]]

h_sums = [sum([1 for b in B if b[0] == h]) for h in range(H)]
w_sums = [sum([1 for b in B if b[1] == w]) for w in range(W)]
h_sum_max = max(h_sums)
w_sum_max = max(w_sums)

for h, hv in enumerate(h_sums):
    if hv != h_sum_max:
        continue
    for w, wv in enumerate(w_sums):
        if wv != w_sum_max:
            continue
        if (h, w) not in B:
            print(h_sum_max + w_sum_max)
            import sys; sys.exit(0)
print(h_sum_max + w_sum_max - 1)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
hw = [["." for j in range(w)] for i in range(h)]
l = [list(map(int, input().split())) for i in range(m)]
ht = [0] * h
wt = [0] * w
for i in range(m):
    hw[l[i][0]-1][l[i][1]-1] = "#"
    ht[l[i][0]-1] += 1
    wt[l[i][1]-1] += 1
mht = max(ht)
mwt = max(wt)
for i in [p for p, q in enumerate(ht) if q == mht]:
    for j in [y for y, x in enumerate(wt) if x == mwt]:
        if hw[i][j] == ".":
            print(mht+mwt)
            exit()
print(mht+mwt-1)

Submission status: Time Limit Exceeded

Solution code:
h,w,m = map(int,input().split())
hw = [list(map(int,input().split())) for i in range(m)]
hb = [0 for i in range(h)]
wb = [0 for j in range(w)]
M = [[0 for i in range(w)] for j in range(h)]

for i in range(m):
    hb[hw[i][0]-1] += 1
    wb[hw[i][1]-1] += 1
    M[hw[i][0]-1][hw[i][1]-1] = -1

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans,hb[i]+wb[j]+M[i][j])
print(ans)

Submission status: Time Limit Exceeded

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

hw_list=[[0 for i in range(w)] for j in range(h)]
h_list=[0]*h
w_list=[0]*w
for i in range(m) :
  a,b=map(int,input().split())
  h_list[a-1]+=1
  w_list[b-1]+=1
  hw_list[a-1][b-1]=1

maxcount=0
h_max=max(h_list)
w_max=max(w_list)
h_max_list=[]
w_max_list=[]
for i in range(h) :
  if h_list[i]==h_max :
    h_max_list.extend([i])

for i in range(w) :
  if w_list[i]==w_max :
    w_max_list.extend([i])
flag=False
for i in h_max_list :
  for j in w_max_list :
    if hw_list[i][j]!=1 :
      flag=True
      break

if flag :
  print(str(h_max+w_max))
else :
  print(str(h_max+w_max-1))

Submission status: Time Limit Exceeded

Solution code:
# Q5
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
from functools import lru_cache

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

H, W, M = LI()
h_w_list = []
for i in range(M):
    h_w_list.append(LI())

count_h = collections.Counter([h for h, w in h_w_list])
count_w = collections.Counter([w for h, w in h_w_list])

h_indices = []
h_bomb_max = 0
for i, h_index_count in enumerate(count_h.most_common()):
    if i == 0:
        h_bomb_max = h_index_count[1]

    if h_bomb_max == h_index_count[1]:
        h_indices.append(h_index_count[0])
    else:
        break

w_indices = []
w_bomb_max = 0
for i, w_index_count in enumerate(count_w.most_common()):
    if i == 0:
        w_bomb_max = w_index_count[1]

    if w_bomb_max == w_index_count[1]:
        w_indices.append(w_index_count[0])
    else:
        break

max_point = False
for h in h_indices:
    for w in w_indices:
        if [h, w] in h_w_list:
            continue
        else:
            print(h_bomb_max + w_bomb_max)
            sys.exit()
print(h_bomb_max + w_bomb_max - 1)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
H, W, M = map(int, input().split())
tate = defaultdict(int)
yoko = defaultdict(int)
T = {}
for i in range(M):
    h, w = map(int, input().split())
    tate[h-1] += 1
    yoko[w-1]+= 1
    T[(h-1, w-1)] = 1

tate = sorted(tate.items(), key=lambda x:x[1], reverse=True)
yoko = sorted(yoko.items(), key=lambda x:x[1], reverse=True)
tate_m = tate[0][1]
yoko_m = yoko[0][1]
ans = 0
for i in range(len(tate)):
    if tate[i][1] == tate_m:
        for j in range(len(yoko)):
            if yoko[j][1] == yoko_m:
                if (tate[i][0], yoko[j][0]) in T:
                    ans = max(ans, tate_m + yoko_m - 1)
                else:
                    ans = max(ans, tate_m + yoko_m)
            elif yoko[j][1] == yoko_m - 1:
                if (tate[i][0], yoko[j][0]) in T:
                    ans = max(ans, tate_m + yoko_m - 2)
                else:
                    ans = max(ans, tate_m + yoko_m - 1)
            else:
                break
    elif tate[i][1] == tate_m - 1:
        for j in range(len(yoko)):
            if yoko[j][1] == yoko_m:
                if (tate[i][0], yoko[j][0]) in T:
                    ans = max(ans, tate_m + yoko_m - 2)
                else:
                    ans = max(ans, tate_m + yoko_m - 1)
            else:
                break
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys

H,W,M,*hw = map(int,open(0).read().split())

h = hw[::2]
w = hw[1::2]

hw_set = set((hi,wi) for hi,wi in zip(h,w))


raw_count = Counter(h)
col_count = Counter(w)

raw_max = raw_count.most_common()[0][1]
col_max = col_count.most_common()[0][1]

ans = 0
for hi, rcount in raw_count.most_common():
    if rcount<raw_max:break
    for wi, ccount in col_count.most_common():
        if ccount<col_max: break
        
        if (hi,wi) in hw_set:
            ans = raw_max + col_max - 1
        else:
            print(raw_max + col_max)
            sys.exit()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
H,W,M=map(int, input().split())
hl=np.array([0 for i in range(H+1)])
wl=np.array([0 for i in range(W+1)])
ms=[]
for i in range(M):
    h,w=map(int, input().split())
    hl[h]+=1
    wl[w]+=1
    ms.append([h,w])
hmax= [i for i, x in enumerate(hl) if x == max(hl)]
wmax=[i for i, x in enumerate(wl) if x == max(wl)]
a=max(hl)
b=max(wl)
bl=False
for x in hmax:
    for y in wmax:
        if [x,y] not in ms:
            bl=True
            break
    if bl:
        break
if bl:
    print(a+b)
else:
    print(a+b-1)

Submission status: Time Limit Exceeded

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

def main():
    input = sys.stdin.readline
    h, w, m = map(int, input().split())
    cnt_h = [0] * h
    cnt_w = [0] * w
    bomb = [[False] * w for _ in range(h)]
    for _ in range(m):
        y, x = map(lambda x:int(x) - 1, input().split())
        cnt_h[y] += 1
        cnt_w[x] += 1
        bomb[y][x] = True
        
    kouho_h = []
    max_h = max(cnt_h)
    max_w = max(cnt_w)
    for i in range(h):
        if cnt_h[i] == max_h:
            kouho_h.append(i)
    kouho_w = []
    for i in range(w):
        if cnt_w[i] == max_w:
            kouho_w.append(i)
    
    flag = False
    for i in kouho_h:
        for j in kouho_w:
            if bomb[i][j] == False:
                flag = True
                break
    if flag:
        print(max_h + max_w)
    else:
        print(max_h + max_w - 1)



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections

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

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

counter = collections.Counter(h)
h_max = max(counter.values())
mode_h = [key for key , value in counter.items() if value == h_max]
counter = collections.Counter(w)
w_max = max(counter.values())
mode_w = [key for key , value in counter.items() if value == w_max]

ans = 0
for i in mode_h:
    for j in mode_w:
        if ans < h.count(i) + w.count(j):
            ans = h.count(i) + w.count(j)
            for k in range(M):
                if h[k] == i and w[k] == j:
                    ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W, M = map(int, input().split())
B = []
c={}
d={}

for i in range(M):
    b = list(map(int, input().split()))
    if b[0] not in c.keys():
        c[b[0]] =1
    else:
        c[b[0]] +=1
        
    if b[1] not in d.keys():
        d[b[1]] =1
    else:
        d[b[1]] +=1
    B.append(b)
#print(B)
#print(c,d,B)
c_max =max(c.values())
cc = [kv[0] for kv in c.items() if kv[1] == max(c.values())]
d_max= max(d.values())
dd = [kv[0] for kv in d.items() if kv[1] == max(d.values())]
#print(c_max,d_max)
#print(cc,c_max,dd,d_max)

for i in range (len(cc)):
    for n in range(len(dd)):
        #print(i, n, cc[i], dd[n])
        if [cc[i], dd[n]] not in B:
            print(c_max+d_max)
            break
    
        if i == len(cc)-1 and n == len(dd)-1:
            print((c_max+d_max)-1)

Submission status: Time Limit Exceeded

Solution code:
import itertools
H,W,M = map(int,input().split())
lsx = [0] + [0]*H
lsy = [0] + [0]*W
lsxy = []
for i in range(M):
    x,y = map(int,input().split())
    lsx[x] += 1
    lsy[y] += 1
    lsxy.append((x,y))
maxx = max(lsx)
maxy = max(lsy)
lsmaxx = [i for i, x in enumerate(lsx) if x == maxx]
lsmaxy = [i for i, x in enumerate(lsy) if x == maxy]
lsp = list(itertools.product(lsmaxx,lsmaxy))
factor = 1
for i in lsp:
    if not i in lsxy:
        factor = 0
        break
print(maxx+maxy-factor)

Submission status: Time Limit Exceeded

Solution code:
def main():
    h, w, m = map(int, input().split())
    height = [0 for _ in range(h)]
    width = [0 for _ in range(w)]
    target = set()
    for _ in range(m):
        x, y = map(lambda x: int(x) - 1, input().split())
        target.add((x, y))
        height[x] += 1
        width[y] += 1
    ans = 0
    for i in range(h):
        for j in range(w):
            now_ans = height[i] + width[j]
            if (i, j) in target:
               now_ans -= 1
            ans = max(ans, now_ans)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections
import sys
H, W, M = [int(x) for x in input().split()]
h = list()
w = list()
graph = [[0 for _ in range(W)] for _ in range(H)]
for _ in range(M):
  a, b = [int(x) for x in input().split()]
  graph[a-1][b-1] = 1
  h.append(a)
  w.append(b)
h_count = collections.Counter(h)
w_count = collections.Counter(w)
h_max = h_count.most_common()
w_max = w_count.most_common()
h_max_value = h_max[0][1]
w_max_value = w_max[0][1]

h_num = len([i for i in range(len(h_max)) if(h_max_value==h_max[i][1])])
w_num = len([i for i in range(len(w_max)) if(w_max_value==w_max[i][1])])

for i in range(h_num):
  for j in range(w_num):
    if(graph[h_max[i][0]-1][w_max[j][0]-1] == 0):
      ans = (h_max[i][1] + w_max[j][1])
      print(ans)
      sys.exit()

ans = h_max[0][1] + w_max[0][1] -1
print(ans)

Submission status: Time Limit Exceeded

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h, w, m = LI()
bomb = defaultdict(lambda : 0)
cnt_h = [0] * h
cnt_w = [0] * w
H_list = []
W_list = []
for _ in range(m):
    H, W = LI()
    H_list.append(H-1)
    W_list.append(W-1)
    bomb[(H-1, W-1)] = 1
    cnt_h[H-1] += 1
    cnt_w[W-1] += 1
 
ans = 0
for i in H_list:
    for j in W_list:
        ans = max(ans, cnt_h[i] + cnt_w[j] - bomb[(i, j)])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W, M = map(int, input().split())
Hdst = [0]*H
Wdst = [0]*W
DST = [[0] * 2 for i in range(M)]
for i in range(M):
    hi, wi = map(int, input().split())
    Hdst[hi-1] += 1
    Wdst[wi-1] += 1
    DST[i] = [hi-1, wi-1]

Hbomb = [i for i, x in enumerate(Hdst) if x == max(Hdst)]
Wbomb = [i for i, x in enumerate(Wdst) if x == max(Wdst)]

ans = 0
for i in Hbomb:
    for j in Wbomb:
        tmp = Hdst[i] + Wdst[j]
        if [i, j] in DST:
            tmp -= 1
        if tmp > ans:
            ans = tmp

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int,input().split())
xnum=[[0,i] for i in range(h)]
ynum=[[0,i] for i in range(w)]
hw=[]
xmax=[]
ymax=[]
for i in range(m):
  s,t=map(int,input().split())
  xnum[s-1][0]+=1
  ynum[t-1][0]+=1
  hw.append([s-1,t-1])
xnum.sort()
ynum.sort()
xmax.append(xnum[-1][1])
ymax.append(ynum[-1][1])
for i in range(2,len(xnum)+1):
  if xnum[-1*i][0]==xnum[-1][0]:
    xmax.append(xnum[-1*i][1])
  else:
    break
for i in range(2,len(ynum)+1):
  if ynum[-1*i][0]==ynum[-1][0]:
    ymax.append(ynum[-1*i][1])
  else:
    break
flag=True
for i in xmax:
  for j in ymax:
    if [i,j] in hw:
      continue
    else:
      flag=False

if flag:
  ans=xnum[-1][0]+ynum[-1][0]-1
else:
  ans=xnum[-1][0]+ynum[-1][0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#from functools import lru_cache
import sys
from itertools import product,combinations
from functools import reduce,lru_cache
import heapq

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def list_t(l): return vfunc(lambda *args:args)(*l)

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

s=rvfunc(int)(reader())
h,w,m=s[0]
hl,wl = [0]*h,[0]*w
hw=s[1:]
hh,ww = list_t(hw)
for i in range(len(hw)):
    hl[hh[i]-1]+=1
    wl[ww[i]-1]+=1
hlmax,wlmax = vfunc(max)([hl,wl])
hlidx = tuple(filter(lambda a:hl[a]==hlmax,range(len(hl))))
wlidx = tuple(filter(lambda a:wl[a]==wlmax,range(len(wl))))
minus = 1
fltd = tuple(filter(lambda a:a[0]-1 in hlidx and a[1]-1 in wlidx,hw))
for i in hlidx:
    for j in wlidx:
#        print([i+1,j+1],fltd)
        if not [i+1,j+1] in fltd:
            minus=0
            break
print(max(hl)+max(wl)-minus)

Submission status: Time Limit Exceeded

Solution code:
#左の最頻と右の最頻合計すればいいんじゃないの？
#でも最頻がお互い3万種類までありえる
#その場合は最頻をminから順番に組み合わせていって、最頻＋最頻が該当した瞬間breakでよい
#これなら処理回数はmax3万？だし
#最頻+最頻-1しかない場合は、左右ぞれぞれ2位×1位を同様に試すいやこれいらんわ
import collections
a = input().split()
M = int(a[2])
hidari = []
migi = []
c = []
 
for i in range(M):
  b = input().split()
  hidari.append(b[0])
  migi.append(b[1])
  c.append(b)
 
countleft = collections.Counter(hidari)
countright = collections.Counter(migi)
leftmax = countleft.most_common()[0][1]
rightmax = countright.most_common()[0][1]
tempmax = int(leftmax + rightmax)
 
leftkeys = [k for k, v in countleft.items() if v == leftmax]
rightkeys = [k for k, v in countright.items() if v == rightmax]
leftkeyN = int(len(leftkeys))
rightkeyN = int(len(rightkeys))
flag = int(0)
cnt = int(0)
#これで左右の最頻回数はOK
#左右の最頻奴も探した
#ここから最頻同志になってるやつを探す
 
for i in range(leftkeyN):
    if flag != 0:
        break
    else:
        for P in range(rightkeyN):
            for T in range(M):
                if c[T] == [leftkeys[i],rightkeys[P]]:
                    cnt = cnt + 1
            if cnt == 0:
                flag = flag + 1
                break
            else:
                cnt = 0
 
if flag == 0:
    print(int(tempmax - 1))
else:
    print(int(tempmax))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
h, w, m = map(int, input().split())
hl=[0]*h
wl=[0]*w
mh=0
mw=0
mhi=deque([])
dic = {}
mwi=deque([])
#ans =[[0]*w for i in range(h)]
s = [list(map(int, input().split())) for i in range(m)]
 
for i in range(m):
  a, b = s[i]
  a-=1
  b-=1
  if not a in dic:
    dic[a] = deque([b])
  else:
    dic[a].append(b)
 
  hl[a]+=1
  if hl[a]>mh:
    mh=hl[a]
    mhi = [a]
  elif hl[a]==mh:
    mhi.append(a)
  
  wl[b]+=1
  if wl[b]>mw:
    mw=wl[b]
    mwi=[b]
  elif wl[b]==mw:
    mwi.append(b)
 
  #ans[a][b]+=1 
for i in mhi:
  for j in mwi:
    if not j in dic[i]:
      print(mw+mh)
      quit()

print(mh+mw-1)