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:
H,W,M=map(int,input().split())
A=[[0]*H]*W
Bomb_ls=[]
R=[0]*H
C=[0]*W
for i in range(M):
  X,Y=[int(x)-1 for x in input().split()]
  R[X]+=1
  C[Y]+=1
  Bomb_ls.append([X,Y])
score=0
for i in range(H):
  for j in range(W):
    if not [i,j] in Bomb_ls:
      score=max(score,R[i]+C[j])
    else:
      score=max(score,R[i]+C[j]-1)
print(score)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return list(map(int, sys.stdin.readline().split()))
def II(): return int(sys.stdin.readline())
def LS(): return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]

def main():
    h, w, m = LI()
    mat = [[0 for i in range(w+1)] for i in range(h+1)]
    boms = []
    hs = []
    ws = []
    for i in range(m):
        hi, wi = LI()
        mat[hi][wi] = 1
        hs.append(hi)
        ws.append(wi)
        boms.append((hi, wi))

    c_hs = Counter(hs)
    c_ws = Counter(ws)

    hsums_top = set()
    wsums_top = set()

    maxh = 0
    for i, top in c_hs.items():
        if maxh == top:
            hsums_top.add(i)
        elif maxh < top:
            maxh = top
            hsums_top = {i}

    maxw = 0
    for i, top in c_ws.items():
        if maxw == top:
            wsums_top.add(i)
        elif maxw < top:
            maxw = top
            wsums_top = {i}

    if len(hsums_top) * len(wsums_top) > m:
        print(maxh + maxw)
        return
    for i in hsums_top:
        for j in wsums_top:
            if mat[i][j] == 0:
                print(maxh + maxw)
                return
    print(maxh + maxw - 1)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter

H, W, M = map(int, input().split())
h_count = [[i, 0] for i in range(H)]
w_count = [[i, 0] for i in range(W)]
exist = dict()
for _ in range(M):
    h, w = map(lambda x: int(x)-1, input().split())
    h_count[h][1] += 1
    w_count[w][1] += 1
    exist[(h, w)] = True

h_count.sort(key=itemgetter(1), reverse=True)
w_count.sort(key=itemgetter(1), reverse=True)
h_max = h_count[0][1]
w_max = w_count[0][1]
res = 0
is_dup = True
for h in h_count:
    if h[1] != h_max and is_dup:
        break
    for w in w_count:
        if w[1] != w_max and is_dup:
            break
        val = h[1] + w[1]
        if exist.get((h[0], w[0]), False):
            val -= 1
        else:
            is_dup = False
        res = max(res, val)
print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
h, w, m = map(int, input().split())
row = [0 for _ in range(h)]
col = [0 for _ in range(w)]
info = dict()
tmpx = 0
tmpy = 0
for _ in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    if x not in info:
        info[x] = set()
    info[x].add(y)
    row[x] += 1
    if row[x] > tmpx:
        tmpx = row[x]
    col[y] += 1
    if col[y] > tmpy:
        tmpy = col[y]

xset = []
yset = []
for idx, v in enumerate(row):
    if tmpx == v:
        xset.append(idx)
for idx, v in enumerate(col):
    if tmpy == v:
        yset.append(idx)
res = 0
for x in xset:
    for y in yset:
        tmp = row[x]+col[y]
        if x in info and y in info[x]:
            tmp -= 1
        res = max(res, tmp)
print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

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

row_dict = defaultdict(int)
col_dict = defaultdict(int)
row_col_dict = defaultdict(list)
for _ in range(m):
    row, col = map(int, input().split())
    row_dict[row] += 1
    col_dict[col] += 1
    row_col_dict[row].append(col)

max_row = max(row_dict.values())
max_col = max(col_dict.values())

max_rows = [k for k, v in row_dict.items() if v == max_row]
max_cols = [k for k, v in col_dict.items() if v == max_col]
ans = max_row + max_col - 1
flg = False
for row in max_rows:
    for col in max_cols:
        if not col in row_col_dict[row]:
            ans += 1
            flg = True
            break
    if flg:
        break

print(ans)

Submission status: Time Limit Exceeded

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

targets = []

for _ in range(M):
    targets.append([int(v) for v in input().split()])

grid = []
for _ in range(H):
    grid.append([0] * W)

for x, y in targets:
    for i in range(H):
        grid[i][y-1] += 1
    
    for j in range(W):
        grid[x-1][j] += 1

    grid[x-1][y-1] -= 1

max_val = max(list(map(lambda x: max(x), grid)))

print(max_val)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

def argmax_poss(xs):
    max_value = max(xs)
    out = []
    for i in range(len(xs)):
        if xs[i] == max_value:
            out.append(i)
    return(out)

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:
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 = xcnt.most_common(1)[0][1]
ycmax = ycnt.most_common(1)[0][1]
xlist = [k for k,v in xcnt.items() if v == xcmax]
ylist = [k for k,v in ycnt.items() if v == ycmax]

tlist = [0 for i,j in zip(x,y) if (i in xlist) and(j in ylist)]
if len( xlist ) * len(ylist) == len(tlist):
  print( xcmax + ycmax -1 )
else:
  print( xcmax + ycmax )

Submission status: Time Limit Exceeded

Solution code:
def Qe():
    h, w, m = map(int, input().split())
    targets = []
    th = set()
    tw = set()
    for i in range(m):
        h1, w1 = map(int, input().split())
        targets.append((h1, w1))
        th.add(h1)
        tw.add(w1)

    c = 0
    lth = list(th)
    ltw = list(tw)
    lth.sort()
    ltw.sort()
    for hh in lth:
        for ww in ltw:
            s = 0
            for h1, w1 in targets:
                if hh == h1 or ww == w1:
                    s += 1
            if c < s:
                c = s
    print(c)


if __name__ == '__main__':
    Qe()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline

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

    H_list = [[0, i] for i in range(H + 1)] 
    W_list = [[0, i] for i in range(W + 1)] 


    S = set()
    S_add = S.add
    INF = 10 ** 6

    for _ in range(M):
        a, b = map(int, input().split())
        tmp = INF * a + b
        S_add(tmp)
        H_list[a][0] += 1
        W_list[b][0] += 1

    ans = 0
    H_list.sort(reverse=True)
    W_list.sort(reverse=True)

    tmp_H = H_list[0][0]
    tmp_W = W_list[0][0]
    tmp_ans = tmp_H + tmp_W

    for h in range(H):
        if H_list[h][0] + 2 < tmp_H:
            continue
        h_ = H_list[h][1]
        for w in range(W):
            if W_list[w][0] + 2 < tmp_W:
                break
            w_ = W_list[w][1]
            tmp = H_list[h][0] + W_list[w][0]
            if INF * h_ + w_ in S:
                tmp -= 1
            ans = max(ans, tmp)
            if ans == tmp_ans:
                print (ans)
                return

    print (ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import itertools

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

h = np.array([0]*H)
w = np.array([0]*W)

bom= np.array([[0]*W]*H)

for _ in range(M):
    i, j = map(int, input().split())
    h[i-1] += 1
    w[j-1] += 1
    bom[i-1][j-1] += 1
m= []
for i in range(H):
   m.append(w + h[i]) 
q= m -bom

n = list(itertools.chain.from_iterable(q))
print(max(n))

Submission status: Time Limit Exceeded

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

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


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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
h, w, m = map(int, input().split())
row = [0 for _ in range(h)]
col = [0 for _ in range(w)]
info = dict()
tmpx = 0
tmpy = 0
xset = []
yset = []
for _ in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    if x not in info:
        info[x] = set()
    info[x].add(y)
    row[x] += 1
    if row[x] >= tmpx:
        tmpx = row[x]
        if row[x] > tmpx:
            xset = [x]
        else:
            xset.append(x)
    col[y] += 1
    if col[y] >= tmpy:
        tmpy = col[y]
        if col[y] > tmpy:
            yset = [y]
        else:
            yset.append(y)

for idx, v in enumerate(row):
    if tmpx == v:
        xset.append(idx)
for idx, v in enumerate(col):
    if tmpy == v:
        yset.append(idx)
res = 0
for x in xset:
    for y in yset:
        tmp = row[x]+col[y]
        if x in info and y in info[x]:
            tmp -= 1
        res = max(res, tmp)
print(res)

Submission status: Time Limit Exceeded

Solution code:
H, W, M = map(int, input().split())
h = [0] * (H+1)
w = [0] * (W+1)
l = []
for i in range(M):
  x, y = map(int, input().split())
  h[x] += 1
  w[y] += 1
  l.append([x, y])
  
c = max(h)
d = max(w)
a = [i for i, x in enumerate(h) if x == c]
b = [i for i, x in enumerate(w) if x == d]

flag = False
for s in a:
  for t in b:
    if [s, t] not in l:
      flag = True
      break
if flag:
  ans = c+d
else:
  ans = c+d-1
print(ans)

Submission status: Time Limit Exceeded

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


listH = [0 for _ in range(H+1)]
listW = [0 for _ in range(W+1)]

for _ in range(M):
    hi, wi = map(int,input().split())
    listH[hi] += 1
    listW[wi] += 1
    Bom.append((hi,wi))

def find_max(listA):
    indexes = []
    max_value = 0
    for i,a in enumerate(listA):
        if max_value < a:
            indexes = [i]
            max_value = a
        elif max_value == a:
            indexes.append(i)
    return indexes

maxH = find_max(listH)
maxW = find_max(listW)

res = 0
for i in maxH:
    for j in maxW:
        if (i,j) in Bom:
            temp = listH[i] + listW[j] - 1
        else:
            temp = listH[i] + listW[j]
        if temp > res:
            res = temp

print(res)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int,input().split())
hlist=[0]*(h+1)
wlist=[0]*(w+1)

cnt=[[False]*(w+1) for i in range(h+1)]
for i in range(m):
  a,b=map(int,input().split())
  hlist[a]+=1
  wlist[b]+=1
  cnt[a][b]=True
  

hcnt,wcnt=max(hlist),max(wlist)

hh,ww=[],[]

for i in range(1,h+1):
  if hlist[i]==hcnt:
    hh+=[i]

for i in range(1,w+1):
  if wlist[i]==wcnt:
    ww+=[i]

for i in hh:
  for j in ww:
    if cnt[i][j]==False:
      print(hcnt+wcnt)
      exit()
  
print(hcnt+wcnt-1)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int,input().split())
h=[0]*m
w=[0]*m
for i in range(m):
  h[i],w[i]=map(int,input().split())
  
h_cnt=[]
w_cnt=[]
h_set=list(set(h))
w_set=list(set(w))
for i in h_set:
  h_cnt.append(h.count(i))
for i in w_set:
  w_cnt.append(w.count(i))

h_indices=[i for i, x in enumerate(h_cnt) if x ==max(h_cnt)]
w_indices=[i for i, x in enumerate(w_cnt) if x ==max(w_cnt)]

h_max=[h_set[i] for i in h_indices]
w_max=[w_set[i] for i in w_indices]

flag=True
for i in h_max:
  for j in w_max:
    if (i,j) not in list(zip(h,w)):
      print(max(h_cnt)+max(w_cnt))
      flag=False
      break

if flag:
  print(max(h_cnt)+max(w_cnt)-1)

Submission status: Time Limit Exceeded

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

h,w,m=map(int,input().split())
l=[[0 for i in range(w+1)] for j in range(h+1)]
row=[0 for i in range(h+1)]
col=[0 for j in range(w+1)]

for i in range(m):
    x,y=map(int,input().split())
    l[x][y]=1
    row[x]+=1
    col[y]+=1
    
maxr=max(row)
maxc=max(col)
maxa=maxr+maxc
p=1
cl,rl=[],[]
for i in range(1,h+1):
    if(row[i]==maxr):rl.append(i)
for j in range(1,w+1):
    if(col[j]==maxc):cl.append(j)
    
for x in rl:
    for y in cl:
        if(l[x][y]==0):p=0;break
    if(p==0):break
    
print(maxa-p)

Submission status: Time Limit Exceeded

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

# 標準入力を取得
H, W, M = list(map(int, input().split()))
I, R = {}, {}
for m in range(M):
    h_m, w_m = list(map(int, input().split()))
    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]

max_bom_w = 0
for h_b in h_bom:
    h_bom_w = 0
    for w in R.keys():
        bom_w = len(R[w])
        if h_b in R[w]:
            bom_w -= 1
        if h_bom_w < bom_w:
            h_bom_w = bom_w
    max_bom_w = max(max_bom_w, h_bom_w)

ans = max_bom_h + max_bom_w

# 結果出力
print(ans)

Submission status: Time Limit Exceeded

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

import sys
import math

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        for line in sys.stdin:
            lines.append(line.strip())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
    
    (h, w, m) = [int(e) for e in lines[0].split(" ")]
    hw = []
    for i in range(1, m + 1):
        hw.append([int(e) - 1 for e in lines[i].split(" ")])

    return (h, w, m, hw)

def solve(h, w, m, hw):

    d = dict()
    for e in hw:
        d[(e[0], e[1])] = 1

    current = 0
    for i in range(h):
        for j in range(w):
            
            total = 0
            for k in range(w):
                total = total + d.get((i, k), 0)
            for l in range(h):
                total = total + d.get((l, j), 0)

            if d.get((i, j)) == 1:
                total = total - 1

            if total > current:
                current = total

    return current



def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Submission status: Time Limit Exceeded

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

targets = set()
row = [0]*H
col = [0]*W

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

# for h in range(H):
#     for w in range(W):
#         row[h] += 1
#         col[w] += 1
#         targets.add((h, w))
# print('ready')

r_max = max(row)
c_max = max(col)
# print([h for h in range(H) if row[h] == r_max])
# print([w for w in range(W) if col[w] == c_max])
max_nearly = set([(h, w) for w in range(W) if col[w] == c_max for h in range(H) if row[h] == r_max])
if len(max_nearly - targets) == 0:
    # print('hoge')
    print(r_max + c_max - 1)
else:
    print(r_max + c_max)

Submission status: Time Limit Exceeded

Solution code:
h,w,m = map(int,input().split())
c=[list(map(int,input().split())) for i in range(m)]
#print(c)
ans = 0
aa = {}
bb = {}
z = 1

for i in range(m):
  if c[i][0] in aa:
    aa[c[i][0]] += 1
  else:
    aa[c[i][0]] = 1
    
  if c[i][1] in bb:
    bb[c[i][1]] += 1
  else:
    bb[c[i][1]] = 1
    
al = [k for k, v in aa.items() if v == max(aa.values())]
bl = [k for k, v in bb.items() if v == max(bb.values())]

for x in al:
  for y in bl:
    if [x,y] not in c:
      z = 0
    
print(max(aa.values())+ max(bb.values())-z)

Submission status: Time Limit Exceeded

Solution code:
import collections

H, W, M = map(int, input().split())
h_list = []
w_list = []
m_list = []

for i in range(M):
    h, w = map(int, input().split())
    h_list.append(h)
    w_list.append(w)
    m_list.append([h, w])

count_h = collections.Counter(h_list)
count_w = collections.Counter(w_list)
pair_h = count_h.most_common()
pair_w = count_w.most_common()
pair_h_max = []
pair_w_max = []

for i in range(len(pair_h)):
    if pair_h[i][1] == pair_h[0][1]:
        pair_h_max.append(pair_h[i])
    else:
        break

for i in range(len(pair_w)):
    if pair_w[i][1] == pair_w[0][1]:
        pair_w_max.append(pair_w[i])
    else:
        break
# print(pair_h_max)
# print(pair_w_max)

count_max_list = []
for i in range(len(pair_h_max)):
    for j in range(len(pair_w_max)):
        if [pair_h_max[i][0], pair_w_max[j][0]] in m_list:
            count_max_list.append(pair_h_max[i][1] + pair_w_max[j][1] - 1)
        else:
            count_max_list.append(pair_h_max[i][1] + pair_w_max[j][1])

print(max(count_max_list))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# Input#
# n = int(input())
h, w, m = map(int, input().split())
tar_list = []

h_count = [0] * (h + 1)
w_count = [0] * (w + 1)

h_max = 0
w_max = 0

h_maxlist = []
w_maxlist = []

ans = 0
ans_tmp = 0

for i in range(m):
    h_in, w_in = map(int, input().split())
    tar_list.append((h_in, w_in))

    h_count[h_in] += 1
    if h_count[h_in] > h_max:
        h_max = h_count[h_in]
        h_maxlist = [h_in]
    elif h_count[h_in] == h_max:
        h_maxlist.append(h_in)

    w_count[w_in] += 1
    if w_count[w_in] > w_max:
        w_max = w_count[w_in]
        w_maxlist = [w_in]
    elif w_count[w_in] == w_max:
        w_maxlist.append(w_in)


for i in range(len(h_maxlist)):
    tar_h = h_maxlist[i]
    for j in range(len(w_maxlist)):
        tar_w = w_maxlist[j]
        if (tar_h, tar_w) in tar_list:
            ans_tmp = h_count[tar_h] + w_count[tar_w] - 1
            ans = max(ans, ans_tmp)
        else:
            ans_tmp = h_count[tar_h] + w_count[tar_w]
            ans = max(ans, ans_tmp)


print("{}".format(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

h,w,m = map(int, input().split())
column = [0 for i in range(h+1)]
row = [0 for i in range(w+1)]
column_max = 0
row_max = 0
column_max_index = []
row_max_index = []
line = []

for i in range(m):
  a,b = map(int, input().split())
  column[a] += 1
  row[b] += 1
  if column[a] == column_max:
    column_max_index.append(a)
    
  if column[a] > column_max:
    column_max = column[a]
    column_max_index = [a]
  
  if row[b] == row_max:
    row_max_index.append(b)
    
  if row[b] > row_max:
    row_max = row[b]
    row_max_index = [b]
  line.append([a,b])
  
par = []
for i in column_max_index:
  for j in row_max_index:
    par.append([i,j])

count = 0
for i in par:
  if i not in line:
    count += 1
    break
if count != 0:
  print(column_max + row_max)
else:
  print(column_max + row_max - 1)

Submission status: Time Limit Exceeded

Solution code:
import collections
h, w, m = map(int, input().split())
hw =[list(map(int, input().split())) for i in range(m)]
hh = [0]*m
ww = [0]*m
for i in range(m):
  hh[i] = hw[i][0]
  ww[i] = hw[i][1]


hcnt = collections.Counter(hh).most_common()[0][1]
wcnt = collections.Counter(ww).most_common()[0][1]
ans = hcnt +wcnt

hhh = []
www = []
for i in range(len(collections.Counter(hh))):
  if hcnt == collections.Counter(hh).most_common()[i][1]:
    hhh.append(collections.Counter(hh).most_common()[i][0])
for i in range(len(collections.Counter(ww))):
  if wcnt == collections.Counter(ww).most_common()[i][1]:
    www.append(collections.Counter(ww).most_common()[i][0])

    
newhw = [[i,j] for i in hhh for j in www]
cnt = 0
for i in range(len(newhw)):
  if newhw[i] in hw:
    cnt += 1
if cnt == len(newhw):
  ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W, M = map(int, input().split())
h = [0] * (H+1)
w = [0] * (W+1)
l = []
for i in range(M):
  x, y = map(int, input().split())
  h[x] += 1
  w[y] += 1
  l.append([x, y])
  
a = []
b = []
c = max(h)
d = max(w)
for i in range(H+1):
  if h[i] == c:
    a.append(i)
for i in range(W+1):
  if w[i] == d:
    b.append(i)

flag = False
for s in a:
  for t in b:
    if [s, t] not in l:
      flag = True
      break
if flag:
  ans = c+d
else:
  ans = c+d-1
print(ans)

Submission status: Time Limit Exceeded

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

list1 = []
for i in range(M):
    list1.append(list(map(int, input().split())))

listh = [0 for i in range(H+1)]

for k in range(M):
    listh[list1[k][0]]+=1

max1 = max(listh)
loc1 = [0]

for i in range(1, len(listh)):
    if max1 == listh[i]:
        loc1.append(i)

#print("Loc1 is ", loc1)
max2_total = 0
for jjj in range(1, len(loc1)):
    listw = [0 for i in range(W+1)]
    #print("jjj is ",jjj)
    for k in range(M):
        if list1[k][0] == loc1[jjj]:
            continue
        listw[list1[k][1]]+=1
    #print("listw is",listw)
    max2 = max(listw)
    #print("max2 is ",max2)
    if max2_total<max2:
        max2_total = max2

print(max1 + max2_total)

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
for i in M1L:
  for j in M2L:
    if [i,j] not in G3:
      c = 1
      break
  if c ==1:
    break
if c == 1:
  print(M1 + M2)
else:
  print(M1 + M2 - 1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
h,w,m=map(int, input().split())
hw=[list(map(int,input().split())) for i in range(m)]
H=np.zeros(h)
W=np.zeros(w)
for i in range(m):
    hi,wi=hw[i]
    H[hi-1]+=1
    W[wi-1]+=1
hmax=[i for i, x in enumerate(H) if x == max(H)]
wmax=[i for i, x in enumerate(W) if x == max(W)]
ans=0
for hi in hmax:
    for wi in wmax:
        if [hi+1,wi+1] in hw:
            ans=max(ans,max(H)+max(W)-1)
        else:
            ans=max(ans,max(H)+max(W))
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

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

most_common_h_list = []
most_common_w_list = []

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

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

ans = []
for hh in most_common_h_list:
    for ww in most_common_w_list:
        for m in range(M):
            if h[m] == hh and w[m] == ww:
                ans.append(most_common_h_count + most_common_w_count - 1)
                break
        else:
            ans.append(most_common_h_count + most_common_w_count)

ans = sorted(ans, reverse=True)
print(ans[0])

Submission status: Time Limit Exceeded

Solution code:
H, W, M = map(int, input().split())
h = [0 for i in range(H)]
w = [0 for i in range(W)]
dp = [[0 for i in range(W)] for j in range(H)]

for i in range(M):
    hh, ww = map(int, input().split())
    dp[hh - 1][ww - 1] += 1

for i in range(H):
    h[i] = sum(dp[i])

for i in range(W):
    w[i] = sum(dp[j][i] for j in range(H))

hhh = max(h)
www = max(w)
hhhh = [i for i, x in enumerate(h) if x == hhh]
wwww = [i for i, x in enumerate(w) if x == www]
check = 0


for i in hhhh:
    if check == 1:
        break
    for j in wwww:
        if dp[i][j] == 0:
            check = 1
            break
print(hhh + www - (1 - check))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
number=list(map(int,input().split()))
H,W,M=number[0],number[1],number[2]
h_count=[0 for i in range(H+1)]
w_count=[0 for i in range(W+1)]
T=[]
ans=[]
flag=0
for i in range(M):
    S=list(map(int,input().split()))
    T.append(S)
    h_count[S[0]]+=1
    w_count[S[1]]+=1
h=max(h_count)
w=max(w_count)
if [h_count.index(h),w_count.index(w)] in T:
    h_count=np.array(h_count)
    w_count=np.array(w_count)
    h_max=[i for i, x in enumerate(h_count) if x == h]
    w_max=[i for i, x in enumerate(w_count) if x == w]
    for i in h_max:
        for j in w_max:
            if [i,j] in T:
                pass
            else:
                print(h+w)
                flag=1
                break
    if flag==0:
        print(h+w-1)
else:
    print(int(h+w))

Submission status: Time Limit Exceeded

Solution code:
def abc176_e():
    h, w, m = map(int, input().split())
    ch = [0]*h
    cw = [0]*w
    bs = dict()
    for _ in range(m):
        hi, wi = map(lambda x: int(x)-1, input().split())
        ch[hi] += 1
        cw[wi] += 1
        bs[(hi,wi)] = 1
    mch = max(ch)
    mcw = max(cw)
    if mch == h and mcw == w:
        ans = h + w - 1
    else:
        flag = False
        for i in range(h):
            if ch[i] < mch: continue
            for j in range(w):
                if cw[j] < mcw: continue
                if bs.get((i,j)) == None:
                    flag = True
                    break
            if flag: break
        if flag: ans = mch + mcw
        else: ans = mch + mcw - 1
    print(ans)

if __name__ == '__main__':
    abc176_e()

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)
    else:
      ans = max(ans,cm+rm-1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W ,M = map(int,input().split())
t = [0] * M
x = [0] * M
for i in range(M):
    t[i], x[i] = map(int, input().split())
a=0
nokosu=[]
for i in range(M):
    nokosu.append((t[i],x[i]))
for i in range(H):
    for j in range(W):
        p=0
        if (i+1,j+1) in nokosu:
            p=1
        if t.count(i+1)+x.count(j+1)-p>a:
            a=t.count(i+1)+x.count(j+1)-p
print(a)

Submission status: Time Limit Exceeded

Solution code:
H,W,M = map(int,input().split())
h = [0]*M
w = [0]*M

from collections import defaultdict
counter_h = defaultdict(int)
counter_w = defaultdict(int)

for i in range(M):
    h[i],w[i] = map(int, input().split())
    counter_h[h[i]] += 1
    counter_w[w[i]] += 1
    
MAX_h = max(counter_h.values())
h_list = [k for k, v in counter_h.items() if v == MAX_h]
MAX_w = max(counter_w.values())
w_list = [k for k, v in counter_w.items() if v == MAX_w]

import itertools
X = list(itertools.product(h_list, w_list))

ans = MAX_h+MAX_w
check = 0

for i in range(M):
    if (h[i],w[i]) in X:
        check += 1
        
if len(X)==check:
    ans -= 1
    
print(ans)

Submission status: Time Limit Exceeded

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

targetH = [0] + [0] * M
targetW = [0] + [0] * M
for i in range(M):
    targetH[i] = Target[i][0]
    targetW[i] = Target[i][1]

dp= [[0]*(W+1) for i in range(H+1)] 

cntH = collections.Counter(targetH)
cntW = collections.Counter(targetW)

for i in range(1,H+1):
    for j in range(1,W+1):
        dp[i][j] = cntH[i] + cntW[j]
        if [i, j] in Target:
            dp[i][j] -= 1

print(max(list(map(lambda x: max(x), dp))))

Submission status: Time Limit Exceeded

Solution code:
row, col, m = map(int, input().split())
bomb = [list(map(int, input().split())) for i in range(m)]
row_bomb = [0]*row
col_bomb = [0]*col


for i in range(m):
  h, w = bomb[i]
  bomb[i] = [h-1, w-1]
  row_bomb[h-1] += 1
  col_bomb[w-1] += 1
  
r_max = max(row_bomb)
c_max = max(col_bomb)
ans = r_max + c_max
rs = []
cs = []

for i in range(row):
  if row_bomb[i] == r_max:
    rs.append(i)
for i in range(col):
  if col_bomb[i] == c_max:
    cs.append(i)
    

if len(rs)*len(cs) <= 3*(10**5):
  ans -= 1
  judge = False
  
  for i in range(len(rs)):
    for j in range(len(cs)):
      if not [rs[i], cs[j]] in bomb:
        ans += 1
        judge = True
        break
    if judge:
      break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
import sys

h, w, m = map(int, input().split())
x = []
y = []
bomb = []
for i in range(m):
  tmp_x, tmp_y = map(int, input().split())
  x.append(tmp_x)
  y.append(tmp_y)
  bomb.append([tmp_x, tmp_y])

c_x = collections.Counter(x)
c_y = collections.Counter(y)
_max_x = c_x.most_common()[0][1]
_max_y = c_y.most_common()[0][1]
ys = c_y.most_common()
count = _max_x + _max_y - 1
      
tansaku_x = []
tansaku_y = []
for x, x_num in c_x.most_common():
  if x_num != _max_x:
    break
  tansaku_x.append(x)
for y, y_num in c_y.most_common():
  if y_num != _max_y:
    break
  tansaku_y.append(y)

for x in tansaku_x:
  for y in tansaku_y:
    if [x, y] in bomb:
      continue
    else:
      print(count+1)
      sys.exit()
      
print(count)