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: Wrong Answer

Solution code:
import numpy
h,w,m=map(int,input().split())
h_arr=[0]*h
w_arr=[0]*w
record = set()
for i in range(m):
    tmp_h, tmp_w = map(int,input().split())
    record.add((tmp_h,tmp_w))
    h_arr[tmp_h-1] += 1
    w_arr[tmp_w-1] += 1
#最大の列と行の交差点に爆破対象があるかどうかがわからない?
max_h = max(h_arr)
max_w = max(w_arr)
max_h_indexes = [i for i, x in enumerate(h_arr) if x == max_h]
max_w_indexes = [i for i, x in enumerate(w_arr) if x == max_w]
ans = max_h + max_w -1
#たぶんindexesが多いときにTLEになってるんじゃないか
if not(max_h == h or max_w == w):
    for i in max_h_indexes:
        for j in max_w_indexes:
            if not((i+1,j+1) in record):
                ans += 1
                break
            else:
                continue
        break
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import random
h,w,m = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(m)]
exist = set()
tate = [[0,i] for i in range(h)]
yoko = [[0,i] for i in range(w)]

for y,x in lis:
    exist.add(format(y).zfill(6)+format(x).zfill(6))
    tate[y-1][0] += 1
    yoko[x-1][0] += 1

tate.sort(key=lambda x:x[1])
yoko.sort(key=lambda x:x[1])
tate.sort(key=lambda x:x[0],reverse=True)
yoko.sort(key=lambda x:x[0],reverse=True)

ans = tate[0][0]+yoko[0][0]-1
if h == 1 or w == 1:
    print(m)
    exit()

  # print(tate)
  # print(yoko)

i = 0
while i < h and i <= 150:
    j = 0
    while j < w and j <= 150:
        ans = max(ans,tate[i][0]+yoko[j][0]-bool(format(tate[i][1]+1).zfill(6)+format(yoko[j][1]+1).zfill(6) in exist))
        j += 1
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
import sys
H, W, M= map(int,input().split())
height = [0]*H
width = [0]*W
object = []
for i in range(H):
   object.append(copy.deepcopy(width))
for s in sys.stdin.readlines():
    h, w = map(int, s.split())
    height[h-1] += 1
    width[w-1] += 1
    object[h-1][w-1] = 1
h_max = max(height)
w_max = max(width)
cand_w = []
cand_h = []
for i in range(W):
   if width[i] == w_max:
      cand_w.append(i)
for i in range(H):
   if height[i] == h_max:
      cand_h.append(i)
ans = h_max + w_max - 1
p = len(cand_h)*len(cand_w)
if M > p:
   for i in range(len(cand_h)):
      for j in range(len(cand_w)):
         if object[cand_h[i]][cand_w[j]] == 0:
            ans += 1
else:
   ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def main():
    H, W, M = map(int, input().split())
    bomb = set()
    yoko = [0] * W
    tate = [0] * H
    for _ in range(M):
        h, w = map(int, input().split())
        bomb.add((h-1, w-1))
        yoko[w - 1] += 1
        tate[h - 1] += 1

    idx_yoko = [(yoko[i], i) for i in range(W)]
    idx_tate = [(tate[i], i) for i in range(H)]
    idx_yoko.sort(reverse=True, key=lambda x: (x[0], x[1]))
    idx_tate.sort(reverse=True, key=lambda x: (x[0], x[1]))
    print(idx_tate)
    print(idx_yoko)
    ans = 0
    for p in range(min(5000, W)):
        for q in range(min(5000, H)):
            yoko_bomb, idx_w = idx_yoko[p]
            tate_bomb, idx_h = idx_tate[q]
            get_bomb = yoko_bomb + tate_bomb
            if (idx_h, idx_w) in bomb: get_bomb -= 1
            ans = max(ans, get_bomb)
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


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

h = [-1]
w = [-1]
hw_dict = defaultdict(int)
for _ in range(M):
    hi, wi = map(int, input().split())
    h.append(hi)
    w.append(wi)
    hw_dict[(hi, wi)] = 1
# print(h)
# print(w)
# print(hw)
h_cnt = [0] * (H + 1)
w_cnt = [0] * (W + 1)
for i in range(1, M + 1):
    h_cnt[h[i]] += 1
    w_cnt[w[i]] += 1
# print(h_cnt)
# print(w_cnt)

h_max = max(h_cnt)
w_max = max(w_cnt)

h_max_idx = []
w_max_idx = []


for i in range(1, H + 1):
    if h_cnt[i] == h_max:
        h_max_idx.append(i)

for i in range(1, W + 1):
    if w_cnt[i] == w_max:
        w_max_idx.append(i)

# print(h_max_idx)
# print(w_max_idx)

if len(h_max_idx)>=10**3:
    h_max_idx = h_max_idx[:10**3] + h_max_idx[-10:]

if len(w_max_idx)>=10**3:
    w_max_idx = w_max_idx[:10**3] + w_max_idx[-10:]

ans = 0

for i in h_max_idx:
    for j in w_max_idx:
        tmp = h_cnt[i] + w_cnt[j]
        if hw_dict[(i, j)] == 1:
            tmp -= 1
        ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
H,W,M = map(int,input().split())
listh = [0] * (H+1)
listw = [0] * (W+1)
s = set()
for i in range(M):
    hm,wm = map(int,input().split())
    listh[hm] += 1
    listw[wm] += 1
    s.add((hm,wm))
mh = max(listh)
mw = max(listw)
maxh = [i for i,v in enumerate(listh) if v == mh]
maxw = [i for i,v in enumerate(listw) if v == mw]

flag = True
for i in maxh:
    for j in maxw:
        if (i,j) not in s:
            flag = False
            print(mh + mw)
if flag:
    print(mh + mw - 1)

Submission status: Wrong Answer

Solution code:
h, w, m = map(int, input().split(' '))
grid = [[0 for j in range(w+1)] for i in range(h+1)]

h_max = 0
w_max = 0
h_max_grid = 0
w_max_grid = 0

for i in range(m):
    hi, wi = map(int, input().split(' '))
    grid[hi][wi] = 1
    grid[hi][0] += 1
    grid[0][wi] += 1

    if h_max < grid[hi][0]:
        h_max = grid[hi][0]
        h_max_grid = hi

    if w_max < grid[0][wi]:
        w_max = grid[0][wi]
        w_max_grid = wi

    t = h_max + w_max - grid[h_max_grid][w_max_grid]
    grid[0][0] = t if grid[0][0] < t else grid[0][0]

print(grid[0][0])

Submission status: Wrong Answer

Solution code:
def main():
    H, W, M_ko = map(int, input().split())

    zahyou = [[0 for i in range(W)] for j in range(H)]
    zahyou_H = [0 for i in range(H)]
    zahyou_W = [0 for i in range(W)]
    

    for i in range(M_ko):
        height, width = map(int, input().split())

        
        zahyou_H[height - 1] += 1
        zahyou_W[width - 1] += 1

    max_h = max(zahyou_H)
    max_w = max(zahyou_W)


    maxi = max_h + max_w


    print(maxi)
    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
HK,WK,M = map(int, input().split())
hw_s = [list(map(int, input().split())) for _ in range(M)]
W = {}
H = {}
HW = [[0 for i in range(WK+1)] for _ in range(HK+1)]
for i, hw in enumerate(hw_s):
    h=hw[0]
    w=hw[1]

    if not h in H:
        H[h] = 1
    else:
        H[h] += 1
    
    if not w in W:
        W[w] = 1
    else:
        W[w] += 1

    HW[h][w] = 1

answer = 0
HMAXES = []
WMAXES = []
HMAX = max(H.values())
WMAX = max(W.values())

answer = HMAX + WMAX

print(answer-1)

Submission status: Wrong Answer

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

h_cnt = [0 for _ in range(3*10**5 + 3)]

for h, w in hw:
    h_cnt[h] += 1

h_max = -1
h_idx = 999999999999

for i in range(len(h_cnt)):
    if h_cnt[i] > h_max:
        h_max = h_cnt[i]
        h_idx = i

ans = 0
L = []
for i in range(len(hw)):
    h, w = hw[i]
    if h == h_idx:
        ans += 1
        L.append(hw[i])

hw = list(set(hw) - set(L))

w_cnt = [0 for _ in range(3*10**5 + 3)]
for h, w in hw:
    w_cnt[w] += 1

w_max = -1
w_idx = 999999999999
for i in range(len(w_cnt)):
    if w_cnt[i] > w_max:
        w_max = w_cnt[i]
        w_idx = i
for h, w in hw:
    if w == w_idx:
        ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
import random
h,w,m = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(m)]
exist = set()
tate = [[0,i] for i in range(h)]
yoko = [[0,i] for i in range(w)]

for y,x in lis:
    exist.add(format(y).zfill(6)+format(x).zfill(6))
    tate[y-1][0] += 1
    yoko[x-1][0] += 1

tate.sort(key=lambda x:x[1])
yoko.sort(key=lambda x:x[1])
tate.sort(key=lambda x:x[0],reverse=True)
yoko.sort(key=lambda x:x[0],reverse=True)

ans = tate[0][0]+yoko[0][0]-1
if h == 1 or w == 1:
    print(m)
    exit()

  # print(tate)
  # print(yoko)

i = 0
j = 0
while i < h and i <= 300:
    while j < w and j <= 300:
        ans = max(ans,tate[i][0]+yoko[j][0]-bool(format(tate[i][1]+1).zfill(6)+format(yoko[j][1]+1).zfill(6) in exist))
        j += 1
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
h,w,m=(int(i) for i in input().split())
l1=[]
l2=[0]*h
l3=[0]*w
for i in range(m):
    x,y=(int(i) for i in input().split())
    l1.append((x,y))
    l2[x-1]+=1
    l3[y-1]+=1
m1=max(l2)
m2=max(l3)
m3=max(m1,m2)
if(m1>m2):
    l4=[0]*w
    k=l2.index(m1)
    for i in range(m):
        if(l1[i][0]!=k+1):
           l4[l1[i][1]-1]+=1
    k1=max(l4)
    print(m3+k1)
elif(m2<m1):
    l4=[0]*h
    k=l3.index(m2)
    for i in range(m):
        if(l1[i][1]!=k+1):
           l4[l1[i][0]-1]+=1
    k1=max(l4)
    print(m3+k1)
else:
    l4=[0]*w
    k=l2.index(m1)
    for i in range(m):
        if(l1[i][0]!=k+1):
           l4[l1[i][1]-1]+=1
    k1=max(l4)
    l4=[0]*h
    k=l3.index(m2)
    for i in range(m):
        if(l1[i][1]!=k+1):
           l4[l1[i][0]-1]+=1
    k1=max(k1,max(l4))
    print(m3+k1)

Submission status: Wrong Answer

Solution code:
from __future__ import print_function
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate, product, combinations_with_replacement
import sys
import bisect
import string
import math
import time


def I(): return int(input())
def S(): return input()
def MI(): return map(int, input().split())
def MS(): return map(str, input().split())
def LI(): return [int(i) for i in input().split()]
def LI_(): return [int(i)-1 for i in input().split()]
def StoI(): return [ord(i)-97 for i in input()]
def ItoS(nn): return chr(nn+97)
def input(): return sys.stdin.readline().rstrip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def print_matrix(mat):
    for i in range(len(mat)):
        print(*['IINF' if v == IINF else "{:0=4}".format(v) for v in mat[i]])


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
show_flg = False
# show_flg = True


def main():
    H, W, M = MI()
    h = [0] * M
    w = [0] * M
    H_N = defaultdict(int)
    W_N = defaultdict(int)

    for i in range(M):
        h[i], w[i] = MI()
        H_N[h[i]] += 1
        W_N[w[i]] += 1

    mkh = 0
    mvh = 0
    for k, v in H_N.items():
        if mvh < v:
            mvh = v
            mkh = k

    mkw = 0
    mvw = 0
    for k, v in W_N.items():
        if mvw < v:
            mvw = v
            mkw = k

    for i in range(M):
        if h[i] == mkh and w[i] == mkw:
            print(mvh + mvw - 1)
            return

    print(mvh + mvw)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

h_max=max(h_list)
w_max=max(w_list)
print(str(h_max+w_max))

Submission status: Wrong Answer

Solution code:
H, W, M = map(int, input().split())
H_max = [0 for _ in range(H+1)]
W_max = [0 for _ in range(W+1)]
A = []
for _ in range(M):
    i, k = map(int, input().split())
    A.append((i,k))
    H_max[i] += 1
    W_max[k] += 1

saidai_H = max(H_max)
possible_H = ([i for i, x in enumerate(H_max) if x == saidai_H])
saidai_W = max(W_max)
possible_W = ([i for i, x in enumerate(W_max) if x == saidai_W])
ans = True
for h in possible_H:
    for w in possible_W:
        if (h, w) in A:
            print(h,w)
            continue
        else:
            ans = False
            break
if ans:
    print(saidai_H+saidai_W-1)
else:
    print(saidai_H+saidai_W)

Submission status: Wrong Answer

Solution code:
h,w,m=map(int,input().split())
row=[0]*h
col=[0]*w
ma=0
for i in range(m):
    x,y=map(int,input().split())
    row[x-1]+=1
    col[y-1]+=1
    ma=max(ma,row[x-1]+col[y-1])
print(ma)

Submission status: Wrong Answer

Solution code:
import random
h,w,m = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(m)]
exist = set()
tate = [[0,i] for i in range(h)]
yoko = [[0,i] for i in range(w)]

for y,x in lis:
    exist.add(format(y).zfill(6)+format(x).zfill(6))
    tate[y-1][0] += 1
    yoko[x-1][0] += 1

tate.sort(key=lambda x:x[1])
yoko.sort(key=lambda x:x[1])
tate.sort(key=lambda x:x[0],reverse=True)
yoko.sort(key=lambda x:x[0],reverse=True)

ans = tate[0][0]+yoko[0][0]-1
if h == 1 or w == 1:
    print(m)
    exit()

  # print(tate)
  # print(yoko)

i = 0
j = 0
while i < h and i <= 200:
    while j < w and j <= 200:
        ans = max(ans,tate[i][0]+yoko[j][0]-bool(format(tate[i][1]+1).zfill(6)+format(yoko[j][1]+1).zfill(6) in exist))
        j += 1
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
H,W,M=map(int,input().split())
hw=[]
xcount=dict()
xmax=0
ans=0
for i in range(M):
    hw.append(list(map(int,input().split())))
    xcount.setdefault(hw[i][0],0)
    xcount[hw[i][0]]+=1
    if xcount[hw[i][0]]>xmax:
        xmax=xcount[hw[i][0]]
        xmaxindex=hw[i][0]
ans+=xcount[xmaxindex]
ycount=dict()
ycount_copy=dict()
ymax=0
for i in range(M):
    if hw[i][0]==xmaxindex:
        ycount_copy.setdefault(hw[i][1],0)
        ycount_copy[hw[i][1]]+=1
        continue
    ycount_copy.setdefault(hw[i][1],0)
    ycount_copy[hw[i][1]]+=1
    ycount.setdefault(hw[i][1],0)
    ycount[hw[i][1]]+=1
    if ycount[hw[i][1]]>ymax:
        ymax=ycount[hw[i][1]]
        ymaxindex=hw[i][1]
try:
    ans+=ycount[ymaxindex]
except:
    pass
print(ans)

Submission status: Wrong Answer

Solution code:
import collections

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

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

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

h_most = collections.Counter(h).most_common()[0]
w_most = collections.Counter(w).most_common()[0]

count = h_most[1] + w_most[1]


if (h_most[0] in h and w_most[0] in w):
    count = count - 1

print(count)

Submission status: Wrong Answer

Solution code:
from collections import Counter
inpl = lambda: list(map(int,input().split()))
H, W, M = inpl()
Shw = set()
Sh = []
Sw = []
for _ in range(M):
    h, w = inpl()
    h -= 1
    w -= 1
    Sh.append(h)
    Sw.append(w)
    Shw.add((h,w))

Gh = Counter(Sh).most_common()
Gw = Counter(Sw).most_common()
mh = Gh[0][1]
mw = Gw[0][1]
Lh = len(Gh)
Lw = len(Gw)
i = j = 0
while i < Lh and Gh[i][1] == mh:
    while j < Lw and Gw[j][1] == mw:
        if not (Gh[i][0],Gw[j][0]) in Shw:
            ans = Gh[0][1] + Gw[0][1]
            break
        j += 1
    else:
        i += 1
        continue
    break
else:
    ans = Gh[0][1] + Gw[0][1] - 1

print(ans)

Submission status: Wrong Answer

Solution code:
x=input().split()

m=int(x[2])
a=[]	
b=[]
for i in range(m):
	y=input().split()

	a.append(int(y[0]))
	b.append(int(y[1]))

a1={}
b1={}

for i in range(m):
	a1.update({a[i]:a.count(a[i])})
	b1.update({b[i]:b.count(b[i])})

amax=max(a1, key=a1.get)
bmax=max(b1, key=b1.get)

counter=0

for i in range(m):
	if amax==a[i]: counter+=1
	if bmax==b[i]: counter+=1
	if amax==a[i] and bmax==b[i]: counter-=1

print(counter)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from collections import Counter as C

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

hs, ws = [], []
hw = []

for i in range(m):
    hi, w = map(int, input().split())
    hs.append(hi)
    ws.append(w)
    hw.append([hi, w])


ch = C(hs).most_common()
cw = C(ws).most_common()
anss = []
pre_ans = -float('inf')
mrow = ch[0][0]
mcow = cw[0][0]

for num1, num2 in zip(ch, cw):
    row, cow = num1[0], num2[0]
    if mrow > row or mcow > cow:
        break
    ans1 = hs.count(row)
    ans2 = ws.count(cow)
    ans = ans1 + ans2

    if [row, cow] in hw:
        ans -= 1

    anss.append(ans)

    # if pre_ans >= ans:
    #    ans = pre_ans
    #    break
    # else:
    #    pre_ans = ans


print(max(anss))

Submission status: Wrong Answer

Solution code:
def resolve():
    H, W, M = map(int, input().split())
    pos = []
    for _ in range(M):
        pos.append(list(map(int, input().split())))

    h_totals = [0] * (H+1)
    w_totals = [0] * (W+1)
    overlap = [set() for _ in range(H+1)]

    for ph, pw in pos:
        h_totals[ph] += 1
        w_totals[pw] += 1
        overlap[ph].add(pw)

    max_h = -1
    max_w = -1
    h_list = []
    w_list = []
    for i in range(1, H+1):
        if h_totals[i] > max_h:
            max_h = h_totals[i]
            h_list = [i]
        elif h_totals[i] == max_h:
            h_list.append(i)
    for i in range(1, W+1):
        if w_totals[i] > max_w:
            max_w = w_totals[i]
            w_list = [i]
        elif w_totals[i] == max_w:
            w_list.append(i)

    ans = max_h + max_w - 1
    for i in h_list:
        for j in w_list:
            if j not in overlap[i]:
                ans += 1
                break
    print(ans)

if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
h,w,m=map(int,input().split())
objs=[tuple(map(int,input().split())) for i in range(m)]
setobj=set(objs)
xs,ys=zip(*objs)

from collections import Counter as co
ws=co(ys).most_common(50)
if len(ws)==1:ws.append((0,0))
#key value
hs=co(xs)
ans=1
for i in range(1,h+1):
    preans=0 if i not in hs else hs[i]
    for k,v in ws:
        ans=max(ans,preans+v-int((i,k) in setobj))
print(ans)

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
h_array = [0]*H
w_array = [0]*W
array = []
for _ in range(N):
    h, w = map(int, input().split())
    array.append([h,w])
    h_array[h-1] +=1
    w_array[w-1] +=1
max_h = max(h_array)
h_indexes = [i for i, x in enumerate(h_array) if x == max_h]
max_w = max(w_array)
w_indexes = [i for i, x in enumerate(w_array) if x == max_w]

count=0

for i in range(len(h_indexes)): 
  for j in range(len(w_indexes)):
    if [h_indexes[i]+1, w_indexes[j]+1] in array:
      count = max_h + max_w -1
    else:
      count = max_h + max_w
      break
print(count)

Submission status: Wrong Answer

Solution code:
import random
h,w,m = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(m)]
exist = set()
tate = [[0,i] for i in range(h)]
yoko = [[0,i] for i in range(w)]

for y,x in lis:
    exist.add(format(y).zfill(6)+format(x).zfill(6))
    tate[y-1][0] += 1
    yoko[x-1][0] += 1

tate.sort(key=lambda x:x[1])
yoko.sort(key=lambda x:x[1])
tate.sort(key=lambda x:x[0],reverse=True)
yoko.sort(key=lambda x:x[0],reverse=True)

ans = tate[0][0]+yoko[0][0]-1
if h == 1 or w == 1:
    print(m)
    exit()

  # print(tate)
  # print(yoko)

i = 0
j = 0
while i < h and i <= 150:
    while j < w and j <= 150:
        ans = max(ans,tate[i][0]+yoko[j][0]-bool(format(tate[i][1]+1).zfill(6)+format(yoko[j][1]+1).zfill(6) in exist))
        j += 1
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
import sys
H, W, M= map(int,input().split())
height = [0]*H
width = [0]*W
object = []
for i in range(H):
   object.append(copy.deepcopy(width))
for s in sys.stdin.readlines():
    h, w = map(int, s.split())
    height[h-1] += 1
    width[w-1] += 1
    object[h-1][w-1] = 1
h_max = max(height)
w_max = max(width)
cand_w = []
cand_h = []
for i in range(W):
   if width[i] == w_max:
      cand_w.append(i)
for i in range(H):
   if height[i] == h_max:
      cand_h.append(i)
ans = h_max + w_max - 1
p = len(cand_h)*len(cand_w)
if M > p:
   for i in range(len(cand_h)):
      for j in range(len(cand_w)):
         if object[cand_h[i]][cand_w[j]] == 0:
            ans += 1
else:
   ans += 1
print(ans)
print(object)

Submission status: Wrong Answer

Solution code:
import numpy
h,w,m=map(int,input().split())
h_arr=[0]*h
w_arr=[0]*w
record = set()
for i in range(m):
    tmp_h, tmp_w = map(int,input().split())
    record.add((tmp_h,tmp_w))
    h_arr[tmp_h-1] += 1
    w_arr[tmp_w-1] += 1
#最大の列と行の交差点に爆破対象があるかどうかがわからない?
max_h = max(h_arr)
max_w = max(w_arr)
max_h_indexes = [i for i, x in enumerate(h_arr) if x == max_h]
max_w_indexes = [i for i, x in enumerate(w_arr) if x == max_w]
ans = max_h + max_w -1
#たぶんindexesが多いときにTLEになってるんじゃないか
if not(max_h == h or max_w) == w:
    for i in max_h_indexes:
        for j in max_w_indexes:
            if not((i+1,j+1) in record):
                ans += 1
                break
            else:
                continue
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import random
h,w,m = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(m)]
exist = set()
tate = [[0,i] for i in range(h)]
yoko = [[0,i] for i in range(w)]

for y,x in lis:
    exist.add(format(y).zfill(6)+format(x).zfill(6))
    tate[y-1][0] += 1
    yoko[x-1][0] += 1

tate.sort(key=lambda x:x[1])
yoko.sort(key=lambda x:x[1])
tate.sort(key=lambda x:x[0],reverse=True)
yoko.sort(key=lambda x:x[0],reverse=True)

ans = tate[0][0]+yoko[0][0]-1
if h == 1 or w == 1:
    print(m)
    exit()

  # print(tate)
  # print(yoko)

i = 0
j = 0
while i < h and i <= 120:
    while j < w and j <= 120:
        ans = max(ans,tate[i][0]+yoko[j][0]-bool(format(tate[i][1]+1).zfill(6)+format(yoko[j][1]+1).zfill(6) in exist))
        j += 1
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
h_array = [0]*H
w_array = [0]*W
array = []
for _ in range(N):
    h, w = map(int, input().split())
    array.append([h,w])
    h_array[h-1] +=1
    w_array[w-1] +=1
max_h = max(h_array)
h_indexes = [i for i, x in enumerate(h_array) if x == max_h]
max_w = max(w_array)
w_indexes = [i for i, x in enumerate(w_array) if x == max_w]

count=0
print(h_indexes,w_indexes)

for i in range(len(h_indexes)): 
  for j in range(len(w_indexes)):
    if [h_indexes[i]+1, w_indexes[j]+1] in array:
      count = max_h + max_w -1
    else:
      count = max_h + max_w
      break
print(count)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    input = readline
    mod = 10 ** 9 + 7
    H, W, M = map(int, input().split())
    Hlist = [0] * H
    Wlist = [0] * W
    hwset = [set() for _ in range(H)]

    for _ in range(M):
        h, w = map(int, input().split())
        Hlist[h-1] += 1
        Wlist[w-1] += 1
        hwset[h-1].add(w)


    hmax = [i for i, x in enumerate(Hlist) if x == max(Hlist)]
    wmax = [i for i, x in enumerate(Wlist) if x == max(Wlist)]

    flag = False
    for h in hmax:
        for w in wmax:
            if w in hwset[h-1]:
                flag = True
                break

    if flag:
        print(Hlist[h] + Wlist[w])
    else:
        print(Hlist[h] + Wlist[w] - 1)





if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from time import time

start = time()

H,W,M=map(int,input().split())
hw = defaultdict(int)
sum1=[0]*H
sum2=[0]*W
for i in range(M):
  h,w=map(int,input().split())
  hw[(h-1,w-1)] += 1
  sum1[h-1] += 1
  sum2[w-1] += 1

sum1 = sorted(enumerate(sum1),key=lambda x:x[1],reverse=True)
sum2 = sorted(enumerate(sum2),key=lambda x:x[1],reverse=True)

ans=0
for idx1,val1 in sum1:
  if time()-start>2.2:break
  for idx2,val2 in sum2:
    tmp = val1+val2-hw[(idx1,idx2)]
    ans = max(ans,tmp)
    
print(ans)

Submission status: Wrong Answer

Solution code:
h, w, m = map(int, input().split())
row, col = [0]*h, [0]*w
target = [[0] * w] * h
for i in range(m):
	x, y = map(int, input().split())
	row[x-1] += 1
	col[y-1] += 1
	target[x-1][y-1] = 1
ans = 0
for i in range(h):
	for j in range(w):
		tmp = row[i] + col[j]
		if target[i][j] == 1:
			tmp -= 1
		ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import random
h,w,m = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(m)]
exist = set()
tate = [[0,i] for i in range(h)]
yoko = [[0,i] for i in range(w)]

for y,x in lis:
    exist.add(format(y).zfill(6)+format(x).zfill(6))
    tate[y-1][0] += 1
    yoko[x-1][0] += 1

tate.sort(key=lambda x:x[1])
yoko.sort(key=lambda x:x[1])
tate.sort(key=lambda x:x[0],reverse=True)
yoko.sort(key=lambda x:x[0],reverse=True)

ans = tate[0][0]+yoko[0][0]-1
if h == 1 or w == 1:
    print(m)
    exit()

  # print(tate)
  # print(yoko)

i = 0
j = 0
while i < h and i <= 100:
    while j < w and j <= 100:
        ans = max(ans,tate[i][0]+yoko[j][0]-bool(format(tate[i][1]+1).zfill(6)+format(yoko[j][1]+1).zfill(6) in exist))
        j += 1
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    h, w, m = i_map()
    num_list = i_row_list(m)

    if h == 1 or w == 1:
        print(m)
        exit()

    h_list = []
    w_list = []

    count = 0

    for num in num_list:
        h_list.append(num[0])
        w_list.append(num[1])

    co_h_list = Counter(h_list)
    co_w_list = Counter(w_list)

    print(num_list)
    print(co_h_list.most_common())
    print(co_w_list.most_common())

    h_max_key = co_h_list.most_common()[0][0]
    h_max_value = co_h_list.most_common()[0][1]

    w_max_key = co_w_list.most_common()[0][0]
    w_max_value = co_w_list.most_common()[0][1]

    if h_max_value > w_max_value:
        max_index = h_max_key
        count = h_max_value

        tmp_list = []
        for num in num_list:
            if num[0] == max_index:
                continue
            tmp_list.append(num[1])

        if len(tmp_list) != 0:
            count += Counter(tmp_list).most_common()[0][1]
    elif h_max_value == w_max_value:
        max_index = h_max_key
        h_count = h_max_value

        tmp_list = []
        for num in num_list:
            if num[0] == max_index:
                continue
            tmp_list.append(num[1])

        if len(tmp_list) != 0:
            h_count += Counter(tmp_list).most_common()[0][1]     


        max_index = w_max_key
        w_count = w_max_value

        tmp_list = []
        for num in num_list:
            if num[1] == max_index:
                continue
            tmp_list.append(num[0])

        if len(tmp_list) != 0:  
            w_count += Counter(tmp_list).most_common()[0][1]

        count = max(h_count, w_count)
    else:
        max_index = w_max_key
        count = w_max_value

        tmp_list = []
        for num in num_list:
            if num[1] == max_index:
                continue
            tmp_list.append(num[0])

        if len(tmp_list) != 0:  
            count += Counter(tmp_list).most_common()[0][1]

    print(count)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter
x=0
H,W,M = map(int,input().split())
h = [0]*M
w = [0]*M
for i in range(0,M):
  h[i],w[i] = map(int,input().split())
a = Counter(h).most_common()
b = Counter(w).most_common()
x = a[0][1]+b[0][1]

print(x)

Submission status: Wrong Answer

Solution code:
h,w,m= map(int,input().split())
a=[0]*h
b=[0]*w
ab=[]

for i in range(m):
    hh,ww= map(int,input().split())
    a[hh-1]+=1
    b[ww-1]+=1
    ab.append([hh-1,ww-1])
    
mm=0
aa=[]
for i in range(h):
    if a[i]>mm:
        aa=[]
        mm=a[i]
    elif a[i]==mm:
        aa.append(i)

mmm=0
bb=[]
for i in range(w):
    if b[i]>mmm:
        bb=[]
        mmm=b[i]
    elif b[i]==mmm:
        bb.append(i)        

ans=False
for i in aa:
    for j in bb:
        if [i,j] in ab:
            ans=True
if ans:
    print(mm+mmm)
else:
    print(mm+mmm-1)

Submission status: Wrong Answer

Solution code:
H, W, M = list(map(int, input().split()))
HW = set()
hd = {}
wd = {}
for i in range(M):
	h, w = tuple(map(int, input().split()))
	HW.add((h, w))
	if h in hd:
		hd[h] += 1
	else:
		hd[h] = 1
	if w in wd:
		wd[w] += 1
	else:
		wd[w] = 1

hm = max(hd, key=hd.get)
wm = max(wd, key=wd.get)
if (hm, wm) in HW:
	print(hd[hm] + wd[wm] - 1)
else:
	print(hd[hm] + wd[wm])