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 math, sys

H,W,M=list(map(int, input().split()))
Bs=[]
#Map=[ [False for _ in range(W+1)] for _ in range(H+1)]

for _ in range(M):
    bh,bw=list(map(int, input().split()))
    #Map[bh][bw]=True
    Bs.append((bh,bw))
Hs=[0 for _ in range(H+1)]
Ws=[0 for _ in range(W+1)]

for each in Bs:
    hh,ww=each
    Hs[hh]+=1
    Ws[ww]+=1

hmax=0
wmax=0
for each in Hs:
    if each >hmax:
        hmax=each
for each in Ws:
    if each>wmax:
        wmax=each
hmax_list=[]
wmax_list=[]
for i in range(1,H+1):
    if Hs[i]==hmax:
        hmax_list.append(i)
for i in range(1,W+1):
    if Ws[i]==wmax:
        wmax_list.append(i)
ans=hmax+wmax
for hi in hmax_list:
    for wi in wmax_list:
        if (hi,wi) not in Bs:#not Map[hi][wi]:
            print(ans)
            sys.exit()
print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
import math, sys

H,W,M=list(map(int, input().split()))
Bs=[]
Map=[ [False for _ in range(W+1)] for _ in range(H+1)]
for _ in range(M):
    bh,bw=list(map(int, input().split()))
    Map[bh][bw]=True
    Bs.append((bh,bw))
Hs=[0 for _ in range(H+1)]
Ws=[0 for _ in range(W+1)]

for each in Bs:
    hh,ww=each
    Hs[hh]+=1
    Ws[ww]+=1

hmax=0
wmax=0
for each in Hs:
    if each >hmax:
        hmax=each
for each in Ws:
    if each>wmax:
        wmax=each
hmax_list=[]
wmax_list=[]
for i in range(1,H+1):
    if Hs[i]==hmax:
        hmax_list.append(i)
for i in range(1,W+1):
    if Ws[i]==wmax:
        wmax_list.append(i)
ans=hmax+wmax
for hi in hmax_list:
    for wi in wmax_list:
        if not Map[hi][wi]:
            print(ans)
            sys.exit()
print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
bord_x, bord_y = [0 for i in range(h)], [0 for i in range(w)]
bord = [[0 for _j in range(w)] for _i in range(h)]

hw = []
for _i in range(m):
    s, t = map(int, input().split())
    s -= 1
    t -= 1
    bord_x[s] += 1
    bord_y[t] += 1
    hw.append([s, t])

x_max, y_max = max(bord_x), max(bord_y)

cnt = 0
for i, j in hw:
    if bord_x[i] == x_max and bord_y[j] == y_max:
        cnt += 1

if cnt == bord_x.count(x_max)*bord_y.count(y_max):
    x_max -= 1

print(x_max+y_max)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
bord_x, bord_y = [0 for i in range(h)], [0 for i in range(w)]
bord = [[0 for _j in range(w)] for _i in range(h)]

for _i in range(m):
    s, t = map(int, input().split())
    s -= 1
    t -= 1
    bord_x[s] += 1
    bord_y[t] += 1
    bord[s][t] = 1

x_max, y_max = max(bord_x), max(bord_y)
a, b = [], []
for i in range(h):
    if bord_x[i] == x_max:
        a.append(i)
for i in range(w):
    if bord_y[i] == y_max:
        b.append(i)

checker = 0
for i in a:
    for j in b:
        if bord[i][j]==0:
            import sys
            print(x_max+y_max)
            sys.exit()
        else:
            checker += 1
            if checker >= m:
                break

print(x_max+y_max-1)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
bord_x, bord_y = [0 for i in range(h)], [0 for i in range(w)]
bord = [[0 for _j in range(w)] for _i in range(h)]

for _i in range(m):
    s, t = map(int, input().split())
    s -= 1
    t -= 1
    bord_x[s] += 1
    bord_y[t] += 1
    bord[s][t] = 1

x_max, y_max = max(bord_x), max(bord_y)
a = [x for x, p in enumerate(bord_x) if x_max==p]
b = [y for y, p in enumerate(bord_y) if y_max==p]

checker = 0
for i in a:
    for j in b:
        if bord[i][j]==0:
            import sys
            print(x_max+y_max)
            sys.exit()
        else:
            checker += 1
            if checker >= m:
                break

print(x_max+y_max-1)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
bord_x, bord_y = [0 for i in range(h)], [0 for i in range(w)]
bord = [[] for _i in range(h)]

for _i in range(m):
    s, t = map(int, input().split())
    s -= 1
    t -= 1
    bord_x[s] += 1
    bord_y[t] += 1
    bord[s].append(t)

x_max, y_max = max(bord_x), max(bord_y)
a = [x for x, p in enumerate(bord_x) if x_max==p]
b = [y for y, p in enumerate(bord_y) if y_max==p]

checker = 0
for i in a:
    for j in b:
        if not(j in bord[i]):
            import sys
            print(x_max+y_max)
            sys.exit()
        else:
            checker += 1
            if checker >= m:
                break

print(x_max+y_max-1)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
bord_x, bord_y = [0 for i in range(h)], [0 for i in range(w)]
bord = [[] for _i in range(h)]

for _i in range(m):
    s, t = map(int, input().split())
    s -= 1
    t -= 1
    bord_x[s] += 1
    bord_y[t] += 1
    bord[s].append(t)

x_max, y_max = max(bord_x), max(bord_y)
a = [x for x, p in enumerate(bord_x) if x_max==p]
b = [y for y, p in enumerate(bord_y) if y_max==p]

checker = 0
for i in a:
    for j in b:
        if not(j in bord[i]):
            import sys
            print(x_max+y_max)
            sys.exit()
        else:
            checker += 1
        if checker >= m:
            break

print(x_max+y_max-1)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
bord_x, bord_y = [0 for i in range(h)], [0 for i in range(w)]
bord = [[0 for _i in range(w)] for _j in range(h)]
hw = []
for _i in range(m):
    s, t = map(int, input().split())
    s -= 1
    t -= 1
    hw.append([s, t])
    bord_x[s] += 1
    bord_y[t] += 1

x_max, y_max = max(bord_x), max(bord_y)
a = [x for x, p in enumerate(bord_x) if x_max==p]
b = [y for y, p in enumerate(bord_y) if y_max==p]

checker = 0
for i, j in hw:
    if bord_x[i]==x_max and bord_y[j]==y_max:
        checker += 1 

if checker == len(a)*len(b):
    print(x_max+y_max-1)
else:
    print(x_max+y_max)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
bord_x, bord_y = [0 for i in range(h)], [0 for i in range(w)]
bord = [[0 for _i in range(w)] for _j in range(h)]
hw = []
for _i in range(m):
    s, t = map(int, input().split())
    s -= 1
    t -= 1
    hw.append([s, t])
    bord_x[s] += 1
    bord_y[t] += 1

x_max, y_max = max(bord_x), max(bord_y)
a = [x for x, p in enumerate(bord_x) if x_max==p]
b = [y for y, p in enumerate(bord_y) if y_max==p]

checker = 0
for i, j in hw:
    if (i in a) and (j in b):
        checker += 1 

if checker == len(a)*len(b):
    print(x_max+y_max-1)
else:
    print(x_max+y_max)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
bord_x, bord_y = [0 for i in range(h)], [0 for i in range(w)]
bord = [[0 for _i in range(w)] for _j in range(h)]

for _i in range(m):
    s, t = map(int, input().split())
    s -= 1
    t -= 1
    bord[s][t] += 1
    bord_x[s] += 1
    bord_y[t] += 1

x_max, y_max = max(bord_x), max(bord_y)
a = [x for x, p in enumerate(bord_x) if x_max==p]
b = [y for y, p in enumerate(bord_y) if y_max==p]

r = x_max + y_max - 1
for i in a:
    for j in b:
        if bord[i][j] == 0:
            r += 1
            import sys
            print(r)
            sys.exit()
print(r)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int, input().split())
bord_x, bord_y = [0 for i in range(h)], [0 for i in range(w)]
bord = [[0 for _i in range(w)] for _j in range(h)]

hw = [list(map(int, input().split())) for _i in range(m)]

for s, t in hw:
    s -= 1
    t -= 1
    bord[s][t] += 1
    bord_x[s] += 1
    bord_y[t] += 1

x_max, y_max = max(bord_x), max(bord_y)
a = [x for x, p in enumerate(bord_x) if x_max==p]
b = [y for y, p in enumerate(bord_y) if y_max==p]

r = x_max + y_max - 1
for i in a:
    for j in b:
        if bord[i][j] >= 1:
            continue
        else:
            r += 1
            import sys
            print(r)
            sys.exit()
print(r)

Submission status: Time Limit Exceeded

Solution code:

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

h_b_list = [0 for i in range(h)]
w_b_list = [0 for i in range(w)]

targets = []
for i in range(m):
  bh, bw = map(int, input().split())
  targets.append([bh, bw])
  h_b_list[bh-1] += 1
  w_b_list[bw-1] += 1
  
h_max = max(h_b_list)
w_max = max(w_b_list)

h_cans = []
w_cans = []
for i in range(h):
  if h_b_list[i] == h_max:
    h_cans.append(i)
    
for i in range(w):
  if w_b_list[i] == w_max:
    w_cans.append(i)

for hi in h_cans:
  for wi in w_cans:
    if [hi+1, wi+1] not in targets:
      print(h_max+w_max)
      exit()
else:
  print(h_max+w_max-1)

Submission status: Time Limit Exceeded

Solution code:

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

hw_map = [[0 for i in range(w)] for j in range(h)]
h_bomb_list = [0 for i in range(h)]
w_bomb_list = [0 for i in range(w)]

for i in range(m):
  bh, bw = map(int, input().split())
  hw_map[bh-1][bw-1] = 1
  h_bomb_list[bh-1] += 1
  w_bomb_list[bw-1] += 1
  

h_max = max(h_bomb_list)
h_argmax = max(zip(h_bomb_list, range(len(h_bomb_list))))[1]
w_max = max([w_bomb_list[i] - hw_map[h_argmax][i] for i in range(w)])
print(int(h_max+w_max))

Submission status: Time Limit Exceeded

Solution code:
# こんがらがりがちな貪欲？

h, w, m = map(int, input().split())
hs, ws = {}, {}
h_max, w_max = 0, 0
data = {}
for _ in range(m):
    hv, wv = map(int, input().split())
    data[(hv, wv)] = True
    if hv in hs:
        hs[hv] += 1
    else:
        hs[hv] = 1
    if wv in ws:
        ws[wv] += 1
    else:
        ws[wv] = 1
    h_max = max(h_max, hs[hv])
    w_max = max(w_max, ws[wv])
hss = sorted(hs.items(), key=lambda x: x[1], reverse=True)
wss = sorted(ws.items(), key=lambda x: x[1], reverse=True)
hi, wi = [], []
for hssv in hss:
    if hssv[1] == h_max:
        hi.append(hssv[0])
    else:
        break
for wssv in wss:
    if wssv[1] == w_max:
        wi.append(wssv[0])
    else:
        break
# hi = list(filter(lambda kv: kv[1] == h_max, hs.items()))
# wi = list(filter(lambda kv: kv[1] == w_max, ws.items()))

ans = 0
for wv in wi:
    for k, v in hss:
        tmp = w_max + v
        if (k, wv) in data:
            tmp -= 1
        ans = max(ans, tmp)
for hv in hi:
    for k, v in wss:
        tmp = h_max + v
        if (hv, k) in data:
            tmp -= 1
        ans = max(ans, tmp)
print(ans)

# oooxo
# oxxox
# oooox
# ooxoo
# xoxxx

Submission status: Time Limit Exceeded

Solution code:
# こんがらがりがちな貪欲？

h, w, m = map(int, input().split())
hs, ws = {}, {}
h_max, w_max = 0, 0
data = {}
for _ in range(m):
    hv, wv = map(int, input().split())
    data[(hv, wv)] = True
    if hv in hs:
        hs[hv] += 1
    else:
        hs[hv] = 1
    if wv in ws:
        ws[wv] += 1
    else:
        ws[wv] = 1
    h_max = max(h_max, hs[hv])
    w_max = max(w_max, ws[wv])
hss = sorted(hs.items(), key=lambda x: x[1], reverse=True)
wss = sorted(ws.items(), key=lambda x: x[1], reverse=True)
hi, wi = [], []
for hssv in hss:
    if hssv[1] == h_max:
        hi.append(hssv[0])
    else:
        break
for wssv in wss:
    if wssv[1] == w_max:
        wi.append(wssv[0])
    else:
        break
# hi = list(filter(lambda kv: kv[1] == h_max, hs.items()))
# wi = list(filter(lambda kv: kv[1] == w_max, ws.items()))

ans = 0
for wv in wi:
    for k, v in hs.items():
        tmp = w_max + v
        if (k, wv) in data:
            tmp -= 1
        ans = max(ans, tmp)
for hv in hi:
    for k, v in ws.items():
        tmp = h_max + v
        if (hv, k) in data:
            tmp -= 1
        ans = max(ans, tmp)
print(ans)

# oooxo
# oxxox
# oooox
# ooxoo
# xoxxx

Submission status: Time Limit Exceeded

Solution code:
# こんがらがりがちな貪欲？

h, w, m = map(int, input().split())
hs, ws = {}, {}
h_max, w_max = 0, 0
data = {}
for _ in range(m):
    hv, wv = map(int, input().split())
    data[(hv, wv)] = True
    if hv in hs:
        hs[hv] += 1
    else:
        hs[hv] = 1
    if wv in ws:
        ws[wv] += 1
    else:
        ws[wv] = 1
    h_max = max(h_max, hs[hv])
    w_max = max(w_max, ws[wv])

hi = list(filter(lambda kv: kv[1] == h_max, hs.items()))
wi = list(filter(lambda kv: kv[1] == w_max, ws.items()))

ans = 0
for wv in wi:
    wv = wv[0]
    for k, v in hs.items():
        tmp = w_max + v
        if (k, wv) in data:
            tmp -= 1
        ans = max(ans, tmp)
for hv in hi:
    hv = hv[0]
    for k, v in ws.items():
        tmp = h_max + v
        if (hv, k) in data:
            tmp -= 1
        ans = max(ans, tmp)
print(ans)

# oooxo
# oxxox
# oooox
# ooxoo
# xoxxx

Submission status: Time Limit Exceeded

Solution code:
# こんがらがりがちな貪欲？

h, w, m = map(int, input().split())
hs, ws = {}, {}
h_max, w_max = 0, 0
data = {}
for _ in range(m):
    hv, wv = map(int, input().split())
    data[(hv, wv)] = True
    if hv in hs:
        hs[hv] += 1
    else:
        hs[hv] = 1
    if wv in ws:
        ws[wv] += 1
    else:
        ws[wv] = 1
    h_max = max(h_max, hs[hv])
    w_max = max(w_max, ws[wv])

hi = list(
    map(lambda v: v[0], list(filter(lambda kv: kv[1] == h_max, hs.items()))))
wi = list(
    map(lambda v: v[0], list(filter(lambda kv: kv[1] == w_max, ws.items()))))

ans = 0
for wv in wi:
    for k, v in hs.items():
        tmp = w_max + v
        if (k, wv) in data:
            tmp -= 1
        ans = max(ans, tmp)
for hv in hi:
    for k, v in ws.items():
        tmp = h_max + v
        if (hv, k) in data:
            tmp -= 1
        ans = max(ans, tmp)
print(ans)

# oooxo
# oxxox
# oooox
# ooxoo
# xoxxx

Submission status: Time Limit Exceeded

Solution code:
#E - Bomber
target     = []
hsetting   = []
wsetting   = []
hcount = 0
wcount = 0
xcount = 1
append = target.append
happend =hsetting.append
wappend =wsetting.append
# 入力
H,W,M = map(int,input().split())
for hwinput in range(M):
    h,w = map(int,input().split())
    append([h,w])
    happend(h)
    wappend(w)
#h_target = sorted(hsetting)
#w_target = sorted(wsetting)

max_bomb_out = 0
w_max_bomb_out = 0
h_max_bomb_out = 0
h_index_count = 0
w_index_count = 0
h_index = []
w_index = []

targetcount = target.count
hsetcount = hsetting.count
wsetcount = wsetting.count
h_index_ap =  h_index.append
h_index_cl =  h_index.clear
w_index_ap =  w_index.append
w_index_cl =  w_index.clear
h_index_index = h_index.index
w_index_index = w_index.index
h_index_p =[]
w_index_p =[]
h_index_p_ap =  h_index_p.append
w_index_p_ap =  w_index_p.append
flag = 0

for i in range(1,H+1):
    hcount = hsetcount(i)
    if h_max_bomb_out  <= hcount:
        if h_max_bomb_out  != hcount:
            h_index_cl()
            h_index_count = 0            
        h_index_ap(i)
        h_index_count += 1
        h_max_bomb_out = hcount

for j in range(1,W+1):
    wcount = wsetcount(j)
    if w_max_bomb_out  <= wcount:
        if w_max_bomb_out  != wcount:
            w_index_cl()
            w_index_count = 0            
        w_index_ap(j)
        w_index_count += 1
        w_max_bomb_out = wcount

flagcount = h_index_count * w_index_count
for k in range(h_index_count):
    for l in range(w_index_count):
        for m in range(M):
            if target[m] == [h_index[k],w_index[l]]:
                flag += 1
if flag != flagcount:
    xcount = 0
max_bomb_out = h_max_bomb_out + w_max_bomb_out - xcount

# 出力
print(max_bomb_out)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

hc = [0 for h in range(H)]
wc = [0 for w in range(W)]

for _ in range(M):
    h,w = map(int, input().split())
    h-=1
    w-=1
    grid[h][w] = 1
    hc[h] += 1
    wc[w] += 1

hmax = max(hc)
wmax = max(wc)

htable = []
for h in range(H):
    if hc[h] == hmax:
        htable.append(h)

wtable = []
for w in range(W):
    if wc[w] == wmax:
        wtable.append(w)

# print(uw, uh)
#print(grid)
#print(wc, uw)
#print(hc, uh)

found = False
for h in htable:
    for w in wtable:
        if grid[h][w] == 0:
            print(hmax + wmax)
            sys.exit(0)

print(hmax + wmax - 1)

Submission status: Time Limit Exceeded

Solution code:
#E - Bomber
target     = []
hsetting   = []
wsetting   = []
hcount = 0
wcount = 0
xcount = 1
append = target.append
happend =hsetting.append
wappend =wsetting.append
# 入力
H,W,M = map(int,input().split())
for hwinput in range(M):
    h,w = map(int,input().split())
    append([h,w])
    happend(h)
    wappend(w)

max_bomb_out = 0
w_max_bomb_out = 0
h_max_bomb_out = 0
h_index_count = 0
w_index_count = 0
h_index = []
w_index = []

targetcount = target.count
hsetcount = hsetting.count
wsetcount = wsetting.count
h_index_ap =  h_index.append
h_index_cl =  h_index.clear
w_index_ap =  w_index.append
w_index_cl =  w_index.clear
for i in range(1,H+1):
    hcount = hsetcount(i)
    if h_max_bomb_out  <= hcount:
        if h_max_bomb_out  != hcount:
            h_index_cl()
            h_index_count = 0
        h_index_ap(i)
        h_index_count += 1
        h_max_bomb_out = hcount

for j in range(1,W+1):
    wcount = wsetcount(j)
    if w_max_bomb_out  <= wcount:
        if w_max_bomb_out  != wcount:
            w_index_cl()
            w_index_count = 0
        w_index_ap(j)
        w_index_count += 1
        w_max_bomb_out = wcount

for k in range(h_index_count):
    for l in range(w_index_count):
        xcount_flag = targetcount([h_index[k],w_index[l]])
        if xcount_flag == 0:
            xcount = 0
            break

max_bomb_out = h_max_bomb_out + w_max_bomb_out - xcount

# 出力
print(max_bomb_out)

Submission status: Time Limit Exceeded

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

hc = [(0, h) for h in range(H)]
wc = [(0, w) for w in range(W)]

for _ in range(M):
    h,w = map(int, input().split())
    h-=1
    w-=1
    grid[h][w] = 1
    hc[h] = (hc[h][0] + 1, hc[h][1])
    wc[w] = (wc[w][0] + 1, wc[w][1])

hc = sorted(hc, key=lambda p: p[0], reverse=True)
wc = sorted(wc, key=lambda p: p[0], reverse=True)

hmax = hc[0][0]
wmax = wc[0][0]

uh = 0
while uh < H and hc[uh][0] == hmax:
    uh += 1

uw = 0
while uw < W and wc[uw][0] == wmax:
    uw += 1

# print(uw, uh)
#print(grid)
#print(wc, uw)
#print(hc, uh)

found = False
for h in range(uh):
    for w in range(uw):
        if grid[hc[h][1]][wc[w][1]] == 0:
            found = True
            break
    if found:
        break

print(hmax + wmax if found else hmax + wmax - 1)

Submission status: Time Limit Exceeded

Solution code:
h,w,m=map(int,input().split())
smat=[list(map(int,input().split())) for i in range(m)]
hb=[d[0] for d in smat]
wb=[d[1] for d in smat]
cmax=0
for i in range(h):
    for j in range(w):
        c=hb.count(i+1)+wb.count(j+1)
        if [i+1,j+1] in smat:
            c=c-1
        if cmax<c:
            cmax=c
print(cmax)

Submission status: Time Limit Exceeded

Solution code:
H,W,M = map(int, input().split())
mat = [[0 for i in range(W)] for j in range(H)]
width = [0]*W
height = [0]*H
for i in range(M):
  x,y = map(int, input().split())
  width[y-1] += 1
  height[x-1] += 1
  mat[x-1][y-1] = 1
 
indexW = [i for i, v in enumerate(width) if v == max(width)]
indexH = [i for i, v in enumerate(height) if v == max(height)]
notHere = False
for i in indexW:
  for j in indexH:
    if mat[j][i] == 0:
      notHere = True
      break
 
ans = max(width) + max(height)
 
if notHere == False:
  ans -= 1
 
print(ans)

Submission status: Time Limit Exceeded

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

r_n = [0] * h
c_n = [0] * w
bomb_table = set()
for i in range(k):
    hi, wi = map(int, input().split())
    hi -= 1
    wi -= 1
    r_n[hi] += 1
    c_n[wi] += 1
    bomb_table.add((hi, wi))

max_x = max(c_n)
max_y = max(r_n)
ans = max_x + max_y

for i in range(h):
    if r_n[i] != max_y:
        continue
    for j in range(w):
        if c_n[j] != max_x:
            continue
        if (i, j) not in bomb_table:
            print(ans)
            exit(0)

print(ans - 1)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

gyou = collections.defaultdict(int)
retu = collections.defaultdict(int)
q = collections.deque()

for i in range(m):
    y,x = map(int,input().split())
    gyou[y-1] += 1
    retu[x-1] += 1
    q.append((y-1,x-1))

ymax = max(gyou.values())
xmax = max(retu.values())
ans =  ymax + xmax 

for xk,xv in retu.items():
    if xv != xmax:
        continue
    for yk, yv in gyou.items():
        if yv != ymax:
            continue
        if (yk,xk) not in q:
            print(ans)
            exit()

print(ans-1)

Submission status: Time Limit Exceeded

Solution code:

import collections

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

gyou = collections.defaultdict(int)
retu = collections.defaultdict(int)
maze = [[0 for i in range(w)] for j in range(h)]

for i in range(m):
    y,x = map(int,input().split())
    gyou[y-1] += 1
    retu[x-1] += 1
    maze[y-1][x-1] = 1

ymax = max(gyou.values())
xmax = max(retu.values())
ans =  ymax + xmax 

for xk,xv in retu.items():
    if xv != xmax:
        continue
    for yk, yv in gyou.items():
        if yv != ymax:
            continue
        if maze[yk][xk] != 1:
            print(ans)
            exit()

print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
#E - Bomber
target     = []
hsetting   = []
wsetting   = []
hcount = 0
wcount = 0
xcount = 1
# 入力
H,W,M = map(int,input().split())
for hwinput in range(M):
    h,w = map(int,input().split())
    target.append([h,w])
#    hsetting.append(h)
#    wsetting.append(w)

max_bomb_out = 0
w_max_bomb_out = 0
h_max_bomb_out = 0
h_index_count = 0
w_index_count = 0
h_index = []
w_index = []
for i in range(1,H+1):
    hcount = 0
    for icount in range(M):
        if target[icount][0] == i:
            hcount += 1
#        print(target[icount][0],hcount)
#    hcount = hsetting.count(i)
    if h_max_bomb_out  <= hcount:
        if h_max_bomb_out  != hcount:
            h_index.clear()
            h_index.append(i)
        else:
            h_index.append(i)
        h_max_bomb_out = hcount

for j in range(1,W+1):
    wcount = 0
    for jcount in range(M):
        if target[jcount][1] == j:
            wcount += 1

    if w_max_bomb_out  <= wcount:
        if w_max_bomb_out  != wcount:
            w_index.clear()
            w_index.append(j)
        else:
            w_index.append(j)
        w_max_bomb_out = wcount

h_index_count = len(h_index)
w_index_count = len(w_index)
for k in range(h_index_count):
    for l in range(w_index_count):
        xcount_flag = target.count([h_index[k],w_index[l]])
        if xcount_flag == 0:
            xcount = 0
            break

max_bomb_out = h_max_bomb_out + w_max_bomb_out - xcount

# 出力
print(max_bomb_out)

Submission status: Time Limit Exceeded

Solution code:
#E - Bomber
target     = []
hsetting   = []
wsetting   = []
hcount = 0
wcount = 0
xcount = 1
# 入力
H,W,M = map(int,input().split())
for hwinput in range(M):
    h,w = map(int,input().split())
    target.append([h,w])
    hsetting.append(h)
    wsetting.append(w)

max_bomb_out = 0
w_max_bomb_out = 0
h_max_bomb_out = 0
h_index_count = 0
w_index_count = 0
h_index = []
w_index = []
for i in range(1,H+1):
    hcount = hsetting.count(i)
    if h_max_bomb_out  <= hcount:
        if h_max_bomb_out  != hcount:
            h_index.clear()
            h_index.append(i)
        else:
            h_index.append(i)
        h_max_bomb_out = hcount

for j in range(1,W+1):
    wcount = wsetting.count(j)
    if w_max_bomb_out  <= wcount:
        if w_max_bomb_out  != wcount:
            w_index.clear()
            w_index.append(j)
        else:
            w_index.append(j)
        w_max_bomb_out = wcount

h_index_count = len(h_index)
w_index_count = len(w_index)
for k in range(h_index_count):
    for l in range(w_index_count):
        xcount_flag = target.count([h_index[k],w_index[l]])
        if xcount_flag == 0:
            xcount = 0
            break

max_bomb_out = h_max_bomb_out + w_max_bomb_out - xcount

# 出力
print(max_bomb_out)

Submission status: Time Limit Exceeded

Solution code:
#E - Bomber
target     = []
bomb_set   = []
hsetting   = []
wsetting   = []
hcount = 0
wcount = 0
xcount = 0
# 入力
H,W,M = map(int,input().split())
for hwinput in range(M):
    h,w = map(int,input().split())
    target.append([h,w])
    hsetting.append(h)
    wsetting.append(w)

max_bomb_out = 0

for i in range(1,H+1):
    for j in range(1,W+1):
        hcount = hsetting.count(i)
        wcount = wsetting.count(j)
        xcount = target.count([i,j])      
        bomb_out = hcount + wcount - xcount
        if max_bomb_out < bomb_out:
            max_bomb_out = bomb_out
# 出力
print(max_bomb_out)

Submission status: Time Limit Exceeded

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

visited = set()

h_sum = [0] * H
w_sum = [0] * W

for i in range(M):
    h, w = map(int, input().split())
    h -= 1
    w -= 1
    visited.add((h, w))
    h_sum[h] += 1
    w_sum[w] += 1

max_h = max_w = 0
can_h = []
can_w = []
for i in range(H):
    max_h = max(max_h, h_sum[i])
    
for i in range(W):
    max_w = max(max_w, w_sum[i])

for i in range(H):
    if h_sum[i] == max_h:
        can_h.append(i)
for i in range(W):
    if w_sum[i] == max_w:
        can_w.append(i)
        
ans = 0

for i in can_h:
    for j in can_w:
        if (i, j) in visited:
            ans = max(ans, max_h+max_w-1)
        else:
            ans = max(ans, max_h+max_w)
print(ans)

Submission status: Time Limit Exceeded

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

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

row = [0]*h
col = [0]*w
enemy = []

for i in range(m):
    hh, ww = map(lambda x:int(x)-1,input().split())
    row[hh] += 1
    col[ww] += 1
    enemy.append((hh,ww))

enemy = set(enemy)
rmax = max(row)
cmax = max(col)

rind = [i for i,v in enumerate(row) if v == rmax]
cind = [i for i,v in enumerate(col) if v == cmax]

ans = 0
for r in rind:
    for c in cind:
        if (r, c) in enemy:
            ans = max(ans, rmax+cmax-1)
        else:
            ans = max(ans, rmax+cmax)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int,input().split())
maze = [[0 for _ in range(w)] for _ in range(h)]
for i in range(m):
    hh, ww = map(lambda x:int(x)-1,input().split())
    maze[hh][ww] = 1

#print(maze)

#yoko
row = [0]*h
for r in range(h):
    row[r] = sum(maze[r])
#tate
col = [0]*w
for c in range(w):
    tmp = 0
    for r in range(h):
        tmp += maze[r][c]
    col[c] = tmp

rmax = max(row)
cmax = max(col)

rind = [n for n, v in enumerate(row) if v == rmax]
cind = [n for n, v in enumerate(col) if v == cmax]

#print(rind)
#print(cind)
ans = 0

for r in rind:
    for c in cind:
        if maze[r][c] == 1:
            ans = rmax+cmax-1
        else:
            ans = rmax+cmax
            print(ans)
            exit()
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w, m = map(int,input().split())
maze = [[0 for _ in range(w)] for _ in range(h)]
for i in range(m):
    hh, ww = map(lambda x:int(x)-1,input().split())
    maze[hh][ww] = 1

#yoko
row = [0]*h
for r in range(h):
    row[r] = sum(maze[r])
#tate
col = [0]*w
for c in range(w):
    tmp = 0
    for r in range(h):
        tmp += maze[r][c]
    col[c] = tmp

rmax = max(row)
cmax = max(col)
ans = 0
for i,r in enumerate(row):
    if r == rmax:
        for j,c in enumerate(col):
            if c == cmax:
                if maze[i][j] == 1:
                    ans = max(ans, rmax+cmax-1)
                else:
                    ans = max(ans,rmax+cmax)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

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

d1 = defaultdict(int)
d2 = defaultdict(int)

b = [[False] * (W+1) for _ in range(H+1)]

for h, w in hw:
    d1[h] += 1
    d2[w] += 1
    b[h][w] = True

m1 = max(d1.values())
m2 = max(d2.values())

e1 = [k for k in d1.keys() if d1[k] == m1]
e2 = [k for k in d2.keys() if d2[k] == m2]

flag = True

for x in e1:
    for y in e2:
        if not b[x][y]:
            flag = False
            break
    if not flag:
        break

if flag:
    print(m1+m2-1)
else:
    print(m1+m2)

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
import bisect
from collections import deque
from copy import copy, deepcopy
import time

#import numpy as np
#import numba
#from numba import njit, b1, i4, i8, f8
#from numba import jit


def main():
    H,W,M = map(int,input().split())
    h={}
    w={}
    bomb_list=[]
    for i in range(M):
        ih,iw = map(int,input().split())
        bomb_list.append([ih,iw])
        if ih not in h:
            h[ih]=1
        else:
            h[ih]+=1
        if iw not in w:
            w[iw]=1
        else:
            w[iw]+=1

    max_h = [k for k, v in h.items() if v == max(h.values())]
    max_w = [k for k, v in w.items() if v == max(w.values())]
    for mh in max_h:
        for mw in max_w:
            if [mh,mw] not in bomb_list:
                print(max(h.values())+max(w.values()))
                return
    
    print(max(h.values())+max(w.values())-1)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

def resolve(in_):
    H, W, M = map(int, next(in_).split())
    targets = tuple(tuple(map(int, line.split())) for line in in_)
    # H, W, M = 3, 3, 3
    # targets = ((1, 1), (2, 1), (1, 3))
    
    target_h = {}
    target_w = {}
    target_hw = set()
    for h, w in targets:
        target_h[h - 1] = target_h.setdefault(h - 1, 0) + 1
        target_w[w - 1] = target_w.setdefault(w - 1, 0) + 1
        target_hw.add((h - 1, w - 1))

    ans = 0
    for h, a in target_h.items():
        for w, b in target_w.items():
            ans = max(ans, target_h[h] + target_w[w] - (1 if (h, w) in target_hw else 0))
    
    return ans

def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()