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 sys
input = sys.stdin.readline
h,w,m=map(int, input().split())
from collections import defaultdict
hd=defaultdict(int)
wd=defaultdict(int)
hd2=defaultdict(list)
wd2=defaultdict(list)
for i in range(m):
    h0,w0=map(int, input().split())
    hd[h0]+=1
    hd2[h0].append(w0)
    wd[w0]+=1
    wd2[w0].append(h0)

max_h_list = [kv[0] for kv in hd.items() if kv[1] == max(hd.values())]
max_w_list = [kv[0] for kv in wd.items() if kv[1] == max(wd.values())]

ans=float('inf')

if len(max_h_list)>=len(max_w_list):
    for x in max_h_list:
        hlist=hd2[x]
        for y in max_w_list:
            if y in hlist:
                ans=min(ans,1)
            else:
                ans=min(ans,0)
else:
    for x in max_w_list:
        wlist=wd2[x]
        for y in max_h_list:
            if y in wlist:
                ans=min(ans,1)
            else:
                ans=min(ans,0)
print(hd[max_h_list[0]]+wd[max_w_list[0]]-ans)

Submission status: Time Limit Exceeded

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

h_obj=[0]*(h+1)
w_obj=[0]*(w+1)
hw_grid=[]
for _ in range(m):
  li=list(map(int,input().split()))
  hw_grid.append(li)
  h_obj[li[0]]+=1
  w_obj[li[1]]+=1
  
def index_Multi(List,liter):
    #Listはリスト本体・literは検索したい文字
    index_L = []
    for val in range(0,len(List)):
        if liter == List[val]:
            index_L.append(val)
    return index_L
  
max_h=max(h_obj)
max_w=max(w_obj)

max_h_li=index_Multi(h_obj,max_h)
max_w_li=index_Multi(w_obj,max_w)

#print("max_w_li",max_w_li)
#print("max_h_li",max_h_li)
#print(hw_grid)

set_list=[]
for H in max_h_li:
  for W in max_w_li:
    set_list.append([H,W])

set_list=[str(pair) for pair in set_list]
hw_grid=[str(pair) for pair in hw_grid]

#print("set_list",set_list)
#print("hw_grid",hw_grid)

#print("set(set_list)-set(hw_grid)",list(set(set_list)-set(hw_grid)))

if len(set(set_list)-set(hw_grid)) !=0:
  print(max(h_obj)+max(w_obj))
else:
  print(max(h_obj)+max(w_obj)-1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import Counter
H, W, m = map(int, input().split())
hw = np.array([list(map(int, input().split())) for i in range(m)])

h = hw[:,0]
w = hw[:,1]
counter_h = Counter(h)
bom_h = max(counter_h.values())
max_h = [i for i in counter_h.keys() if counter_h[i] == bom_h]

counter_w = Counter(w)
bom_w = max(counter_w.values())
max_w = [i for i in counter_w.keys() if counter_w[i] == bom_w]

flag = True
for i in max_h:
    for j in max_w:
        if not (np.array([i,j])==hw).all(axis=1).any():
            flag = False
            break
    else:
        continue
    break

if flag:
    print(bom_h + bom_w -1)
else:
    print(bom_h + bom_w)

Submission status: Time Limit Exceeded

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
h,w,m=map(int,input().split())
l={}
u={}
hw=[list(map(int,input().split())) for _ in range(m)]
sl=set()
for i in range(m):
    hr,wr=hw[i]
    if hr-1 in u:
        u[hr-1]+=1
    else:
        u[hr-1]=1
    if wr-1 in l:
        l[wr-1]+=1
    else:
        l[wr-1]=1
    
    sl.add((hr-1)*w+(wr-1))
resl=0
resu=0
cl=[]
cu=[]
for i in l:
    if resl<l[i]:
        resl=l[i]
        cl=[i]
    elif resl==l[i]:
        cl.append(i)
for i in u:
    if resu<u[i]:
        resu=u[i]
        cu=[i]
    elif resu==u[i]:
        cu.append(i)
ans=0
for i in cu:
    for j in cl:
        if (i*w+j) in sl:
            res=l[j]+u[i]-1
        else:
            res=l[j]+u[i]
        ans=max(res,ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
h,w,m=map(int,input().split())
l=[]
 
lh,lw=[0]*h,[0]*w
for i in range(m):
    l1=list(map(int,input().split()))
    lh[l1[0]-1]+=1
    lw[l1[1]-1]+=1
    l.append(l1)

lhm=max(lh)
lhml=[i for i, x in enumerate(lh) if x == lhm]
lwm=max(lw)
lwml=[i for i, x in enumerate(lw) if x == lwm]

for i in range(len(lhml)):
    for j in range(len(lwml)):
        k=[lhml[i]+1,lwml[j]+1]
        l.append(k)

l_arr = np.unique(l, axis=0)
if len(l_arr)==m:print(lhm+lwm-1)
else:print(lhm+lwm)

Submission status: Time Limit Exceeded

Solution code:
h,w,m = list(map(int,input().split()))
t = [list(map(int,input().split())) for _ in range(m)]
from collections import Counter
 
x,y = zip(*t)
xcnt = Counter(x)
ycnt = Counter(y)
 
xcmax = max( xcnt.values() )
ycmax = max( ycnt.values() )
xlist = [k for k,v in xcnt.items() if v == xcmax]
ylist = [k for k,v in ycnt.items() if v == ycmax]
xylist = { i*(10**6)+j for i in xlist for j in ylist }
tlist = { i[0]*(10**6) + i[1] for i in t }
if len( xylist - tlist ) == 0:
  print( xcmax + ycmax -1 )
else:
  print( xcmax + ycmax )

Submission status: Time Limit Exceeded

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

arr = []
for _ in range(M):
    h, w = map(int, input().split())
    arr.append((h, w))

ans = 0

for i in range(1, H + 1):
    for j in range(1, W + 1):
        ans = max(ans, sum([x == i or y == j for (x, y) in arr]))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
H , W , M = map(int,input().split())
G1 = [0]*H
G2 = [0]*W
h = [0]*M
w = [0]*M
for i in range(M):
  h[i] , w[i] = map(int,input().split())
  temp = h[i] - 1
  G1[temp] += 1
  temp = w[i] - 1
  G2[temp] += 1
G3 = [[h[i] - 1 , w[i] - 1] for i in range(M)]
M1 = max(G1)
M2 = max(G2)
M1L = deque()
M2L = deque()
for i in range(H):
  if G1[i] == M1:
    M1L.appendleft(i)
for j in range(W):
  if G2[j] == M2:
    M2L.appendleft(j)
c = 0
f = len(M1L) * len(M2L)
for X in G3:
  if X[0] in M1L and X[1] in M2L:
    c += 1
#for i in M1L:
#  for j in M2L:
#    if [i,j] not in G3:
#      c = 1
#break
#  if c ==1:
#    break
if c == f:
  print(M1 + M2 - 1)
else:
  print(M1 + M2)

Submission status: Time Limit Exceeded

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

Solution code:
H,W,M=map(int, input().split())
nrow=[0]*H
ncol=[0]*W
xycoord=[]

for _ in range(M):
	h,w=map(int, input().split())
	nrow[h-1]+=1
	ncol[w-1]+=1
	xycoord+=[[w-1,h-1]]

#ncol=collections.Counter(xcoord)
#nrow=collections.Counter(ycoord)

row_cand=[i for i, x in enumerate(nrow) if x == max(nrow)]
col_cand=[i for i, x in enumerate(ncol) if x == max(ncol)]


flag=0
for i in xycoord:
	if (i[0] in col_cand) and (i[1] in row_cand):
		flag+=1

if flag<(len(col_cand)*len(row_cand)):
	print(max(nrow)+max(ncol))
else:
	print(max(nrow)+max(ncol)-1)



# 2darray [[0] * 4 for i in range(3)]
# import itertools

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
H, W, M = map(int, input().split())
rows = {i: 0 for i in range(H)}
cols = {i: 0 for i in range(W)}
d = defaultdict(int)
for _ in range(M):
    h, w = map(int, input().split())
    h -= 1
    w -= 1
    rows[h] += 1
    cols[w] += 1
    d[(h,w)] += 1
max_rows = [kv for kv in rows.items() if kv[1] == max(rows.values())]
max_cols = [kv for kv in cols.items() if kv[1] == max(cols.values())]
non_dup = False
ans = max_rows[0][1] + max_cols[0][1]
for kh, vh in max_rows:
    for kw, vw in max_cols:
        if d[(kh, kw)] == 0:
            non_dup = True
            break
if not non_dup:
    ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys 
import itertools
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
H, W, M= map(int,readline().split())
h_cnt = [0]*(H+1)
w_cnt = [0]*(W+1)
data = [[False]*(W+1) for i in range(H+1)]

for i in range(M):
    h,w = map(int,readline().split())
    data[h][w] = True
    h_cnt[h] += 1 
    w_cnt[w] += 1
    
max_h = max(h_cnt)
max_w = max(w_cnt)

max_h_ind = []
max_w_ind = []
max_h_cnt = 0
max_w_cnt = 0

for i in range(H+1):
    if max_h == h_cnt[i]:
        max_h_cnt += 1
        max_h_ind.append(i)

for i in range(W+1):
    if max_w == w_cnt[i]:
        max_w_cnt += 1
        max_w_ind.append(i)
        
t = list(itertools.product(max_h_ind, max_w_ind))

if max_w_cnt * max_h_cnt > M:
    print(max_h+max_w)
else:
    for h,w in t:
        if data[h][w] == False:
            print(max_h+max_w)
            sys.exit()
    print(max_h+max_w-1)

Submission status: Time Limit Exceeded

Solution code:
h,w,m = map(int,input().split())
hw = [list(map(int,input().split())) for _ in range(m)]

cnt_h = [0] * h
cnt_w = [0] * w

for h1,w1 in hw:
    cnt_h[h1-1] += 1
    cnt_w[w1-1] += 1

lh = []
lw = []

for i,e in enumerate(cnt_h):
    lh.append((e,i))

for i,e in enumerate(cnt_w):
    lw.append((e,i))

lh.sort(reverse = True)
lw.sort(reverse = True)

mh = lh[0][0]
mw = lw[0][0]

idx_h = 0
idx_w = 0

cnt = 0
for i,j in lh:
    if i == mh:
        cnt += 1
    else:
        break

idx_h = cnt

cnt = 0
for i,j in lw:
    if i == mw:
        cnt += 1
    else:
        break

idx_w = cnt

ans = mh + mw

v = [[False] * w for _ in range(h)]
for h1,w1 in hw:
    v[h1-1][w1-1] = True

if idx_h * idx_w > m:
    print(ans)
    exit()
    
for i in range(idx_h):
    for j in range(idx_w):
        if not v[lh[i][1]][lw[j][1]]:
            print(ans)
            exit()

print(ans - 1)

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(h)]
for i in range(m):
    inp = list(map(int, input().split()))
    nh[inp[0] - 1] += 1
    nw[inp[1] - 1] += 1
    bomb[inp[0]-1].append(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
            if i in bomb[j]:
                flag = True
            if not flag:
                print(maxh + maxw)
                exit()
    else:
        print(maxh + maxw - 1)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int, input().split())
M=max(h, w)
lh=[0]*(M+1)
lw=[0]*(M+1)
point=[[-1,-1]]
for i in range(m):
  a,b=map(int, input().split())
  lh[a]=lh[a]+1
  lw[b]=lw[b]+1
  point.append([a,b])

mh=max(lh)
mw=max(lw)

pansh = [i for i, x in enumerate(lh) if x == mh]
pansw = [i for i, x in enumerate(lw) if x == mw]
  
d=0

for i in range(len(pansh)):
  for j in range(len(pansw)):
    if [pansh[i], pansw[j]] not in point:
      d=1
      break

if d==0:
  print(mh+mw-1)
else:
  print(mh+mw)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int, input().split())
hw=[list(map(int, input().split())) for _ in range(m)]

tate=[]
yoko=[]

for i in range(m):
    tate.append(hw[i][0])
    yoko.append(hw[i][1])

from collections import Counter
tatec=Counter(tate).most_common()
yokoc=Counter(yoko).most_common()

c_1_max=tatec[0][1]
c_1s=[]
for t in tatec:
    if t[1]==c_1_max:
        c_1s.append(t[0])
    else:
        break

c_2_max=yokoc[0][1]
c_2s=[]
for y in yokoc:
    if y[1]==c_2_max:
        c_2s.append(y[0])
    else:
        break

flag=False

for c_1 in c_1s:
    for c_2 in c_2s:
        tmp_flag=0
        for i in range(m):
            h_i=hw[i][0]
            w_i=hw[i][1]
            if h_i!=c_1 or w_i!=c_2:
                tmp_flag+=1
        if tmp_flag==m:
            flag=True


print(max(tatec[0][1]+yokoc[0][1], 1) if flag else max(tatec[0][1]+yokoc[0][1]-1, 1))

Submission status: Time Limit Exceeded

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

X = [0] * H
Y = [0] * W

B = []
h = H
while h:
    h -= 1
    B.append([])
    
A = []
m = M
while m:
    m -= 1
    x, y = map(int,input().split())
    x, y = x -1, y -1
    A.append((x, y))
    B[x].append(y)
    X[x] += 1
    Y[y] += 1

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

xm = max(X)
ym = max(Y)
xms = my_index_multi(X, xm)
yms = my_index_multi(Y, ym)

z = 0
for a, b in A:
    if a in xms:
        if b in yms: z += 1
if z == len(xms) * len(yms):
    ans = False
else:
    ans = True

cnt = xm + ym         
print(cnt if ans else cnt - 1)

Submission status: Time Limit Exceeded

Solution code:
val1 = input().split(" ")
H,W,M = [int(i) for i in val1]

#爆弾がある場所の二次元行列
h=[]
w=[]


for i in range(M):
    val2 = input().split(" ")
    h.append(int(val2[0]))
    w.append(int(val2[1]))
#################

#答えの数 爆弾の爆破できる数
maxbakuha = 0


# 高橋君１回ごとの爆破できる個数

for a in range(W):
    for b in range(H):
        bakuha=0
#    ht = b = 5
#    wt = a = 3
        for i in range(M):
            if b+1 ==h[i] or a+1 ==w[i]:
                bakuha += 1
        if maxbakuha < bakuha:
            maxbakuha = bakuha
            

print(maxbakuha)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter
def input(): return sys.stdin.readline().rstrip()


def main():
    H,W,m=map(int, input().split())
    Hline=[]
    wline=[]
    blist=[]
    for k in range(m):
        h,w=map(int, input().split())
        Hline.append(h)
        wline.append(w)
        blist.append([h,w])
    hc=Counter(Hline).most_common()
    wc=Counter(wline).most_common()
    ans=hc[0][1]+wc[0][1]
    hl=[]
    wl=[]
    for hcc in hc:
        if hcc[1]!= hc[0][1]:
            break
        hl.append(hcc[0])
    for wcc in wc:
        if wcc[1]!= wc[0][1]:
            break
        wl.append(wcc[0])
    for hll in hl:
        for wll in wl:
            if [hll,wll] not in blist:
                print(ans)
                exit()
    print(ans-1)

    
        



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

h_bomb = [0 for _ in range(h)]
w_bomb = [0 for _ in range(w)]

bomb_xy = {}
for ij in range(m):
    y, x = map(int, input().split())
    h_bomb[y-1] += 1
    w_bomb[x-1] += 1
    bomb_xy[str(y-1)+str(x-1)] = 1

h_max = max(h_bomb)
h_max_index = [i for i, y in enumerate(h_bomb) if y == h_max]

if str(h_max_index[0])+str(0) in bomb_xy:
    ans = h_bomb[h_max_index[0]] + w_bomb[0] - 1
else:
    ans = h_bomb[h_max_index[0]] + w_bomb[0]

# print("ans:", ans)
for j in h_max_index:
    for i in range(w):
        if str(j)+str(i) in bomb_xy:
            temp_max = h_bomb[j] + w_bomb[i] - 1
        else:
            temp_max = h_bomb[j] + w_bomb[i]

        # print("temp:", temp_max)
        if temp_max > ans:
            ans = temp_max

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int, input().split())
from collections import defaultdict
hd=defaultdict(int)
wd=defaultdict(int)
hd2=defaultdict(list)
wd2=defaultdict(list)
for i in range(m):
    h0,w0=map(int, input().split())
    hd[h0]+=1
    hd2[h0].append(w0)
    wd[w0]+=1
    wd2[w0].append(h0)

max_h_list = [kv[0] for kv in hd.items() if kv[1] == max(hd.values())]
max_w_list = [kv[0] for kv in wd.items() if kv[1] == max(wd.values())]

ans=float('inf')

if len(max_h_list)>=len(max_w_list):
    for x in max_h_list:
        hlist=hd2[x]
        for y in max_w_list:
            if y in hlist:
                ans=min(ans,1)
            else:
                ans=min(ans,0)
else:
    for x in max_w_list:
        wlist=wd2[x]
        for y in max_h_list:
            if y in wlist:
                ans=min(ans,1)
            else:
                ans=min(ans,0)
print(hd[max_h_list[0]]+wd[max_w_list[0]]-ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

def argmax_poss(a):
    if len(a) == 0:
        return []
    all_ = [0]
    max_ = a[0]
    for i in range(1, len(a)):
        if a[i] > max_:
            all_ = [i]
            max_ = a[i]
        elif a[i] == max_:
            all_.append(i)
    return all_

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

h_scores = [0 for i in range(h)]
w_scores = [0 for i in range(w)]

record = deque([])

for i in range(m):
  h,w = map(int,input().split())
  record.append((h-1,w-1))
  h_scores[h-1] += 1
  w_scores[w-1] += 1

max_h_poss = argmax_poss(h_scores)
max_w_poss = argmax_poss(w_scores)

out_pos_count = 0 

while record != deque([]):
    item = record.popleft()
    if item[0] in max_h_poss:
        if item[1] in max_w_poss:
            out_pos_count += 1
        

if len(max_h_poss) * len(max_w_poss) > out_pos_count:
    print(max(h_scores) + max(w_scores))
else:
    print(max(h_scores) + max(w_scores)-1)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import itertools
import numpy
def main():
    H, W, M = [int(j) for j in input().split()]
    Hx = [0]*H
    Wx = [0]*W
    dict = [[0] * W for i in range(H)]
    for i in range(M):
        h, w =  [int(j) for j in input().split()]
        dict[h-1][w-1]=1
        Hx[h-1] += 1
        Wx[w-1] += 1
    ans = max(Hx)+ max(Wx)
    maxIndex_Hx = [i for i, x in enumerate(Hx) if x == max(Hx)]
    maxIndex_Wx = [i for i, x in enumerate(Wx) if x == max(Wx)]

    ax = numpy.zeros((H,1))
    for i in maxIndex_Hx:
        ax[i][0] = 1
    ay = numpy.zeros((1,W))
    for i in maxIndex_Wx:
        ay[0][i] = 1
    az = numpy.dot(ax, ay)
    dot = numpy.multiply(numpy.array(dict),az)
    #print(dot)
    if numpy.sum(dot) != numpy.sum(az):
        print(ans)
    else:
        print(ans-1)
    '''
    if ans = H+W:
        print(H+W-1)
        return
    for j in list(itertools.product(maxIndex_Hx, maxIndex_Wx)):
        if dict[j[0]][j[1]] == 0:
            print(ans)
            return
    print(ans-1)
    '''
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
h,w,m = map(int,input().split())
hw = [list(map(int,input().split())) for _ in range(m)]

cnt_h = [0] * h
cnt_w = [0] * w

for h1,w1 in hw:
    cnt_h[h1-1] += 1
    cnt_w[w1-1] += 1

lh = []
lw = []

for i,e in enumerate(cnt_h):
    lh.append((e,i))

for i,e in enumerate(cnt_w):
    lw.append((e,i))

lh.sort(reverse = True)
lw.sort(reverse = True)

mh = lh[0][0]
mw = lw[0][0]

idx_h = 0
idx_w = 0

cnt = 0
for i,j in lh:
    if i == mh:
        cnt += 1
    else:
        break

idx_h = cnt

cnt = 0
for i,j in lw:
    if i == mw:
        cnt += 1
    else:
        break

idx_w = cnt

ans = mh + mw

v = [[False] * w for _ in range(h)]
for h1,w1 in hw:
    v[h1-1][w1-1] = True

for i in range(idx_h):
    for j in range(idx_w):
        if not v[lh[i][1]][lw[j][1]]:
            print(ans)
            exit()

print(ans - 1)

Submission status: Time Limit Exceeded

Solution code:
import sys
def print2(s):
    print(*s, sep="\n")

[h, w, m] = map(int, input().split())
pos = []
for i in range(m):
    tmp = input().split()
    tmp[0] = int(tmp[0])
    tmp[1] = int(tmp[1])
    pos.append(tmp)
# print2(pos)
mh = [0 for i in range(h+1)]
mw = [0 for i in range(w+1)]
# print(len(pos))
for i in range(len(pos)):
    mh[pos[i][0]] += 1
    mw[pos[i][1]] += 1
# print(mh)
# print(mw)
mmh = max(mh)
mmw = max(mw)
mmhs = []
mmws = []
for i in range(h+1):
    if mh[i] == mmh:
        mmhs.append(i)
for i in range(w+1):
    if mw[i] == mmw:
        mmws.append(i)
# print(mmhs)
# print(mmws)
for i in mmhs:
    for j in mmws:
        if [i, j] not in pos:
            print(mmh + mmw)
            sys.exit(0)
print(mmh+mmw-1)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int, input().split())
from collections import defaultdict
hd=defaultdict(int)
wd=defaultdict(int)
hd2=defaultdict(list)
wd2=defaultdict(list)
for i in range(m):
    h0,w0=map(int, input().split())
    hd[h0]+=1
    hd2[h0].append(w0)
    wd[w0]+=1
    wd2[w0].append(h0)

max_h_list = [kv[0] for kv in hd.items() if kv[1] == max(hd.values())]
max_w_list = [kv[0] for kv in wd.items() if kv[1] == max(wd.values())]

ans=float('inf')

if len(max_h_list)<=len(max_w_list):
    for x in max_h_list:
        hlist=hd2[x]
        for y in max_w_list:
            if y in hlist:
                ans=min(ans,1)
            else:
                ans=min(ans,0)
else:
    for x in max_w_list:
        wlist=wd2[x]
        for y in max_h_list:
            if y in wlist:
                ans=min(ans,1)
            else:
                ans=min(ans,0)
print(hd[max_h_list[0]]+wd[max_w_list[0]]-ans)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int,input().split())
bomb=[list(map(int,input().split())) for _ in range(m)]
a=[0 for i in range(h+1)]
b=[0 for _ in range(w+1)]
for i in range(m):
    a[bomb[i][0]]+=1
    b[bomb[i][1]]+=1
am=max(a)
bm=max(b)
aa=[i for i, x in enumerate(a) if x==am]
bb=[i for i, x in enumerate(b) if x==bm]
for i in aa:
    for j in bb:
        if not [i,j] in bomb:
            print(am+bm)
            exit()
print(am+bm-1)

Submission status: Time Limit Exceeded

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

hdic = {}
wdic = {}
hws_h = {}

for m in range(M):
    h, w = map(int, input().split())
    if h in hdic.keys():
        hdic[h] += 1
        hws_h[h].append(w)
    else:
        hdic[h] = 1
        hws_h[h] = [w]
    
    if w in wdic.keys():
        wdic[w] += 1
    else:
        wdic[w] = 1

hmax = max(hdic.values())
wmax = max(wdic.values())

hs = []
ws = []

hs = [k for k, v in hdic.items() if v==hmax]
ws = [k for k, v in wdic.items() if v==wmax]

f = 1
for h in hs:
    wlist = hws_h[h]
    for w in ws:
        if w not in wlist:
            f = 0
            break
    if f == 0:
        break

print(hmax + wmax - f)

Submission status: Time Limit Exceeded

Solution code:
H,W,M = map(int,input().split())
h = list()
w = list()
for i in range(M):
    hh,ww = map(int,input().split())
    h.append(hh)
    w.append(ww)
ch = 0
hh = list()
for i in range(H):
    a = h.count(i+1)
    if ch < a:
        hh = list()
        hh.append(i+1)
        ch = a
    elif ch == a:
        hh.append(i+1)
cw = 0
ans = 0
for i in hh:
    ww = list()
    cnt = 0
    for ii in range(M):
        if i == h[ii]:
            ww.append(w[ii])
    for ii in range(W):
        if (ii+1) in ww:
            cnt = -1
        cnt += w.count(ii+1)
        #print('{}:{}:{}:{}'.format(cnt,i,ww,ii))
        ans = max(ans,cnt)
        cnt = 0
print(ans+ch)

Submission status: Time Limit Exceeded

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


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    h, w, m = MI()
    targets = [[] for i in range(h)]
    tate = [[] for i in range(w)]
    tate_length = [0] * w
    original = []
    lengths = [0] * h
    for i in range(m):
        target = LI()
        original.append(target)
        targets[target[0] - 1].append(target[1] - 1)
        tate[target[1] - 1].append(target[0] - 1)
        lengths[target[0] - 1] += 1
        tate_length[target[1] - 1] += 1

    max_length = max(lengths)
    max_h_indexes = [i for i, x in enumerate(lengths) if x == max_length]
    semi_max_h_indexes = [i for i, x in enumerate(
        lengths) if x == max_length - 1]

    for j in range(len(tate)):
        tate[j] = set(tate[j])
    ans = 0

    for index in max_h_indexes:
        for j in range(w):
            if index in tate[j]:
                ans = max(ans, tate_length[j] + lengths[index] - 1)
            else:
                ans = max(ans, tate_length[j] + lengths[index])
    for index in semi_max_h_indexes:
        for j in range(w):
            if index in tate[j]:
                ans = max(ans, tate_length[j] + lengths[index] - 1)
            else:
                ans = max(ans, tate_length[j] + lengths[index])

    print(ans)


main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
import sys
input = sys.stdin.readline

H, W, M = map(int, input().split())
Hsum = [0] * H
Wsum = [0] * W
hw = []
for m in range(M):
  h, w = map(int, input().split())
  Hsum[h-1] += 1
  Wsum[w-1] += 1
  hw.append(str(h-1)+"_"+str(w-1))

hmax_ind = list(map(lambda x: True if x == max(Hsum) else False, Hsum))
hmax_ind = list(itertools.compress(range(len(Hsum)), hmax_ind))
wmax_ind = list(map(lambda x: True if x == max(Wsum) else False, Wsum))
wmax_ind = list(itertools.compress(range(len(Wsum)), wmax_ind))
 
res = []
for n in itertools.product(hmax_ind, wmax_ind):
  res.append(str(n[0])+"_"+str(n[1]))

if len(set(res).intersection(set(hw))) == len(res):
  print(max(Hsum) + max(Wsum)-1)
else:
  print(max(Hsum) + max(Wsum))

Submission status: Time Limit Exceeded

Solution code:
#import numpy as np
import collections
#import math




#f = open('./input_E.txt')
#
#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])



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





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

num_kouho = len(H_kouho)*len(W_kouho)
if num_kouho <= M:
    kouho = [x for x in bakuha if x[0] in H_kouho and x[1] in W_kouho]
    if len(kouho) == len(H_kouho)*len(W_kouho):
        ans-=1

#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:
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]
lenlen = len(lsmaxx)*len(lsmaxy)
for i in range(M):
    if lsxy[i][0] in lsmaxx and lsxy[i][1] in lsmaxy:
        lenlen -= 1
if lenlen == 0:
    factor = 1
else:
    factor = 0
print(maxx+maxy-factor)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# モジュールのインポート
import itertools
import numpy as np

# 標準入力を取得
H, W, M = list(map(int, input().split()))
B = []
I, R = {}, {}
for m in range(M):
    h_m, w_m = list(map(int, input().split()))
    B.append((h_m, w_m))
    if h_m in I.keys():
        I[h_m].append(w_m)
    else:
        I[h_m] = [w_m]
    if w_m in R.keys():
        R[w_m].append(h_m)
    else:
        R[w_m] = [h_m]

# 求解処理
h_bom = []
max_bom_h = 0
for h in I.keys():
    bom_h = len(I[h])
    if max_bom_h == bom_h:
        h_bom.append(h)
        continue
    if max_bom_h < bom_h:
        max_bom_h = bom_h
        h_bom = [h]

w_bom = []
max_bom_w = 0
for w in R.keys():
    bom_w = len(R[w])
    if max_bom_w == bom_w:
        w_bom.append(w)
        continue
    if max_bom_w < bom_w:
        max_bom_w = bom_w
        w_bom = [w]

can_separate = False
for h_b, w_b in itertools.product(h_bom, w_bom):
    if (h_b, w_b) not in B:
        can_separate = True
        break

ans = max_bom_h + max_bom_w
if not can_separate:
    ans -= 1

# 結果出力
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

h = []
w = []
point = []
for i in range(M):
    a, b = list(map(int, input().split()))

    h.append(a)
    w.append(b)
    point.append((a, b))

h_count = collections.Counter(h)
w_count = collections.Counter(w)

max = 0
for i in range(1, H+1):
    for j in range(1, W+1):
        count = h_count[i] + w_count[j]

        if ((i, j) in point):
            count = count - 1

        if (count > max):
            max = count

print(max)

Submission status: Time Limit Exceeded

Solution code:
h,w,m = map(int, input().split())
row = [0]*(h+1)
rn = [[] for _ in range(h+1)]
col = [0]*(w+1)
cn = [[] for _ in range(w+1)]
data = []
for _ in range(m):
  hd,wd = map(int, input().split())
  row[hd] += 1
  rn[hd].append(wd)
  col[wd] += 1
  cn[wd].append(hd)

cm = max(col)
rm = max(row)
ri = [x for x,y in enumerate(row) if y == rm]
ci = [x for x,y in enumerate(col) if y == cm]
ans = -1
for c in ci:
  for r in ri:
    if c not in rn[r]:
      ans = max(ans,cm+rm)
      print(ans)
      exit()
    else:
      ans = max(ans,cm+rm-1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

H,W,M = map(int, input().split())
hw = [list(map(int, input().split())) for _ in range(M)]
h = [h1 for h1,w1 in hw]
w = [w1 for h1,w1 in hw]
ch = collections.Counter(h).most_common()
mh = []
mnh = ch[0][1]
for ch1 in ch:
    if mnh == ch1[1]:
        mh.append(ch1[0])
        continue
    else:
        break
    break
cw = collections.Counter(w).most_common()
mw = []
mnw = cw[0][1]
for cw1 in cw:
    if mnw == cw1[1]:
        mw.append(cw1[0])
        continue
    else:
        break
    break
for h1 in mh:
    for w1 in mw:
        if [h1,w1] not in hw:
            print(mnh+mnw)
            exit()
print(mnh+mnw-1)

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])
a=max(hl)
b=max(wl)
# hmax= [i for i, x in enumerate(hl) if x ==a]
# wmax=[i for i, x in enumerate(wl) if x ==b]
hmax=np.where(hl == hl.max())[0]
wmax=np.where(wl == wl.max())[0]
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)