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:
def main():
    from operator import itemgetter
    import sys
    input = sys.stdin.readline

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

    rows = [0] * H
    cols = [0] * W
    ps = set()
    for _ in range(M):
        r, c = (int(x) - 1 for x in input().split())
        rows[r] += 1
        cols[c] += 1
        ps.add((r, c))

    erows = sorted(enumerate(rows), key=itemgetter(1), reverse=True)
    ecols = sorted(enumerate(cols), key=itemgetter(1), reverse=True)

    ans = 0
    for r, cnt_r in erows:
        for c, cnt_c in ecols:
            s = cnt_r + cnt_c
            if ans >= s:
                print(ans)
                return
            cond = (r, c) in ps
            ans = max(ans, s - int(cond))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
H,W,M = map(int,input().split())
YX = [tuple(map(int,input().split())) for i in range(M)]
yxs = set(YX)

from collections import Counter
xc = Counter()
yc = Counter()
for y,x in YX:
    yc[y] += 1
    xc[x] += 1

ans = 0
for y,x in YX:
    ans = max(ans, yc[y] + xc[x] - 1)

for xk,xv in xc.most_common()[:500]:
    for yk,yv in yc.most_common()[:500]:
        if (yk,xk) in yxs: continue
        ans = max(ans, yv + xv)
print(ans)

Submission status: Wrong Answer

Solution code:
import collections

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

my_s = set()

h_ls = []
w_ls = []

for _ in range(m):
    h_e, w_e = map(int, input().split())
    my_s.add((h_e, w_e))
    h_ls.append(h_e)
    w_ls.append(w_e)

h_c = collections.Counter(h_ls)
w_c = collections.Counter(w_ls)

h_ls = h_c.most_common()
w_ls = w_c.most_common()

h_ptr = 0
w_ptr = 0
ret = 0

h_idx, h_cnt = h_ls[0]
w_idx, w_cnt = w_ls[0]
tmp = h_cnt + w_cnt
flag = False
if (h_idx, w_idx) not in my_s:
    ret = tmp
else:
    for i, item in enumerate(h_ls):
        h_cnt_tmp, _ = item
        if h_cnt_tmp != h_cnt:
            h_ptr = i - 1

    for i, item in enumerate(w_ls):
        w_cnt_tmp, _ = item
        if w_cnt_tmp != w_cnt:
            w_ptr = i - 1

    for i in range(h_ptr):
        for j in range(w_ptr):
            _, h_idx = h_ls[i]
            _, w_idx = w_ls[j]
            if (h_idx, w_idx) not in my_s:
                ret = tmp
                flag = True
                break
        if flag:
            break
    ret = tmp - 1

print(ret)

Submission status: Wrong Answer

Solution code:
h,w,m=map(int,input().split())
a=dict()
b=dict()
points=set()
for i in range (m):
    x,y=map(int,input().split())
    points.add((x,y))
    if x in a:
        a[x]+=1
    else:
        a[x]=1
    if y in b:
        b[y]+=1
    else:
        b[y]=1
m1=max(a.values())
m2=max(b.values())
s1=set()
s2=set()
for i in a:
    if a[i]==m1:
        s1.add(i)
for i in b:
    if b[i]==m2:
        s2.add(i)
r=set()
for i in points:
    if i[0] in s1 and i[1] in s2:
        r.add(i)
if len(r)==len(s1)*len(s2):
    print(m1*m2-1)
else:
    print(m1*m2)

Submission status: Wrong Answer

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

h, w, m = map(int,input().split())
C = [list(map(int,input().split())) for i in range(m)]
CC = [[] for i in range(h)]
for i in range(m):
    CC[C[i][0]-1].append(C[i][1]-1)

for i in range(h):
    CC[i].sort()

A = [0] * h
B = [0] * w
for i in range(m):
    A[C[i][0]-1] += 1
    B[C[i][1]-1] += 1

# ans = 0
# for i in range(m):
#     ans = max(ans, A[C[i][0]-1] + B[C[i][1]-1] - 1)

D = [[A[i], i] for i in range(h)]
E = [[B[i], i] for i in range(w)]
D.sort(reverse=True)
E.sort(reverse=True)

# print(D,E)

from bisect import bisect
if not ([D[0][1]+1, E[0][1]+1] in C):
    # print("r1")
    ans = D[0][0] + E[0][0]
else:
    ans = 0
    aa = D[0][0] + E[0][0]
    co = 0
    F = []

    for i in range(h):
        for j in range(w):
            if D[i][0] + E[j][0] == aa:
                F.append([D[i][1], E[j][1]])
                co += 1
            else:
                break
        if co > m:
            break
    if co > m:
        # print("r2")
        ans = D[0][0] + E[0][0]
    else:
        ha = 0
        # print(F)
        # print(CC)
        for i in range(len(F)):
            ind = bisect(CC[F[i][0]], F[i][1])
            if ind >= 1 and CC[F[i][0]][ind-1] != F[i][1]:
                # print(CC[ind-1], F[i][1])
                ha = 1
                break
        if ha == 1:
            # print("r3")
            ans = D[0][0] + E[0][0]
        else:
            # print("m1")
            ans = D[0][0] + E[0][0] - 1
print(ans)

Submission status: Wrong Answer

Solution code:
H,W,M = map(int,input().split())
YX = [tuple(map(int,input().split())) for i in range(M)]
yxs = set(YX)

from collections import Counter
xc = Counter()
yc = Counter()
for y,x in YX:
    yc[y] += 1
    xc[x] += 1

ans = 0
for y,x in YX:
    ans = max(ans, yc[y] + xc[x] - 1)

for xk,xv in xc.most_common()[:1000]:
    for yk,yv in yc.most_common()[:1000]:
        if (yk,xk) in yxs: continue
        ans = max(ans, yv + xv)
print(ans)

Submission status: Wrong Answer

Solution code:
import collections
import numpy as np

h, w, m = map(int, input().split())
lst_ = []
for i in range(m):
    h, m = map(int, input().split())
    lst_.append([h, m])

lst = np.array(lst_)
h_lst = collections.Counter(lst[:,0])
w_lst = collections.Counter(lst[:,1])

h_most = h_lst.most_common()[0]
w_most = w_lst.most_common()[0]

#print("bomber :", h_most[0], w_most[0])
ans = h_most[1] + w_most[1]
if [h_most[0], w_most[0]] in lst_:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
H,W,M = map(int,input().split())
YX = [tuple(map(int,input().split())) for i in range(M)]
yxs = set(YX)

from collections import Counter
xc = Counter()
yc = Counter()
for y,x in YX:
    yc[y] += 1
    xc[x] += 1

ans = 0
for y,x in YX:
    ans = max(ans, yc[y] + xc[x] - 1)

for xk,xv in xc.most_common()[:2000]:
    for yk,yv in yc.most_common()[:2000]:
        if (yk,xk) in yxs: continue
        ans = max(ans, yv + xv)
print(ans)

Submission status: Wrong Answer

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

    bn_h = {}
    bn_v = {}

    for i in range(M):
        h, w = map(int, input().split())
        bn_h.setdefault(h, set())
        bn_h[h].add(w)
        bn_v.setdefault(w, set())
        bn_v[w].add(h)

    b1 = sorted(bn_h.items(), key = lambda x:len(x[1]), reverse=True)
    b2 = sorted(bn_v.items(), key = lambda x:len(x[1]), reverse=True)

    m1 = len(b1[0][1])
    m2 = len(b2[0][1])

    max_result = 0
    for i in b1:
        h, sh = i
        if len(sh) < m1:
            break
        for j in b2:
            v, sv = j
            if len(sv) < m2:
                break
            result = len(sh) + len(sv) - 1
            if h not in sv:
                result += 1
            flag = True
            max_result = max(max_result, result)
            if flag == True:
                break
        if flag == True:
            break
    print(max_result)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import collections
a = list(map(int,input().split()))
h = a[0]
w = a[1]
m = a[2]

xy = [map(int, input().split()) for _ in range(m)]
x, y = [list(i) for i in zip(*xy)]
xc=collections.Counter(x)
yc=collections.Counter(y)
x_m = xc.most_common()[0]
y_m = yc.most_common()[0]

wa = x_m[1]+y_m[1]


for i in  range(m):
  if x[i]==x_m[0] and y[i]==y_m[0]:
    wa=wa-1
    
print(wa)

Submission status: Wrong Answer

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


row = [set()for _ in range(h)]
col = [set()for _ in range(w)]
for _ in range(m):
    x, y = map(lambda x: int(x)-1, input().split())
    row[x].add(y)
    col[y].add(x)

crow = 0
ic = None
for i, x in enumerate(row):
    if crow < len(x):
        crow = len(x)
        ic = i

ccol = 0
jc = None
for j, x in enumerate(col):
    if ccol < len(x):
        ccol = len(x)
        jc = j

if j in row[i]:
    print(crow+ccol-1)
else:
    print(crow+ccol)

Submission status: Wrong Answer

Solution code:
import math
import collections

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

c_x = collections.Counter(x)
c_y = collections.Counter(y)

_max_bomb = 0
unko = min(m, 10)
ys = c_y.most_common()[0:unko]
for x, x_num in c_x.most_common()[0:unko]:
  for y, y_num in ys:
    if [x, y] in bomb:
      _max_bomb = max(_max_bomb, x_num+y_num-1)
    else:
      _max_bomb = max(_max_bomb, x_num+y_num)
      
print(_max_bomb)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m,targets = map(int,input().split())
r,c = {},{}
rcnt,ccnt = Counter(),Counter()
for _ in range(targets):
    i,j = map(int,input().split())
    rcnt[i] += 1
    ccnt[j] += 1
    if i in r:
        r[i].append(j)
    else:
        r[i] = [j]
    if j in c:c[j]+=1
    else:c[j]=1
#print(r,c)
mx = 0
for key in r.keys():
    arr = r[key]
    l = len(arr)
    for v in arr:
        cand = l + c[v] - 1
        mx = max(cand,mx)
rcnt = [(k,v) for (k,v) in rcnt.items()]
ccnt = [(k,v) for (k,v) in ccnt.items()]

rcnt.sort(key = lambda x:x[1],reverse = True)
ccnt.sort(key = lambda x:x[1],reverse = True)
rcnt = rcnt[:30]
ccnt = ccnt[:30]
#print(rcnt,ccnt)
for row in rcnt:
    for col in ccnt:
        rowk,colk = row[0],col[0]
        if rowk in r and colk in c and colk not in r[rowk]:
            cand = len(r[rowk]) + c[colk]
            mx = max(cand,mx)
print(mx)

Submission status: Wrong Answer

Solution code:
H,W,M = map(int,input().split())
ch,cw = [0 for i in range(H)],[0 for i in range(W)]
mapp = [[0 for i in range(H)] for i in range(W)]

for i in range(M):
    h,w = map(int,input().split())
    ch[h-1] += 1
    cw[w-1] += 1
    mapp[w-1][h-1]=1



mh = max(ch)
mw = max(cw)

fh = []
fw = []

for i in range(H):
    if ch[i] == mh:
        fh.append(i)

for i in range(W):
    if cw[i] == mw:
        fw.append(i)

r = mw+mh-1

for i in fw:
    for j in fh:
        if mapp[i][j] == 0:
            r = r + 1
            break

print(r)

Submission status: Wrong Answer

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

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

hhh = max(h)
www = max(w)

print(hhh + www)

Submission status: Wrong Answer

Solution code:
h,w,m=map(int,input().split())
e1=[0]*h
e2=[0]*w
f=[]
for c in range(m):
    a,b=map(int,input().split())
    f.append([a-1,b-1])
    e1[a-1]+=1
    e2[b-1]+=1
p=max(e1)
q=max(e2)
r=0
if [p,q] in f:
    r=1
print(p+q-r)

Submission status: Wrong Answer

Solution code:
import collections

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

my_s = set()

h_ls = []
w_ls = []

for _ in range(m):
    h_e, w_e = map(int, input().split())
    my_s.add((h_e, w_e))
    h_ls.append(h_e)
    w_ls.append(w_e)

h_c = collections.Counter(h_ls)
w_c = collections.Counter(w_ls)

h_ls = h_c.most_common()
w_ls = w_c.most_common()

h_ptr = 0
w_ptr = 0
ret = 0

h_idx, h_cnt = h_ls[0]
w_idx, w_cnt = w_ls[0]
tmp = h_cnt + w_cnt
flag = False
if (h_idx, w_idx) not in my_s:
    ret = tmp
else:
    for i, item in enumerate(h_ls):
        h_cnt_tmp, _ = item
        if h_cnt_tmp != h_cnt:
            h_ptr = i - 1

    for i, item in enumerate(w_ls):
        w_cnt_tmp, _ = item
        if w_cnt_tmp != w_cnt:
            w_ptr = i - 1

    for i in range(h_ptr):
        for j in range(w_ptr):
            print(i, j)
            _, h_idx = h_ls[i]
            _, w_idx = w_ls[j]
            if (h_idx, w_idx) not in my_s:
                ret = tmp
                flag = True
                break
        if flag:
            break
    ret = tmp - 1

print(ret)

Submission status: Wrong Answer

Solution code:
H,W,M = map(int,input().split())
YX = [tuple(map(int,input().split())) for i in range(M)]
yxs = set(YX)

from collections import Counter
xc = Counter()
yc = Counter()
for y,x in YX:
    yc[y] += 1
    xc[x] += 1

ans = 0
for y,x in YX:
    ans = max(ans, yc[y] + xc[x] - 1)

for xk,xv in xc.most_common()[:3000]:
    for yk,yv in yc.most_common()[:3000]:
        if (yk,xk) in yxs: continue
        ans = max(ans, yv + xv)
print(ans)

Submission status: Wrong Answer

Solution code:
H,W,M=map(int,input().split())
from collections import defaultdict
dw = defaultdict(list)
dh = defaultdict(list)
for m in range(M):
  h,w=map(int,input().split())
  dh[h].append(w)
  dw[w].append(h)
kh,Lh=1,0
for h in dh.keys():
  if len(dh[h])>Lh:
    Lh=len(dh[h])
    kh=h
K=dh[kh]
Lw=0
a=0
for w in dw.keys():
  if kh in dw[w]:
    a=1
  else:
    a=0
  if len(dw[w])-a>Lw:
    Lw=len(dw[w])-a
print(Lh+Lw)

Submission status: Wrong Answer

Solution code:

def get_count(li,c):
    if(li==[]):
        return ((0,0))
    m=(li[0][c],1)
    h={}
    h[li[0][c]]=1
    for i in li[1:]:
        if(i[c] in h):
            h[i[c]]+=1
        else:
            h[i[c]]=1
        if(m[1]<h[i[c]]):
            m=(i[c],h[i[c]])
    return m


h,w,n=list(map(int,input().split()))

if(h==1 or w==1 or n<=1):
    li = []
    for i in range(n):
        li.append(tuple(map(int, input().split())))
else:
    li=[]
    for i in range(n):
        li.append(tuple(map(int,input().split())))
    li=list(set(li))
    r=get_count(li,0)
    c=get_count(li,1)
    # print(r,c)
    s=0
    if(r[1]>c[1]):
        temp=[]
        for i in li:
            if(i[0]!=r[0]):
                temp.append(i)
        s+=r[1]+get_count(temp,1)[1]
    else:
        temp=[]
        for i in li:
            if(i[1]!=c[0]):
                temp.append(i)
        s+=c[1]+get_count(temp,0)[1]

    print(s)

Submission status: Wrong Answer

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

for y,x in lis:
    exist.append(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 = 1
if m >= 2:ans = 2

  # print(tate)
  # print(yoko)

for i in range(min(h,30)):
    for j in range(min(w,30)):
        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))
        if j == w-1:
              # print(j,w-1,yoko[j][0],yoko[j+1][0])
            break
    if i == h-1:
        print(ans)
        exit()

Submission status: Wrong Answer

Solution code:
h,w,z = map(int, input().split())
#from sys import stdin
hlist = [0] * h
wlist = [0] * w
hyou=[]
for _ in range(z):
    x, y = map(int, input().split())
    hyou.append([x,y])
    hlist[x-1]+=1
    wlist[y-1]+=1

hmax_i=[i for i, v in enumerate(hlist) if v == max(hlist)]
wmax_i=[i for i, v in enumerate(wlist) if v == max(wlist)]
flag=0

for i in hmax_i:
    if flag==0:
        for j in wmax_i:
            if [i+1,j+1] in hyou:
                True
            else:
                print(i+1,j+1)
                flag=1
                break
    else:
        break
if flag==1:
    print(max(hlist)+max(wlist))
else:
    print(max(hlist)+max(wlist)-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for y,x in lis:
    exist.append(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 = 1
if m >= 2:ans = 2

  # print(tate)
  # print(yoko)

for i in range(min(h,5)):
    for j in range(min(w,5)):
        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))
        if j == w-1:
              # print(j,w-1,yoko[j][0],yoko[j+1][0])
            break
    if i == h-1:
        print(ans)
        exit()

Submission status: Wrong Answer

Solution code:
import math
import collections

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

c_x = collections.Counter(x)
c_y = collections.Counter(y)

_max_bomb = 0
unko = min(m, 10)
ys = c_y.most_common()[0:unko]
for x, x_num in c_x.most_common()[0:unko]:
  for y, y_num in ys:
    print(x,y, x_num+y_num)
    if [x, y] in bomb:
      _max_bomb = max(_max_bomb, x_num+y_num-1)
    else:
      _max_bomb = max(_max_bomb, x_num+y_num)
      
print(_max_bomb)

Submission status: Wrong Answer

Solution code:
h,w,m=map(int,input().split())
X=[list(map(int,input().split())) for _ in range(m)]
Y=[]
L=[]
ans=[]

for i in range(h):
    for j in range(w):
        L.append([i,j])

def get_unique_list(seq):
    seen = []
    return [x for x in seq if x not in seen and not seen.append(x)]

for i in range(h*w):
    for j in range(m):
        if L[i][0]==X[j][0]:
            Y.append(X[j])
    for j in range(m):
        if L[i][1]==X[j][1]:
            Y.append(X[j])
    ans.append(len(get_unique_list(Y)))
print(max(ans))

Submission status: Wrong Answer

Solution code:
h,w,m= map(int,input().split(" "))
r,c=[0 for i in range(h)],[ 0 for i in range(w)]
g=[[0 for i in range(w)] for j in range(h)]
for i in range(m):
    x,y= map(int,input().split(" "))
    r[x-1]+=1
    c[y-1]+=1
    g[x-1][y-1]=1
mx=max(r)
q,t=[],[]
for i in range(h):
    if r[i]==mx:
        q.append(i)
m=max(c)
for j in range(w):
    if c[j]==m:
        t.append(j)

s=mx+m-1
for u in q:
    for v in t:
        if g[u][v]==0:
            break
print(s)

Submission status: Wrong Answer

Solution code:
import collections
import numpy as np

h, w, m = map(int, input().split())
lst_ = []
for i in range(m):
    h, m = map(int, input().split())
    lst_.append([h, m])

lst = np.array(lst_)
h_lst = collections.Counter(lst[:,0])
w_lst = collections.Counter(lst[:,1])

h_most = h_lst.most_common()[0]
w_most = w_lst.most_common()[0]

print("bomber :", h_most[0], w_most[0])
ans = h_most[1] + w_most[1]
if [h_most[0], w_most[0]] in lst_:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
h,w,m = map(int,input().split())
ls1 = [0 for i in range(h)]
ls2 = [0 for i in range(w)]
ls = []
for i in range(m):
    H,W = map(int,input().split())
    ls.append([H-1,W-1])
    ls1[H-1] += 1
    ls2[W-1] += 1
k = [i for i, v in enumerate(ls1) if v == max(ls1)]
l = [i for i, v in enumerate(ls2) if v == max(ls2)]
maximum = 0
ans = 0
for i in range(len(k)):
    for j in range(len(l)):
        ans = 0
        for o in range(m):
            if ls[o][0] == k[i]:
                ans += 1
            elif ls[o][1] == l[j]:
                ans += 1
        if maximum < ans: maximum = ans
print(ans)

Submission status: Wrong Answer

Solution code:
r=input().split()
H=int(r[0])
W=int(r[1])
M=int(r[2])
d=[input().split() for i in range(M)]
D=[]
for i in range(M):
    D_pre=[int(s) for s in d[i]]
    D.append(D_pre)
gyou=[0]*H
retu=[0]*W
for i in range(M):
    gyou[D[i][0]-1]+=1
    retu[D[i][1]-1]+=1
g_Max=max(gyou)
r_Max=max(retu)
g=[]
r=[]
for i in range(H):
    if gyou[i]==g_Max:
        g.append(i)
for i in range(W):
    if retu[i]==r_Max:
        r.append(i)
x=0
for i in g:
    for j in r:
        if [i+1,j+1] not in D:
            x+=1
            print(g_Max+r_Max)
            break
if x==0:
    print(g_Max+r_Max-1)

Submission status: Wrong Answer

Solution code:
h,w,m=[int(i) for i in raw_input().split()]
r=[0 for i in range(h)]
c=[0 for i in range(w)]
d={}
for i in range(m):
    h1,w1=[int(i)-1 for i in raw_input().split()]
    r[h1]+=1
    c[w1]+=1
    x=str(h1)+'#'+str(w1)
    d[x]=d.get(x,0)+1
maxx=max(r)
r1=[]
for i in range(len(r)):
    if maxx==r[i]:
        r1.append(i)
maxx=max(c)
c1=[]
for i in range(len(c)):
    if maxx==c[i]:
        c1.append(i)
maxx1=0;flag=0
for i in r1:
    for j in c1:
        x=str(i)+'#'+str(j)
        if(d.get(x,0)==0):
            maxx1=r[i]+c[j]
            flag=1
        else:
            maxx1=r[i]+c[j]-d[x]
    if flag:
        break
print maxx1

Submission status: Wrong Answer

Solution code:
import sys
import math
H, W, M = map(int, input().split())
MASU = [[0 for i in range(W)]for j in range(H)]
hcount = [0]*H
wcount = [0]*W
for i in range(M):
    h, w = map(int, input().split())
    MASU[h-1][w-1] = 1
    hcount[h-1] += 1
    wcount[w-1] += 1
hmax = max(hcount)
wmax = max(wcount)
ans = hmax+wmax

print(ans)

Submission status: Wrong Answer

Solution code:
H,W,M = map(int,input().split())
Hlist= {}
Wlist ={}
HWlist = []
for i in range(M):
    h,w = map(int,input().split())
    if h not in Hlist:
        Hlist[h] = 1
    else:
        Hlist[h] += 1
    if w not in Wlist:
        Wlist[w] = 1
    else:
        Wlist[w] += 1
    HWlist.append([h,w])
h = max(Hlist)
w = max(Wlist)
Answer = max(Hlist.values())+max(Wlist.values())
if [h,w] in HWlist:
    Answer -= 1
print(Answer)

Submission status: Wrong Answer

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

h, w, m = map(int,input().split())
C = [list(map(int,input().split())) for i in range(m)]
CC = [[] for i in range(h)]
for i in range(m):
    CC[C[i][0]-1].append(C[i][1]-1)

for i in range(h):
    CC[i].sort()

A = [0] * h
B = [0] * w
for i in range(m):
    A[C[i][0]-1] += 1
    B[C[i][1]-1] += 1

# ans = 0
# for i in range(m):
#     ans = max(ans, A[C[i][0]-1] + B[C[i][1]-1] - 1)

D = [[A[i], i] for i in range(h)]
E = [[B[i], i] for i in range(w)]
D.sort(reverse=True)
E.sort(reverse=True)

# print(D,E)

from bisect import bisect
if not [D[0][1]+1, E[0][1]+1] in C:
    # print("r1")
    ans = D[0][0] + E[0][0]
else:
    ans = 0
    aa = D[0][0] + E[0][0]
    co = 0
    F = []

    for i in range(h):
        for j in range(w):
            if D[i][0] + E[j][0] == aa:
                F.append([D[i][1], E[j][1]])
            else:
                break
        if co > m:
            break
    if co > m:
        # print("r2")
        ans = D[0][0] + E[0][0]
    else:
        ha = 0
        # print(F)
        # print(CC)
        for i in range(len(F)):
            ind = bisect(CC[F[i][0]], F[i][1])
            if ind >= 1 and CC[F[i][0]][ind-1] != F[i][1]:
                # print(CC[ind-1], F[i][1])
                ha = 1
                break
        if ha == 1:
            # print("r3")
            ans = D[0][0] + E[0][0]
        else:
            # print("m1")
            ans = D[0][0] + E[0][0] - 1
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter, defaultdict, deque

R, C, M = [int(x) for x in input().split()]
targets = [tuple(int(x) - 1 for x in input().split()) for i in range(M)]  # 0 indexed

mostByRow = Counter(r for r, c in targets)
mostByCol = Counter(c for r, c in targets)

r = max(mostByRow.keys(), key=lambda r: mostByRow[r])
c = max(mostByCol.keys(), key=lambda c: mostByCol[c])

ans = mostByRow[r] + mostByCol[c]
if (r, c) in targets:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
input = lambda : sys.stdin.readline().rstrip()

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

xs = []
ys = []
for i in range(m):
    x, y = map(int, input().split(" "))
    xs.append(x)
    ys.append(y)

xcounter = Counter(xs)
ycounter = Counter(ys)

x_most, x_most_num = xcounter.most_common(1)[0]
y_most, y_most_num = ycounter.most_common(1)[0]

out = 0
if x_most_num < y_most_num:
    x_most_num = y_most_num
    tmp = ys
    ys = xs
    xs = tmp

out = x_most_num

if x_most_num < m:
    y2s = [] 
    for i in range(m):
        if xs[i] == x_most:
            continue
        else:  
            y2s.append(ys[i])
        
    y2counter = Counter(y2s)
    y2_most, y2_most_num = y2counter.most_common(1)[0]
    out += y2_most_num

print(out)

Submission status: Wrong Answer

Solution code:
import math
import collections

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

c_x = collections.Counter(x)
c_y = collections.Counter(y)

_max_bomb = 0
unko = min(m, 5)
ys = c_y.most_common()[0:unko]
for x, x_num in c_x.most_common()[0:unko]:
  for y, y_num in ys:
    if [x, y] in bomb:
      _max_bomb = max(_max_bomb, x_num+y_num-1)
    else:
      _max_bomb = max(_max_bomb, x_num+y_num)
      
print(_max_bomb)

Submission status: Wrong Answer

Solution code:
h,w,m=map(int, input().split())
H=[0]*h
W=[0]*w
for i in range(m):
	a,b=map(int, input().split())
	H[a-1]+=1
	W[b-1]+=1

print(max(H)+max(W)-1)

Submission status: Wrong Answer

Solution code:
import sys
import collections

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))

h,w,m=rm()
lih = []
liw = []
for _ in range(m):
  a,b=rm()
  lih.append(a)
  liw.append(b)
ch=collections.Counter(lih)
cw=collections.Counter(liw)
lish=sorted(ch.items(),key=lambda x:x[1])
lisw=sorted(cw.items(),key=lambda x:x[1])
a=lish[-1][0]
b=lisw[-1][0]
cnt=lish[-1][1]+lisw[-1][1]
for i,j in zip(lih,liw):
  if i==a and j==b:
    cnt-=1
print(cnt)

Submission status: Wrong Answer

Solution code:
h,w,m=[int(i) for i in raw_input().split()]
r=[0 for i in range(h)]
c=[0 for i in range(w)]
d={}
for i in range(m):
    h1,w1=[int(i)-1 for i in raw_input().split()]
    r[h1]+=1
    c[w1]+=1
    x=str(h1)+'#'+str(w1)
    d[x]=1
maxx=max(r)
r1=[]
for i in range(len(r)):
    if maxx==r[i]:
        r1.append(i)
maxx=max(c)
c1=[]
for i in range(len(c)):
    if maxx==c[i]:
        c1.append(i)
maxx1=0;flag=0
for i in r1:
    for j in c1:
        x=str(i)+'#'+str(j)
        if(d.get(x,0)==0):
            maxx1=r[i]+c[j]
            flag=1
        else:
            maxx1=r[i]+c[j]-1
    if flag:
        break
print maxx1

Submission status: Wrong Answer

Solution code:
H,W,M=map(int, input().split())
nrow=[0]*H
ncol=[0]*W
xcoord=[]
ycoord=[]

for _ in range(M):
	h,w=map(int, input().split())
	nrow[h-1]+=1
	xcoord+=[w-1]
	ycoord+=[h-1]

y_ans=nrow.index(max(nrow)) #y coord of best target
for i in range(M):
	if ycoord[i]!=y_ans:
		ncol[xcoord[i]]+=1

print(max(nrow)+max(ncol))



# 2darray [[0] * 4 for i in range(3)]
# import itertools

Submission status: Wrong Answer

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

def main():
    H, W, M = map(int,input().split())
    
    d_row = defaultdict(int)
    d_col = defaultdict(int)
    d_rc = defaultdict(int)

    for _ in range(M):
        h, w = map(int, input().split())
        h -= 1
        w -= 1
        d_row[h] += 1
        d_col[w] += 1
        d_rc[(h, w)] = True

    list_row = list(d_row.items())
    list_col = list(d_col.items())

    list_row.sort(key=lambda x: x[1], reverse=True)
    list_col.sort(key=lambda x: x[1], reverse=True)

    if len(list_row) >= 2 and len(list_col) >= 2:
        ans = list_row[0][1] + list_col[0][1] - d_rc[(list_row[0][0], list_col[0][0])]
        ans = max(ans, list_row[1][1] + list_col[0][1] - d_rc[(list_row[1][0], list_col[0][0])])
        ans = max(ans, list_row[0][1] + list_col[1][1] - d_rc[(list_row[0][0], list_col[1][0])])
        ans = max(ans, list_row[1][1] + list_col[1][1] - d_rc[(list_row[1][0], list_col[1][0])])
    elif len(list_row) >= 2:
        ans = list_row[0][1] + list_col[0][1] - d_rc[(list_row[0][0], list_col[0][0])]
        ans = max(ans, list_row[1][1] + list_col[0][1] - d_rc[(list_row[1][0], list_col[0][0])])
    elif len(list_col) >= 2:
        ans = list_row[0][1] + list_col[0][1] - d_rc[(list_row[0][0], list_col[0][0])]
        ans = max(ans, list_row[0][1] + list_col[1][1] - d_rc[(list_row[0][0], list_col[1][0])])
    else:
        ans = list_row[0][1] + list_col[0][1] - d_rc[(list_row[0][0], list_col[0][0])]

    print(ans)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
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)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    h,w,m = LI()
    fx = [0]*w
    fy = [0]*h
    f = defaultdict(lambda : 0)
    for _ in range(m):
        y,x = LI()
        y -= 1
        x -= 1
        f[(y,x)] = 1
        fx[x] += 1
        fy[y] += 1
    argx = 0
    m = 0
    for x in range(w):
        srx = fx[x]
        if m < srx:
            m = srx
            argx = x

    argy = 0
    M = 0
    for y in range(h):
        srx = fy[y]
        if M < srx:
            M = srx
            argy = y

    print(m+M-f[(y,x)])
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

h, w, m = map(int, input().split())
HW = [list(map(int, input().split())) for _ in range(m)]

hd = defaultdict(int)
wd = defaultdict(int)

for i, j in HW:
  hd[i] += 1
  wd[j] += 1

hd = list(hd.items())
wd = list(wd.items())

hd.sort(key=lambda x: -x[1])
wd.sort(key=lambda x: -x[1])

maxhi = hd[0][0]
maxwi = wd[0][0]

if [maxhi, maxwi] in HW:
  ans = hd[0][1] + wd[0][1] - 1
else:
  ans = hd[0][1] + wd[0][1]
  
print(ans)