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:
h,w,m=map(int,input().split())
import collections
h2l=[]
w2l=[]
for i in range(m):
    h2,w2=map(int,input().split())
    h2l.append(h2)
    w2l.append(w2)

ch=collections.Counter(h2l)
cw=collections.Counter(w2l)
'''
ansh=ch.most_common()[0][1]
answ=cw.most_common()[0][1]
ansx=ch.most_common()[0][0]
ansy=cw.most_common()[0][0]
ans=ansh+answ

nexth=ch.most_common()[0][1]+ch.most_common()[1][1]
nextw=cw.most_common()[0][1]+cw.most_common()[1][1]
'''
chcnt=0
cwcnt=0
for i in range(len(ch)-1):
    if ch.most_common()[i][1]==ch.most_common()[i+1][1]:
        chcnt+=1
for i in range(len(cw)-1):
    if cw.most_common()[i][1]==cw.most_common()[i+1][1]:
        cwcnt+=1
num=0
ansh=ch.most_common()[0][1]
ansx=ch.most_common()[0][0]
answ=cw.most_common()[0][1]
ansy=cw.most_common()[0][0]
ans=ansh+answ
point=1

i=0
j=0
for i in range(chcnt):
    for j in range(cwcnt):
        ansh=ch.most_common()[i][1]
        ansx=ch.most_common()[i][0]
        answ=cw.most_common()[j][1]
        ansy=cw.most_common()[j][0]
        ans=ansh+answ
        for k in range(m):
            if h2l[k]==ansx and w2l[k]==ansy:
                ans-=1
        num=max(num,ans)
num=max(num,ans)
'''
while(point==1):
    for i in range(1,h-1):
        out=0
        if ansh!=ch.most_common()[i-1][1]:
            out+=1
            ansh=ch.most_common()[i-1][1]
            ansx=ch.most_common()[i-1][0]
        else:
            ansh=ch.most_common()[i][1]
            ansx=ch.most_common()[i][0]

        for j in range(1,w-1):
            if cw.most_common()[j][1]!=ch.most_common()[i-1][1]:
                out+=1
                answ=cw.most_common()[j-1][1]
                ansy=cw.most_common()[j-1][0]
                if out>=2:
                    point=0
            else:
                answ=cw.most_common()[j][1]
                ansy=cw.most_common()[j][0]
            ans=ansh+answ
            if h2l[i]==ansx and w2l[j]==ansy:
                ans-=1
            num=max(ans,num)
'''
print(num)

Submission status: Wrong Answer

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

for i in range(m):
  if c[i][0] in aa:
    aa[c[i][0]] += 1
    if aa[c[i][0]] > ma:
    	ma = aa[c[i][0]]
    	ta = c[i][0]
  else:
    aa[c[i][0]] = 1
    
  if c[i][1] in bb:
    bb[c[i][1]] += 1
    if bb[c[i][1]] > mb:
    	mb = bb[c[i][1]]
    	tb = c[i][1]
  else:
    bb[c[i][1]] = 1

if [ta,tb] not in c:
  z = 0
    
print(max(aa.values())+ max(bb.values())-z)

Submission status: Wrong Answer

Solution code:
sum=0
i = list(map(int, input().split()))
H=i[0]
W=i[1]
N=i[2]
h=[0]*(H)
w=[0]*(W)
a = [[0 for j in range(W)] for i in range(H)]
for j in range(N):
    i = list(map(int, input().split()))
    h[i[0]-1]+=1
    w[i[1]-1]+=1
    a[i[0]-1][i[1]-1]=1
if max(h)>max(w):
    sum+=max(h)
    for i in range(W):
        if a[h.index(max(h))][i]==1:
            a[h.index(max(h))][i]=0
            w[i]-=1
    print(a)
    print(sum+max(w))
else:
    sum+=max(w)
    for i in range(H):
        if a[i][w.index(max(w))]==1:
            a[i][w.index(max(w))]=0
            h[i]-=1
    print(h)
    print(sum+max(h))

Submission status: Wrong Answer

Solution code:
h,w,m = map(int,input().split())
hl = [0]*h
wl = [0]*w
for i in range(m):
  hi,wi = map(int,input().split())
  hl[hi-1] += 1
  wl[wi-1] += 1
print(max(hl)+max(wl))

Submission status: Wrong Answer

Solution code:
H, W, M = map(int, input().split())
B = []
c={}
d={}

for i in range(M):
    b = list(map(int, input().split()))
    if b[0] not in c.keys():
        c[b[0]] =1
    else:
        c[b[0]] +=1
        
    if b[1] not in d.keys():
        d[b[1]] =1
    else:
        d[b[1]] +=1
    B.append(b)
#print(B)
#print(c,d,B)
c_max =max(c.values())
cc = [kv[0] for kv in c.items() if kv[1] == c_max]
d_max= max(d.values())
dd = [kv[0] for kv in d.items() if kv[1] == d_max]
#print(c_max,d_max)
#print(cc,c_max,dd,d_max)
if c_max == H and d_max== W:
    print((c_max+d_max)-1)
else:    
    for i in range (len(cc)):
        for n in range(len(dd)):
            #print(i, n, cc[i], dd[n])
            if [cc[i], dd[n]] not in B:
                print(c_max+d_max)
                break
        
            if i == len(cc)-1 and n == len(dd)-1:
                print((c_max+d_max)-1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
H,W,M=map(int,input().split())
A=[[0]*H]*W
Bomb_ls=[]
X=[0]*M
Y=[0]*M
for i in range(M):
  X[i],Y[i]=[int(x) for x in input().split()]
  Bomb_ls.append([X[i],Y[i]])
R=Counter(X).most_common()
C=Counter(Y).most_common()
if not [R[0],C[0]] in Bomb_ls:
  print(R[0][1]+C[0][1])
else:
  print(R[0][1]+C[0][1]-1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
H, W, M = map(int,input().split())
lh = []
lw = []
hw = []
for i in range(M):
    h, w = map(int,input().split())
    lh.append(h)
    lw.append(w)
    hw.append([h,w])
h = Counter(lh).most_common()[0][0]
w = Counter(lw).most_common()[0][0]
if [h,w] in hw: print(Counter(lh).most_common()[0][1] + Counter(lw).most_common()[0][1] - 1)
else: print(Counter(lh).most_common()[0][1] + Counter(lw).most_common()[0][1])

Submission status: Wrong Answer

Solution code:
import numpy as np
import itertools
H, W, M = map(int, input().split())
h = [0]*H
w = [0]*W
ab = []
for i in range(M):
    a, b = map(int, input().split())
    h[a-1] += 1
    w[b-1]+= 1
    ab.append((a, b))
hm = [i+1 for i, v in enumerate(h) if v == max(h)]
wm = [i+1 for i, v in enumerate(w) if v == max(w)]
hw = list(itertools.product(hm, wm))
print(hw)
print(ab)
if set(hw) <= set(ab):
    print(max(h)+max(w)-1)
else:
    print(max(h)+max(w))

Submission status: Wrong Answer

Solution code:
def resolve():
    import sys
    from collections import defaultdict
    import heapq
    input = sys.stdin.readline
    H, W, M = [int(i) for i in input().split()]
    # hhww = [list(map(int, input().split())) for _ in range(M)]
    hhww = set(tuple(int(i) - 1 for i in input().split()) for _ in range(M))
    dich = defaultdict(int)
    dicw = defaultdict(int)
    maxh, maxw = 0, 0
    for h, w, in hhww:
        dich[h] += 1
        dicw[w] += 1
        maxh = max(maxh, dich[h])
        maxw = max(maxw, dicw[w])
    dich_s = sorted(dich.items(), key=lambda x: -x[1])
    dicw_s = sorted(dicw.items(), key=lambda x: -x[1])
    if dich_s[0][1] > 1 and dicw_s[0][1] > 1:
        ans = dich_s[0][1] + dicw_s[0][1]
        if tuple([dich_s[0][0], dicw_s[0][0]]) in hhww:
            ans -= 1
    else:
        ans = dich_s[0][1] + dicw_s[0][1]
    print(ans)


resolve()

Submission status: Wrong Answer

Solution code:
from math import inf, log2


class SegmentTree:
    def __init__(self, array, func=max):
        self.n = len(array)
        self.size = 2**(int(log2(self.n-1))+1) if self.n != 1 else 1
        self.func = func
        self.default = 0 if self.func != min else inf
        self.data = [self.default] * (2 * self.size)
        self.process(array)

    def process(self, array):
        self.data[self.size : self.size+self.n] = array
        for i in range(self.size-1, -1, -1):
            self.data[i] = self.func(self.data[2*i], self.data[2*i+1])

    def query(self, alpha, omega):
        """Returns the result of function over the range (inclusive)!"""
        if alpha == omega:
            return self.data[alpha + self.size]
        res = self.default
        alpha += self.size
        omega += self.size + 1
        while alpha < omega:
            if alpha & 1:
                res = self.func(res, self.data[alpha])
                alpha += 1
            if omega & 1:
                omega -= 1
                res = self.func(res, self.data[omega])
            alpha >>= 1
            omega >>= 1
        return res

    def update(self, index, value):
        """Updates the element at index to given value!"""
        index += self.size
        self.data[index] = value
        index >>= 1
        while index:
            self.data[index] = self.func(self.data[2*index], self.data[2*index+1])
            index >>= 1


# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------- fast io --------------------
from math import gcd, ceil

def pre(s):
    n = len(s)
    pi = [0] * n
    for i in range(1, n):
        j = pi[i - 1]
        while j and s[i] != s[j]:
            j = pi[j - 1]
        if s[i] == s[j]:
            j += 1
        pi[i] = j
    return pi


def prod(a):
    ans = 1
    for each in a:
        ans = (ans * each)
    return ans

def lcm(a, b): return a * b // gcd(a, b)


def binary(x, length=16):
    y = bin(x)[2:]
    return y if len(y) >= length else "0" * (length - len(y)) + y
from math import ceil

from heapq import heappop, heappush

def dijkstra(graph, alpha):
    n = len(graph)
    distance = [float("inf")]*n
    distance[alpha] = 0
    queue = [(0, alpha)]
    while queue:
        length, v = heappop(queue)
        for x, dist in graph[v]:
            if dist + length < distance[x]:
                distance[x] = dist + length
                heappush(queue, (dist+length, x))
    return distance

for _ in range(int(input()) if not True else 1):
    h, w, m = map(int, input().split())
    hw = set()
    x = {}
    y = {}
    for i in range(m):
        xs, ys = list(map(int, input().split()))
        hw.add(str(xs)+","+str(ys))
        x[xs] = 1 if xs not in x else x[xs]+1
        y[ys] = 1 if ys not in y else y[ys]+1

    mx = max(x[k] for k in x)
    my = max(y[k] for k in y)

    ans = 0

    xx = [k for k in x if x[k] == mx][:10]
    yy = [k for k in y if y[k] == my][:10]

    for p in xx:
        for q in yy:
            ans = max(ans, x[p] + y[q] - 1 if str(p)+","+str(q) in hw else 0)
    print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:

def main():
    h, w, m = map(int, input().split())
    blis = []
    xdic = dict()
    ydic = dict()

    for _ in range(m):
        a, b = map(int, input().split())
        blis.append([a,b])

        if a in xdic:
            xdic[a] += 1
        else:
            xdic[a] = 1

        if b in ydic:
            ydic[b] += 1
        else:
            ydic[b] = 1

    xdic2 = sorted(xdic.items())
    ydic2 = sorted(ydic.items())
    
    xsaidai = xdic2[0][1] 
    ysaidai = ydic2[0][1]

    ans = 0

    if xsaidai >= ysaidai:
        for j in range(min(w, 10**2, len(xdic))):
            for i in range(min(h, 10**2, len(ydic))):
                
                x = xdic2[j][0]
                y = ydic2[i][0]
                tmp = 0

                tmp += ydic[y]
                tmp += xdic[x]

                if [x,y] in blis:
                    tmp -= 1

                if ans < tmp:
                    ans = tmp
                    #print(x, y, ydic[x], xdic[y])
    else:
        for i in range(min(h, 10**2, len(ydic))):
            for j in range(min(w, 10**2, len(xdic))):
                
                y = ydic2[i][0]
                x = xdic2[j][0]
                tmp = 0

                tmp += ydic[y]
                tmp += xdic[x]

                if [x,y] in blis:
                    tmp -= 1

                if ans < tmp:
                    ans = tmp
                    #print(x, y, ydic[x], xdic[y])
    print(ans)
            


if __name__ == "__main__":
    main()

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[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)

a = [tate[0]]
b = [yoko[0]]
for i in range(h-1):
    if tate[i][0] <= tate[i+1][0]+1:
        a.append(tate[i+1])
    else:
        break
for i in range(w-1):
    if yoko[i][0] <= yoko[i+1][0]+1:
        b.append(yoko[i+1])
    else:
        break
a = random.sample(a,len(a))
b = random.sample(b,len(b))

while i < len(a) and i <= 1800:
    j = 0
    while j < len(b) and j <= 1800:
        ans = max(ans,a[i][0]+b[j][0]-bool(format(a[i][1]+1).zfill(6)+format(b[j][1]+1).zfill(6) in exist))
        j += 1
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:


# coding: utf-8
# Your code here!
import numpy as np
H,W,M=map(int,input().split())
#s_h,s_w=map(int,input().split())
#g_h,g_w=map(int,input().split())
grid=[[0]*W for i in range(H)]

for i in range(M):
    x,y=map(int,input().split())
    grid[x-1][y-1]=1

a=np.sum(grid,axis=0)
b=np.sum(grid,axis=1)
max_m=0
t=[]
for i in range(len(a)):
    s=a[i]+b
    t.append([np.max(s),np.where(s==np.max(s))[0][0],i])
t=np.array(t)
h=np.where(t==np.max(t[:,:1]))[0]
count=1

for i in h:
    if grid[t[i][1]][t[i][2]]==0:
        count=0
        break

#print(np.max(t[:,:1])[0]-count)
print(np.max(t[:,:1])-count)

Submission status: Wrong Answer

Solution code:


strh,strw,strm = input().split()
h =int (strh)
w = int(strw)
m = int(strm)

dicth = {}
dictw = {}
hi = []
wi = []
for i in range(m):
  strhi , strwi = input().split()
  hi.append(int(strhi))
  wi.append(int(strwi))


for i in range(m):
  if not (hi[i] in dicth):
    dicth[hi[i]] = 1
  else:
    dicth[hi[i]] += 1
  if not (wi[i] in dictw):
    dictw[wi[i]] = 1
  else:
    dictw[wi[i]] += 1

max_h = max(dicth.values())
max_w = max(dictw.values())

max_kv_list_h = [kv for kv in dicth.items() if kv[1] == max(dicth.values())]
max_k_list_w = [kv[0] for kv in dictw.items() if kv[1] == max(dictw.values())]

#print(max_kv_list_h)
#print(max_k_list_w)

crossing = True 

for i in range(len(max_kv_list_h)):
  if crossing == False:
    break
    
  maxih = [i for i, x in enumerate(hi) if x == max_kv_list_h[0][1]]
  for ih in range(len(maxih)):
    if crossing == False:
      break

    for key in max_k_list_w:
      if wi[maxih[ih]] == key:
 #       print('ex')
        continue
      else:
        #print('not ex')
        crossing = False
        break


if crossing == True:
  num = max_h + max_w -1
else :
  num = max_h + max_w

print(num)

Submission status: Wrong Answer

Solution code:
H, W, M = map(int, input().split())
HA = [0]*H
WA = [0]*W
for m in range(M):
    h, w = map(int, input().split())
    HA[h-1] += 1
    WA[w-1] += 1
print(max(HA)+max(WA)-1)

Submission status: Wrong Answer

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

targets = []

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

h_count = [0] * H
w_count = [0] * W

for x, y in targets:
    h_count[x-1] += 1
    w_count[y-1] += 1

h_count_max = max(h_count)
w_count_max = max(w_count)

h_max = h_count.index(h_count_max)
w_max = w_count.index(w_count_max)

if [h_max+1, w_max+1] in targets:
    result = h_count_max + w_count_max - 1
else:
    result = h_count_max + w_count_max

print(result)

Submission status: Wrong Answer

Solution code:
h, w, m = map(int, input().split())
square = list()
for _ in range(h):
  row = list()
  for _ in range(w):
    row.append(0)
  square.append(row)
for _ in range(m):
  hi, wi = map(int, input().split())
  square[hi - 1][wi - 1] = 1

maxRow = maxCol = 0
row = col = 0
for i in range(len(square)):
  RowCount = square[i].count(1)
  if(RowCount > maxRow):
    row = i
    maxRow = RowCount
for j in range(len(square[0])):
  colCount = 0
  for i in range(len(square)):
    if(square[i][j] == 1):
      colCount += 1
  if(colCount > maxCol):
    col = j
    maxCol = colCount
if(square[row][col] == 1):
  print(maxRow + maxCol - 1)
else:
  print(maxRow + maxCol)

Submission status: Wrong Answer

Solution code:
def binary_search(data, value):
    left = 0            # 探索する範囲の左端を設定
    right = len(data) - 1            # 探索する範囲の右端を設定
    while left <= right:
        mid = (left + right) // 2            # 探索する範囲の中央を計算
        if data[mid] == value:
            # 中央の値と一致した場合は位置を返す
            return mid
        elif data[mid] < value:
            # 中央の値より大きい場合は探索範囲の左を変える
            left = mid + 1
        else:
            # 中央の値より小さい場合は探索範囲の右を変える
            right = mid - 1
    return -1            # 見つからなかった場合

import heapq

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

bombw = [0]*w
bombh = [0]*h
bomb = [[]]*h
heapq.heapify(bomb)
# print(bomb)
for i in range(m):
    hi,wi = map(int,input().split())
    bombw[wi-1] += 1
    bombh[hi-1] += 1
    heapq.heappush(bomb[hi-1], wi-1)
    # print(bomb)
maw = max(bombw)
mah = max(bombh)
# print(maw,mah)
inmaw = [i for i, x in enumerate(bombw) if x == maw]
inmah = [i for i, x in enumerate(bombh) if x == mah]
# print(inmaw,inmah)

ans = maw + mah
# print(bomb)
# print(ans)
for i in inmah:
    if binary_search(bomb, inmaw) != -1:
        print(ans)
        exit()
print(ans-1)

Submission status: Wrong Answer

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

    row_dict = {}
    col_dict = {}
    maps = []

    try:
        while True:
            row, col = map(int, input().split())
            maps.append((row, col))

            if row_dict.get(row):
                row_dict[row] += 1
            else:
                row_dict[row] = 1

            if col_dict.get(col):
                col_dict[col] += 1
            else:
                col_dict[col] = 1
    except EOFError:

        row_max_key = max(row_dict, key=row_dict.get)
        col_max_key = max(col_dict, key=col_dict.get)

        total = row_dict[row_max_key]
        total += col_dict[col_max_key]

        row_dict.pop(row_max_key)
        col_dict.pop(col_max_key)

        flag = True
        if row_dict and col_dict:

            row_max_key_2 = max(row_dict, key=row_dict.get)
            col_max_key_2 = max(col_dict, key=col_dict.get)
            total_2 = row_dict[row_max_key_2]
            total_2 += col_dict[col_max_key_2]

            if total == total_2:
                flag = False

        if (row_max_key, col_max_key) in maps and flag:
            total -= 1

        print(total)


def main():
    probE()


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

from collections import defaultdict

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

ctr_h = defaultdict(list)
ctr_w = defaultdict(list)

for h, w in X:
    ctr_h[h].append((h, w))
    ctr_w[w].append((h, w))

val_h = sorted(ctr_h.items(), key=lambda x: -len(x[1]))
val_w = sorted(ctr_w.items(), key=lambda x: -len(x[1]))

max_h = len(val_h[0][1])
max_w = len(val_w[0][1])

if (len(val_h) > 1 and len(val_h[1][1]) == max_h
        and len(val_w) > 1 and len(val_w[1][1]) == max_w):
    print(max_h + max_w)
else:
    h = val_h[0][0]
    w = val_w[0][0]
    for u, v in X:
        if u == h and v == w:
            print(max_h + max_w - 1)
            break
    else:
        print(max_h + max_w)

Submission status: Wrong Answer

Solution code:
import collections
h, w, m = map(int, input().split())
L_g = []
L_r = []
L = []
for i in range(m):
    a, b = map(int, input().split())
    L_g.append(a)
    L_r.append(b)
    L.append((a, b))
c1 = collections.Counter(L_g)
c2 = collections.Counter(L_r)

L1 = c1.most_common()
L2 = c2.most_common()

s = L1[0][1]
bL1 = []
d = 0
for i in range(len(L1)):
    if L1[i][1] == s:
        bL1.append(L1[i])
        d += 1
        if d == 3:
          break
    else:
        break
s = L2[0][1]
bL2 = []
d = 0
for i in range(len(L2)):
    if L2[i][1] == s:
        bL2.append(L2[i])
        d += 1
        if d == 3:
          break
    else:
        break
bL = []
for i in range(len(bL1)):
    for j in range(len(bL2)):
        bL.append((bL1[i][0], bL2[j][0]))
flag = 0
for e in bL:
    if e not in L:
        bom = e
        flag = 1
        break
if flag == 0:
    bom = bL[0]

t = L1[0][1]+L2[0][1]

if flag == 0:
        t -= 1
print(t)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

most_col = Counter(h).most_common()[0][0]
col_count = Counter(h).most_common()[0][1]

c_index = [i for i, x in enumerate(h) if x == most_col]

x = 0
for i in c_index:
    del w[i-x]
    x += 1

if len(w) == 0:
    print(col_count)
    exit()
    
row_count = Counter(w).most_common()[0][1]

ans = col_count + row_count

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import collections
import itertools

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

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

    countedh=collections.Counter(listH)
    countedw=collections.Counter(listW)

    retH = [letter for letter,count in countedh.most_common() if count == countedh.most_common()[0][1]]
    retW = [letter for letter,count in countedw.most_common() if count == countedw.most_common()[0][1]]

    # print(retH)
    # print(retW)
    tmp=0
    tmpMax=0

    tmp=countedh.most_common()[0][1]+countedw.most_common()[0][1]-1
    good=True
    for i in range(len(retH)):
        for j in range(len(retW)):
            for k in range(M):
                if(listH[k]==retH[i] and listW[k]==retW[j]):
                    good=False
                    break
            if(good):
                tmp+=1
                break
        else:
            continue
        break
    print(tmp)
resolve()

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

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 s in sys.stdin.readlines():
    h, w = map(int, s.split())
    height[h-1] += 1
    width[w-1] += 1
    object.append([h-1, w-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 not [i,j] in object:
            ans = h_max + w_max
            
else:
   ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
h, w, m = map(int, input().split(' '))
grid = {}

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

for i in range(m):
    hi, wi = input().split(' ')
    grid[(hi, wi)] = 1
    grid[(hi, '0')] = grid.get((hi, '0'), 0) + 1
    grid[('0', wi)] = grid.get(('0', wi), 0) + 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.get((h_max_grid,w_max_grid), 0)
    grid[('0','0')] = t if grid.get(('0','0'), 0) < t else grid.get(('0','0'), 0)

print(grid[('0','0')])

Submission status: Wrong Answer

Solution code:
H,W,M = map(int,input().split())
P = [0]*(H+1)
Q = [0]*(W+1)
A = [[0 for j in range(W+1)] for i in range(H+1)]
for k in range(M):
  a,b = map(int,input().split())
  P[a] += 1
  Q[b] += 1
  A[a][b] = 1
#print(P,Q,A,Pmax,Qmax,ans)
Pindex = [i for i, v in enumerate(P) if v == max(P)]
Qindex = [i for i, v in enumerate(Q) if v == max(Q)]
Pmax = P[Pindex[0]]
Qmax = Q[Qindex[0]]
ans = Pmax+Qmax-1
key = 0

Submission status: Wrong Answer

Solution code:
import numpy as np

H,W,M = map(int,input().split())
target = np.zeros(shape=(M,2))

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

"""
grid = np.zeros(shape=(H,W))

for t in target:
    h = int(t[0])-1
    w = int(t[1])-1
    grid[h,w] = 1


row_sum = grid.sum(axis=1) #行
col_sum = grid.sum(axis=0) #列
"""

row_sum = np.zeros(H)
col_sum = np.zeros(W)

for t in target:
    t -= 1
    t[0],t[1] = int(t[0]),int(t[1])
    row_sum[int(t[0])] += 1
    col_sum[int(t[1])] += 1


i = np.argmax(row_sum)

def judge(i,j):
    for t in target:
        if t[0]==i and t[1]==j:
            return True
    return False


col_idx = np.where(col_sum==col_sum.max())[0]
j = col_idx[0]
for j_ in col_idx:
    if judge(i,j_)==False:
        j = j_
        break
    
ans = row_sum[i] + col_sum[j]

if judge(i,j):
    ans -=1

print(int(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[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)

a = [tate[0]]
b = [yoko[0]]
for i in range(h-1):
    if tate[i][0] == tate[i+1][0]:
        a.append(tate[i+1])
    else:
        break
for i in range(w-1):
    if yoko[i][0] == yoko[i+1][0]:
        b.append(yoko[i+1])
    else:
        break
a = random.sample(a,len(a))
b = random.sample(b,len(b))
while i < len(a) and i <= 1800:
    j = 0
    while j < len(b) and j <= 1800:
        ans = max(ans,a[i][0]+b[j][0]-bool(format(a[i][1]+1).zfill(6)+format(b[j][1]+1).zfill(6) in exist))
        j += 1
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    H, W, M = in_nn()

    hcnt = [0] * H
    wcnt = [0] * W

    hw = []

    for i in range(M):
        h, w = in_nn()
        h, w = h - 1, w - 1
        hw.append((h, w))

    for i in range(M):
        h, w = hw[i]
        hcnt[h] += 1
        wcnt[w] += 1

    max_h = max(hcnt)
    max_w = max(wcnt)

    ans = max_h + max_w - 1

    h_index = []
    w_index = []

    for i in range(H):
        if hcnt[i] == max_h:
            h_index.append(i)

    for i in range(W):
        if wcnt[i] == max_w:
            w_index.append(i)

    hw_set = set(hw)
    # print(h_index)
    # print(w_index)

    hn = len(h_index)
    wn = len(w_index)

    f = False
    for i in range(hn):
        for j in range(wn):
            if (h_index[i], w_index[j]) not in hw_set:
                f = True

        if f or hn * i > 10**7:
            ans += 1
            break

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

hcol = [[0,i] for i in range(H)]
wcol = [[0,i] for i in range(W)]

P = set()
for i in range(M):
  h,w = map(int,readline().split())
  h -= 1
  w -= 1
  hcol[h][0] += 1
  wcol[w][0] += 1
  P.add((h,w))
  
hcol = sorted(hcol, key = lambda x:x[0], reverse = True)
wcol = sorted(wcol, key = lambda x:x[0], reverse = True)

hmax = hcol[0][0]
wmax = wcol[0][0]

ans = 0
hind = 0
wind = 0

# 交点がないものがあればhmax + wmaxが答え
found = True
while hind < H:
  while wind < W:
    val = hcol[hind][0] + wcol[wind][0]
    if (hcol[hind][1],wcol[wind][1]) in P:
      val -= 1
    else:
      found = True
      ans = val
      break
    if ans < val:
      ans = val
    wind += 1
    if wind == W or wcol[wind][0] < wmax:
      break
  if found:
    break
  hind += 1
  if hind == H or hcol[hind][0] < hmax:
    break
    
print(ans)