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 sys
def input():
    return sys.stdin.readline()[:-1]

h, w, m = list(map(int, input().split()))
bh = [0 for j in range(h)]
bw = [0 for j in range(w)]
bhw = [[0 for j in range(w)] for i in range(h)]
hmaxs = []
wmaxs = []

for i in range(m):
    h,w = map(int, input().split())
    bh[h-1] +=1
    bw[w-1] +=1
    bhw[h-1][w-1] +=1

maxbh= max(bh)
maxbw =max(bw)

for i in range(h):
    if bh[i] == maxbh:
        hmaxs.append(i)

for i in range(w):
    if bw[i] == maxbw:
        wmaxs.append(i)

c =1

for i in hmaxs:
    for j in wmaxs:

        if bhw[i][j]==0:
            c=0

print(maxbh + maxbw -c)

Submission status: Wrong Answer

Solution code:
H,W,M=map(int,input().split())
MX=[0]*H
MY=[0]*W
MA=[0]*M

for i in range(M):
  x,y=map(int,input().split())
  x-=1
  y-=1
  MX[x]+=1
  MY[y]+=1
  MA[i]=(x,y)
xmax=max(MX)
ymax=max(MY)
ix=[x[0] for x in enumerate(MX) if x[1]==xmax]
iy=[x[0] for x in enumerate(MY) if x[1]==ymax]

i=0
for x,y in MA:
  if x in ix or y in iy:
    pass
  else:
    i+=1
  
  if len(ix)*len(iy)>M-i:
    print(xmax+ymax)
else:
  print(xmax+ymax-1)

Submission status: Wrong Answer

Solution code:
import sys, math, heapq, bisect





if(__name__ == "__main__"):
    """
    N = int( input().strip() )
    Length = list( map(int, input().strip().split() ) )
    """
    H, W, N_bombs= map(int, input().strip().split() )

    H_N_bomb = [ [0, i]  for i in range(H) ]
    W_N_bomb = [ [0, i]  for i in range(W) ]
    bombs = [ [] for _ in range(H)  ]


    for i in range(N_bombs):
        y, x = map(int, input().strip().split() )

        W_N_bomb[x-1][0] += 1
        H_N_bomb[y-1][0] += 1
        bombs[y-1].append(x-1)


    W_N_bomb.sort(key = lambda x: x[0])
    H_N_bomb.sort(key = lambda x: x[0])



    Ws = [ W_N_bomb[i][0] for i in range(W)]
    Hs = [ H_N_bomb[i][0] for i in range(H)]



    pos_x= bisect.bisect_left(Ws, Ws[-1])
    pos_y= bisect.bisect_left(Hs, Hs[-1])
    #print(W_N_bomb, H_N_bomb, pos_x, pos_y)
    flag = False
    value = Ws[-1] + Hs[-1]

    for j in range(pos_y, H):
        pos_j = H_N_bomb[j][1]
        if(len(bombs[pos_j])==0 ):
            flag = True
            break
        indices = []
        for i in range(pos_x, W):
            pos_i = W_N_bomb[i][1]

            indices.append( bisect.bisect(bombs[pos_j], pos_i) )
            indices.sort()
            if(indices[0] != indices[-1]):
                flag=True
                break
    if(flag==True):
        print(value)
    else:
        print(value-1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

h, w, m = list(map(int, input().split()))
bh = [0 for j in range(h)]
bw = [0 for j in range(w)]
bhw = [[0 for j in range(w)] for i in range(h)]
hmaxs = []
wmaxs = []

for i in range(m):
    h,w = map(int, input().split())
    bh[h-1] +=1
    bw[w-1] +=1
    bhw[h-1][w-1] +=1

maxbh= max(bh)
maxbw =max(bw)

for i in range(h):
    if bh[i] == maxbh:
        hmaxs.append(i)

for i in range(w):
    if bw[i] == maxbw:
        wmaxs.append(i)

c =1



for i in hmaxs:
    for j in wmaxs:
       

        if bhw[i][j]==0:
            c=0

print(maxbh + maxbw -c)

Submission status: Wrong Answer

Solution code:
from collections import Counter
from collections import defaultdict

def li():
    return [int(x) for x in input().split()]

H, W, M = li()
PH = []
PW = []

for i in range(M):
    h, w = li()
    PH.append(h)
    PW.append(w)

frequency_h_list = Counter(PH).most_common()
frequency_w_list = Counter(PW).most_common()
most_common_h_cnt = frequency_h_list[0][1]
most_common_w_cnt = frequency_w_list[0][1]

most_common_h_list = []
for x in frequency_h_list:
    if x[1] < most_common_h_cnt:
        break
    most_common_h_list.append(x[0])

most_common_w_list = []
for x in frequency_w_list:
    if x[1] < most_common_w_cnt:
        break
    most_common_w_list.append(x[0])

contain_intersection = {}
for h in most_common_h_list:
    for w in most_common_w_list:
        contain_intersection[h, w] = False

add = 0

for i in range(M):
    h, w = PH[i], PW[i]
    if h not in most_common_h_list or w not in most_common_w_list:
        continue
    if (h, w) not in contain_intersection:
        add = -1
        break
cnt = most_common_h_cnt + most_common_w_cnt + add

print(cnt)

Submission status: Wrong Answer

Solution code:
from collections import Counter
from collections import defaultdict

def li():
    return [int(x) for x in input().split()]

H, W, M = li()
PH = []
PW = []

for i in range(M):
    h, w = li()
    PH.append(h)
    PW.append(w)

frequency_h_list = Counter(PH).most_common()
frequency_w_list = Counter(PW).most_common()
most_common_h_cnt = frequency_h_list[0][1]
most_common_w_cnt = frequency_w_list[0][1]

most_common_h_list = []
for x in frequency_h_list:
    if x[1] < most_common_h_cnt:
        break
    most_common_h_list.append(x[0])

most_common_w_list = []
for x in frequency_w_list:
    if x[1] < most_common_w_cnt:
        break
    most_common_w_list.append(x[0])

contain_intersection = {}
for h in most_common_h_list:
    for w in most_common_w_list:
        contain_intersection[h, w] = False

add = 0

for i in range(M):
    h, w = PH[i], PW[i]
    if h not in most_common_h_list and w not in most_common_w_list:
        continue
    if (h, w) not in contain_intersection:
        add = -1
        break
cnt = most_common_h_cnt + most_common_w_cnt + add

print(cnt)

Submission status: Wrong Answer

Solution code:
from collections import deque, Counter, defaultdict
import itertools

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

d_h = deque()
d_w = deque()

dic = defaultdict(list)

for h, w in HW:
    d_h.append(h)
    d_w.append(w)
    dic[h].append(w)

# 出現頻度順にソート
c_h = Counter(d_h).most_common()
c_w = Counter(d_w).most_common()

# 登場頻度の一番高い回数
n_h = c_h[0][1]
n_w = c_w[0][1]

# 同率のものを数える
t_h = deque()
t_w = deque()

for k, v in c_h:
    if v == n_h:
        t_h.append(k)
    else:
        break

for k, v in c_w:
    if v == n_w:
        t_w.append(k)
    else:
        break


nn = len(t_h) * len(t_w)

ans = n_h + n_w
"""
if nn <= M:
    q = set(list(itertools.product(t_h, t_w)))
    for h, w in q:
        if w not in dic[h]:
            break
    else:
        ans -= 1

"""
print(ans)

Submission status: Wrong Answer

Solution code:
H,W,M=map(int,input().split())
MX=[0]*H
MY=[0]*W
MA=[0]*M

for i in range(M):
  x,y=map(int,input().split())
  x-=1
  y-=1
  MX[x]+=1
  MY[y]+=1
  MA[i]=(x,y)
xmax=max(MX)
ymax=max(MY)
ix=[x[0] for x in enumerate(MX) if x[1]==xmax]
iy=[x[0] for x in enumerate(MY) if x[1]==ymax]

if len(ix)*len(iy)>M:
  print(xmax+ymax)
else:
  print(xmax+ymax-1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
from collections import defaultdict

def li():
    return [int(x) for x in input().split()]

H, W, M = li()
PH = []
PW = []

for i in range(M):
    h, w = li()
    PH.append(h)
    PW.append(w)

frequency_h_list = Counter(PH).most_common()
frequency_w_list = Counter(PW).most_common()
most_common_h_cnt = frequency_h_list[0][1]
most_common_w_cnt = frequency_w_list[0][1]

most_common_h_list = []
for x in frequency_h_list[1:]:
    if x[1] < most_common_h_cnt:
        break
    most_common_h_list.append(x[0])

most_common_w_list = []
for x in frequency_w_list[1:]:
    if x[1] < most_common_w_cnt:
        break
    most_common_w_list.append(x[0])

contain_intersection = {}
for h in most_common_h_list:
    for w in most_common_w_list:
        contain_intersection[h, w] = False

for i in range(M):
    h, w = PH[i], PW[i]
    if (h, w) in contain_intersection:
        contain_intersection[h, w] = True
cnt = most_common_h_cnt + most_common_w_cnt
if all([contain_intersection[key] for key in contain_intersection]):
    cnt -= 1

print(cnt)

Submission status: Wrong Answer

Solution code:
H,W,M=map(int,input().split())
MX=[0]*H
MY=[0]*W
MA=[0]*M

for i in range(M):
  x,y=map(int,input().split())
  x-=1
  y-=1
  MX[x]+=1
  MY[y]+=1
  MA[i]=(x,y)
xmax=max(MX)
ymax=max(MY)
ix=[x[0] for x in enumerate(MX) if x[1]==xmax]
iy=[x[0] for x in enumerate(MY) if x[1]==ymax]

for x,y in MA:
  if x not in ix and y not in iy:
    print(xmax+ymax)
    exit()
print(xmax+ymax-1)

Submission status: Wrong Answer

Solution code:
import itertools

import numpy as np

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

A = np.zeros(H, dtype=np.int64)
B = np.zeros(W, dtype=np.int64)

for h, w in HW:
    A[h - 1] += 1
    B[w - 1] += 1

maxA = np.max(A)
maxB = np.max(B)

# 最大値を取るindex
ind_A = np.arange(H)[A == maxA]
ind_B = np.arange(W)[B == maxB]

# 解の候補の数
nn = len(ind_A) * len(ind_B)

# Mより候補の数のほうが多いとok
# 少ないときはM以下なので可算
ans = maxA + maxB
if nn <= M:
    for h, w in itertools.product(ind_B, ind_A):
        if [h + 1, w + 1] not in HW:
            break
    else:
        ans -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
H,W,M=map(int,input().split())
MX=[0]*H
MY=[0]*W
MA=[0]*M

for i in range(M):
  x,y=map(int,input().split())
  x-=1
  y-=1
  MX[x]+=1
  MY[y]+=1
  MA[i]=(x,y)
xmax=max(MX)
ymax=max(MY)
ix=[x[0] for x in enumerate(MX) if x[1]==xmax]
iy=[x[0] for x in enumerate(MY) if x[1]==ymax]

for x,y in MA:
  if (x not in ix)&(y not in iy):
    print(xmax+ymax)
    exit()
print(xmax+ymax-1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W, M = map(int, input().split())
count_row = Counter()
count_col = Counter()
count_point = Counter()
for i in range(M):
    h, w = map(int, input().split())
    count_row[h] += 1
    count_col[w] += 1
    count_point[(h, w)] += 1

max_h, max_count_row = count_row.most_common()[0]
max_w, max_count_col = count_col.most_common()[0]
row_iter = [h for h, c in count_row.most_common() if c == max_count_row]
col_iter = [w for w, c in count_col.most_common() if c == max_count_col]
ans = max_count_row + max_count_col - 1
for h in row_iter:
    for w in col_iter:
        if count_point[(h, w)]:
            continue
        ans += 1
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import collections
h,w,m=map(int,input().split())
H1=[]
H2=[]
W1=[]
W2=[]
S=[]

for _ in range(m):
  a,b=map(int,input().split())
  H1.append(a)
  W1.append(b)
  S.append([a,b])
  
HC1=collections.Counter(H1)
commonHC1=HC1.most_common()[0][0]
Hn1=HC1.most_common()[0][1]
WC1=collections.Counter(W1)
commonWC1=WC1.most_common()[0][0]
Wn1=WC1.most_common()[0][1]

for i in range(m):
  if S[i][0]!=commonHC1:
    W2.append(S[i][1])
  if S[i][1]!=commonWC1:
    H2.append(S[i][0])
if W2!=[]:
  WC2=collections.Counter(W2)
  Wn2=WC2.most_common()[0][1]
else:
  Wn2=0
if H2!=[]:
  HC2=collections.Counter(H2)
  Hn2=HC2.most_common()[0][1]
else:
  Hn2=0

print(max(Hn1+Wn2,Hn2+Wn2))

Submission status: Wrong Answer

Solution code:
H,W,M=map(int,input().split())
MX=[0]*H
MY=[0]*W
MA=[0]*M

for i in range(M):
  x,y=map(int,input().split())
  x-=1
  y-=1
  MX[x]+=1
  MY[y]+=1
  MA[i]=(x,y)
xmax=max(MX)
ymax=max(MY)
ix=[x[0] for x in enumerate(MX) if x[1]==xmax]
iy=[x[0] for x in enumerate(MY) if x[1]==ymax]

flg=False
for x,y in MA:
  if (x in ix)&(y in iy):
    print(xmax+ymax)
    exit()
print(xmax+ymax-1)

Submission status: Wrong Answer

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

xy = [list(map(int, input().split())) for _ in range(M)]
x, y = [list(i) for i in zip(*xy)]

x_score = [0]*(H+1)
y_score = [0]*(W+1)
total = 0

for i in range(M):
	x_score[x[i]] += 1
	y_score[y[i]] += 1



total = max(x_score) +  max(y_score)

if [x_score.index(max(x_score)), y_score.index(max(y_score))] in xy:
	total -= 1
print(total)

Submission status: Wrong Answer

Solution code:
import itertools

import numpy as np

H, W, M = map(int, input().split())
HW = [list(map(int, input().split())) for _ in range(M)]
print('h')
"""
F = np.zeros([H, W], dtype=np.int64)

for h, w in HW:
    F[h - 1, w - 1] = 1

# 各方向の和を計算
A = np.sum(F, axis=0)
B = np.sum(F, axis=1)

maxA = np.max(A)
maxB = np.max(B)

# 最大値を取るindex
ind_A = np.arange(W)[A == maxA]
ind_B = np.arange(H)[B == maxB]

cnt = 0
ans = maxA + maxB
for h, w in itertools.product(ind_B, ind_A):
    if F[h, w] == 0:
        break

    cnt += 1
    if cnt == M:
        ans -= 1
        break
else:
    ans -= 1
print(ans)
"""

Submission status: Wrong Answer

Solution code:
temparr = input()
temparr = temparr.split()
row = int(temparr[0])
col = int(temparr[1])
m = int(temparr[2])
dicts= {}
dicts1 = {}
for i in range(m):
    temparr = input()
    temparr = temparr.split()
    x = int(temparr[0])
    y = int(temparr[1])
    if x not in dicts:
        dicts[x] = []
    dicts[x].append(y)
    if y not in dicts1:
        dicts1[y] = []
    dicts1[y].append(x)
    
firstround = []
for i in sorted (dicts, key=lambda item: len(dicts[item]) ) :  
    firstround.append(i)
maxsitem = firstround[-1]
ans = 0 
#print(dicts)
#print("maxsitem")
#print(maxsitem)
ans += len(dicts[maxsitem])
for i in dicts[maxsitem]:
    dicts1[i].remove(maxsitem)
secondround = []
#print(dicts1)
for i in sorted(dicts1, key=lambda item: len(dicts1[item]) ):
    secondround.append(i)
    
#print(secondround)
ans += len( dicts1[ secondround[-1] ] )
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
from bisect import bisect_left, bisect_right
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from copy import deepcopy

INF = float('inf')


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


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


def LS(): return sys.stdin.readline().split()


def S(): return sys.stdin.readline().strip()


def IR(n): return [I() for i in range(n)]


def LIR(n): return [LI() for i in range(n)]


def SR(n): return [S() for i in range(n)]


def LSR(n): return [LS() for i in range(n)]


def SRL(n): return [list(S()) for i in range(n)]


def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]


mod = 10 ** 9 + 7

h,w,m= LI()
s=set()
D1=defaultdict(int)
D2=defaultdict(int)
for _ in range(m):
    h,w=LI()
    D1[h-1]+=1
    D2[w-1]+=1
    s.add((h-1,w-1))

h_max = max(D1.values())
w_max = max(D2.values())
A=[i for i,v in D1.items() if v==h_max]
B=[i for i,v in D2.items() if v==w_max]

minus = -1
for i in A:
    for j in B:
        if (i, j) not in s:
            minus = 0
            break

print(w_max+h_max-minus)

Submission status: Wrong Answer

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

hc = [0] * H
wc = [0] * W
d = {}
for _ in range(M):
    h, w = map(lambda x: int(x) - 1,input().split())
    hc[h] += 1
    wc[w] += 1
    d[(h, w)] = 1

maxh = max(hc)
maxw = max(wc)

result = max(maxh + maxw - 1, 0)
wi = [i for i in range(W) if wc[i] == maxw]
for h in [i for i in range(H) if hc[i] == maxh]:
    for w in wi:
        if (h, w) in d:
            result = maxh + maxw
            break
print(result)

Submission status: Wrong Answer

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

hc = [0] * H
wc = [0] * W
d = {}
for _ in range(M):
    h, w = map(lambda x: int(x) - 1,input().split())
    hc[h] += 1
    wc[w] += 1
    d[(h, w)] = 1

maxh = max(hc)
maxw = max(wc)

result = maxh + maxw - 1
wi = [i for i in range(W) if wc[i] == maxw]
for h in [i for i in range(H) if hc[i] == maxh]:
    for w in wi:
        if (h, w) in d:
            result = maxh + maxw
            break
print(result)

Submission status: Wrong Answer

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

# from typing import Optional
from typing import List
import array
import numpy as np

# from typing import Dict
from typing import Tuple

# from typing import Sequence


def main():
    args = input().split()
    H = int(args[0])
    W = int(args[1])
    M = int(args[2])

    codList = np.zeros((0, 2), dtype="int64")
    for i in range(M):
        args = input().split()
        codList = np.append(codList, np.array([[int(args[0]), int(args[1])]]), axis=0)

    columnSumList = np.zeros(W, dtype="int64")
    rowSumList = np.zeros(H, dtype="int64")

    maxColumnSum = 0
    maxColumIndexList: array[int] = []
    maxRowSum = 0
    maxRowIndexList: array[int] = []
    for item in codList:
        columnSum = columnSumList[item[1] - 1] + 1
        if columnSum > maxColumnSum:
            maxColumnSum = columnSum
            maxColumIndexList = [item[1]]
        elif columnSum == maxColumnSum:
            maxColumIndexList.append(item[1])

        rowSum = rowSumList[item[0] - 1] + 1
        if rowSum > maxRowSum:
            maxRowSum = rowSum
            maxRowIndexList = [item[0]]
        elif rowSum == maxRowSum:
            maxRowIndexList.append(item[0])

        columnSumList[item[1] - 1] = columnSum
        rowSumList[item[0] - 1] += 1

    result = maxColumnSum + maxRowSum
    # for row in maxRowIndexList:
    #     for column in maxColumIndexList:
    #         data = np.array([row, column])
    #         isMatch = False
    #         for item in codList:
    #             if np.allclose(item, data):
    #                 isMatch = True

    #         if isMatch:
    #             continue
    #         else:
    #             print(result)
    #             return

    print(result - 1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

lhm=max(lh)
lhml=[i for i, x in enumerate(lh) if x == lhm]
lwm=max(lw)
lwml=[i for i, x in enumerate(lw) if x == lwm]
 
for i in range(len(lhml)):
    lss=set(lwml) & set(ls[i])
    lss=list(lss)
    if len(lss)>0:pass
    else:
        print(lhm+lwm)
        exit()
print(lhm+lwm-1)

Submission status: Wrong Answer

Solution code:
import numpy as np

h, w, m= map(int, input().split())
line=np.zeros(h, dtype='int8')
colm=np.zeros(w, dtype='int8')
locline=np.zeros(m,dtype='int8')
loccolm=np.zeros(m,dtype='int8')

for i in range(0,m):
  hi, wi = map(int, input().split())
  line[hi-1]=line[hi-1]+1 
  colm[wi-1]=colm[wi-1]+1 
  locline[i]=hi-1
  loccolm[i]=wi-1
    
#colm=np.sum(ans, axis=0)
#line=np.sum(ans, axis=1)

maxline=max(line)
maxcolm=max(colm)

ataimax=0
for ih in range(0,h):
  if (line[ih] == maxline):
    for iw in range(0,w):
      if (colm[iw] == maxcolm):
        cho=0
        for i in range(0,m):
          if ((locline[i] == ih) and (loccolm[i] == iw)): cho=1
        atai=colm[iw]+line[ih]-cho

        if (atai >= ataimax): ataimax=atai

print (ataimax)

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    s = set()
    h,w,m = map(int,input().split())
    a = [[[0] * 2 for i in range(h)] for j in range(w)]
    a = [0]*h
    b = [0]*w
    for i in range(m):
        x,y = map(int,input().split())
        x -= 1
        y -= 1
        a[x] += 1
        b[y] += 1
        s.add((x,y))
    amax = max(a)
    bmax = max(b)
    ans = amax+bmax
    print(ans)

Submission status: Wrong Answer

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

xy = [map(int, input().split()) for _ in range(M)]
x, y = [list(i) for i in zip(*xy)]

x_score = [0]*(H+1)
y_score = [0]*(W+1)
total = 0

for i in range(M):
	x_score[x[i]] += 1
	y_score[y[i]] += 1



total = max(x_score) +  max(y_score)
for i in range(M):
	if x[i] == x_score.index(max(x_score))  and y[i] == y_score.index(max(y_score)):
		total -= 1
		break

print(total)

Submission status: Wrong Answer

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




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




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





count_H = collections.Counter(bakuha_H)
H_most = count_H.most_common()
H_most_num = H_most[0][1]

#H_only_most = [x for x in H_most if x[1]==H_most_num]
#H_kouho = [x[0] for x in H_only_most]

H_kouho = [x[0] for x in H_most if x[1]==H_most_num]


count_W = collections.Counter(bakuha_W)
W_most = count_W.most_common()
W_most_num = W_most[0][1]

#W_only_most = [x for x in W_most if x[1]==W_most_num]
#W_kouho = [x[0] for x in W_only_most]
W_kouho = [x[0] for x in W_most if x[1]==W_most_num]

ans = H_most_num + W_most_num

#マイナス1するかどうか
ans_minus = True

len_H_kouho=len(H_kouho)
len_W_kouho=len(W_kouho)
num_kouho = len_H_kouho*len_W_kouho

bakuha_1d = [W * (x[0]-1) + x[1] for x in bakuha]
bakuha_1d.sort(reverse=True)
bakuha_1d_max = bakuha_1d[0]
bakuha_1d_min = bakuha_1d[-1]

#print(bakuha_1d)

if num_kouho <= M:
    kouho_1d = []
    for h_k in H_kouho:
        for w_k in W_kouho:
            kouho_1d.append(W*(h_k-1)+w_k)
    kouho_1d.sort()
    kouho_1d_min = kouho_1d[0]
    kouho_1d_max = kouho_1d[-1]
    
#    print(kouho_1d)    
    
    if kouho_1d_max <= bakuha_1d_max and kouho_1d_min >= bakuha_1d_min:
        b1_0 = bakuha_1d_min
        for_break=False
        for k1 in kouho_1d:
#            if k1 >= b1_0:
            tuzuku = True
            while tuzuku:
                b1_0 = bakuha_1d.pop()
                print(k1,b1_0)
                if b1_0 >= k1:
                    tuzuku = False
                    if b1_0 != k1:
                        ans_minus = False
                        for_break=True
            if for_break:
                break
    else:
        ans_minus = False
#    for i in range(M):
#        b1_0 = bakuha_1d.pop(0)
        
        
        
else:
    ans_minus = False
#
if ans_minus:
    ans-=1
    

print(ans)

Submission status: Wrong Answer

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

lst=[]
hhh=[0]*(h+1)
www=[0]*(w+1)
for i in range(m):
  a,b=map(int,input().split())
  hlist[a]+=1
  wlist[b]+=1
  lst.append([a,b])
  

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 ww:
  www[i]+=1

for i in hh:
  hhh[i]+=1

for i in lst:
  if hhh[i[0]]!=0 and www[i[1]]!=0:
    print(hcnt+wcnt-1)
    exit()

print(hcnt+wcnt)

Submission status: Wrong Answer

Solution code:
def main():
    H, W, M = map(int, input().split())
    # H, W, M = (3 * 10 ** 5, 3 * 10 ** 5, 3 * 10 ** 5)

    row = [0] * (H + 1)
    row_set = [set() for _ in range(H+1)]
    column = [0] * (W + 1)
    column_set = [set() for _ in range(W+1)]
    # import random
    ms = []
    for m in range(M):
        h, w = map(int, input().split())
        ms.append((w, h))
        # h, w = (random.randrange(3*10**5), random.randrange(3*10**5))
        row[h] += 1
        row_set[h].add(w)
        # if h in row_set:
        #     row_set[h].add(w)
        # else:
        #     row_set.update({h: set({w})})

        column[w] += 1
        column_set[w].add(h)
        # if w in column_set:
        #     column_set[w].add(h)
        # else:
        #     column_set.update({w: set({h})})

    flatten_max_rows = []
    maxR = -1
    for i, v in enumerate(row[1:]):
        if v > maxR:
            flatten_max_rows = []
            flatten_max_rows += list(row_set[i+1])
            maxR = v
        elif v == maxR:
            flatten_max_rows += list(row_set[i+1])
    flatten_max_rows_set = set(flatten_max_rows)

    flatten_max_cols = []
    maxC = -1
    for i, v in enumerate(column[1:]):
        if v > maxC:
            flatten_max_cols = []
            flatten_max_cols += list(column_set[i + 1])
            maxC = v
        elif v == maxC:
            flatten_max_cols += list(column_set[i + 1])
    flatten_max_cols_set = set(flatten_max_cols)

    for m in ms:
        if not (m[0] in flatten_max_cols_set and m[1] in flatten_max_rows_set):
            print(maxR + maxC)
            exit()

    # maxC = max(column)
    # for i, v in enumerate(column):
    #     if v == maxC:
    #         if i not in flatten_max_rows_set:
    #             print(maxR + maxC)
    #             exit()

    print(maxR + maxC - 1)
main()

Submission status: Wrong Answer

Solution code:
def main():
    H, W, M = map(int, input().split())
    # H, W, M = (3 * 10 ** 5, 3 * 10 ** 5, 3 * 10 ** 5)

    row = [0] * (H + 1)
    row_set = [set() for _ in range(H+1)]
    column = [0] * (W + 1)
    column_set = [set() for _ in range(W+1)]
    # import random
    ms = []
    for m in range(M):
        h, w = map(int, input().split())
        ms.append((w, h))
        # h, w = (random.randrange(3*10**5), random.randrange(3*10**5))
        row[h] += 1
        row_set[h].add(w)
        # if h in row_set:
        #     row_set[h].add(w)
        # else:
        #     row_set.update({h: set({w})})

        column[w] += 1
        column_set[w].add(h)
        # if w in column_set:
        #     column_set[w].add(h)
        # else:
        #     column_set.update({w: set({h})})

    flatten_max_rows = []
    maxR = -1
    for i, v in enumerate(row[1:]):
        if v > maxR:
            flatten_max_rows = []
            flatten_max_rows += list(row_set[i+1])
            maxR = v
        elif v == maxR:
            flatten_max_rows += list(row_set[i+1])
    flatten_max_rows_set = set(flatten_max_rows)

    flatten_max_cols = []
    maxC = -1
    for i, v in enumerate(column[1:]):
        if v > maxC:
            flatten_max_cols = []
            flatten_max_cols += list(column_set[i + 1])
            maxC = v
        elif v == maxC:
            flatten_max_cols += list(column_set[i + 1])
    flatten_max_cols_set = set(flatten_max_cols)

    for m in ms:
        if m[0] not in flatten_max_cols_set and m[1] not in flatten_max_rows_set:
            print(maxR + maxC)
            exit()

    # maxC = max(column)
    # for i, v in enumerate(column):
    #     if v == maxC:
    #         if i not in flatten_max_rows_set:
    #             print(maxR + maxC)
    #             exit()

    print(maxR + maxC - 1)
main()

Submission status: Wrong Answer

Solution code:
import numpy as np
from numba import njit

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

h = np.zeros(H + 1, np.int64)
w = np.zeros(W + 1, np.int64)
boms = set()

for _ in range(M):
    x, y = map(int, input().split())
    h[x] += 1
    w[y] += 1
    boms.add((x, y))

h_max = h.max()
w_max = w.max()

h_idx = np.where(h == h_max)[0]
w_idx = np.where(w == w_max)[0]


@njit
def solve(h_max, w_max, boms):
    minus = -1
    for x in h_idx:
        for y in w_idx:
            if (x, y) in boms:
                minus = 0
                break
    return h_max + w_max + minus


print(solve(h_max, w_max, boms))

Submission status: Wrong Answer

Solution code:
H,W,M = list(map(int, input().split()))
Mlist = []
Hnum = [0 for i in range(H)]
Wnum = [0 for i in range(W)]
for i in range(M):
  h,w = list(map(int, input().split()))
  Hnum[h-1] += 1
  Wnum[w-1] += 1
  Mlist.append([h,w])

Hmax = [0,-1]
Wmax = [0,-1,0]
for i in range(H):
  if Hnum[i] > Hmax[1]:
    Hmax[0] = i
    Hmax[1] = Hnum[i]

WHmaxexist = [0 for i in range(W)]
for i in range(M):
  if Mlist[i][0]-1 == Hmax[0]:
    WHmaxexist[Mlist[i][1]-1] = 1
for i in range(W):
  if Wnum[i]-WHmaxexist[i] > Wmax[1]:
    Wmax[0] = i
    Wmax[1] = Wnum[i]-WHmaxexist[i]

if [Hmax[0]+1, Wmax[0]+1] in Mlist:
  print(Hmax[1]+Wmax[1])
else:
  print(Hmax[1]+Wmax[1])

Submission status: Wrong Answer

Solution code:
from collections import deque, Counter
import itertools

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

d_h = deque()
d_w = deque()
for h, w in HW:
    d_h.append(h)
    d_w.append(w)

# 出現頻度順にソート
c_h = Counter(d_h).most_common()
c_w = Counter(d_w).most_common()


# 登場頻度の一番高い回数
n_h = c_h[0][1]
n_w = c_w[0][1]

# 同率のものを数える
t_h = deque()
t_w = deque()

for k, v in c_h:
    if v == n_h:
        t_h.append(k)
    else:
        break

for k, v in c_w:
    if v == n_w:
        t_w.append(k)
    else:
        break

q = set(list(itertools.product(t_h, t_w)))

"""
for h, w in HW:
    q.discard((h, w)

if len(q) == 0:
    ans = n_h + n_w - 1
else:
    ans = n_h + n_w

print(ans)
"""

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    s = set()
    h,w,m = map(int,input().split())
    a = [0]*h
    b = [0]*w
    for i in range(m):
        x,y = map(int,input().split())
        x -= 1
        y -= 1
        a[x] += 1
        b[y] += 1
        s.add((x,y))
    amax = max(a)
    bmax = max(b)
    c = []
    d = []
    for x in a:
      if amax == x:
        c.append(x)
    for x in b:
      if bmax == x:
        d.append(x)
    minus = True
    end = False
    for x in c:
      for y in d:
        if not (x,y) in s:
          minus = False
          end = True
        if end:
            break
      if end:
        break
    ans = amax+bmax
    if minus:
        ans -= 1
    print(ans)

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    s = set()
    h,w,m = map(int,input().split())
    a = [0]*h
    b = [0]*w
    for i in range(m):
        x,y = map(int,input().split())
        x -= 1
        y -= 1
        a[x] += 1
        b[y] += 1
        s.add((x,y))
    amax = max(a)
    bmax = max(b)
    c = []
    d = []
    for x in a:
      if amax == x:
        c.append(x)
    for x in b:
      if bmax == x:
        d.append(x)
    minus = 1
    end = False
    for x in c:
      for y in d:
        if not (x,y) in s:
          minus = 0
          end = True
        if end:
            break
      if end:
        break
          
    print(amax+bmax-minus)

Submission status: Wrong Answer

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

count_h = [0 for i in range(h)]
count_w = [0 for i in range(w)]
points = set()
for i in range(m):
    y, x = map(int, input().split())
    y -= 1
    x -= 1
    points.add((y, x))
    count_h[y] += 1
    count_w[x] += 1

ch_max, cw_max = max(count_h), max(count_w)
ys = [y for y in range(len(count_h)) if count_h[y] == ch_max]
xs = [x for x in range(len(count_w)) if count_w[x] == cw_max]

    
ans = 0
if len(ys) * len(xs) >= m:
    ans = max(ans, ch_max + cw_max)
else:
    for x in xs:
        for y in ys:
            if (y, x) in points:
                ans = max(ans, count_h[y] + count_w[x] - 1)
            else:
                ans = max(ans, count_h[y] + count_w[x])
print(ans)