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: Runtime Error

Solution code:
# Fast IO (be careful about bytestring)

# import os,io
# input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline


H,W,M = map(int,input().split())
targetList = []
rowCount = [0] * H
columnCount = [0] * W
for _ in range(M):
    p,q = map(int,input().split())
    targetList.append((p-1,q-1))
    rowCount[p-1] += 1
    columnCount[q-1] += 1
rowCountLabelled = []
columnCountLabelled = []
for i in range(H):
    rowCountLabelled.append((rowCount[i],i))
for i in range(W):
    columnCountLabelled.append((columnCount[i],i))
rowCountLabelled.sort(reverse = True)
columnCountLabelled.sort(reverse = True)
targetList.sort()
maxi = rowCountLabelled[0][0] + columnCountLabelled[0][0]
isMaxAchievable = False
for elem1 in rowCountLabelled:
    if elem1[0] != rowCountLabelled[0][0]:
        break
    for elem2 in columnCountLabelled:
        if elem2[0] != columnCountLabelled[0][0]:
            break
        target = (elem1[1],elem2[1])
        l = 0
        r = len(targetList)
        while l < r:
            m = (l + r) // 2
            if target == targetList[m]:
                l = m
                r = m
                break
            elif target < targetList[m]:
                r = m-1
            elif target > targetList[m]:
                l = m + 1
        if target != targetList[l] and target != targetList[r]:
            isMaxAchievable = True
            break
    if isMaxAchievable:
        break
if isMaxAchievable:
    print(maxi)
else:
    print(maxi - 1)

Submission status: Runtime Error

Solution code:

def get_count(li,c):
    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()))
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: Runtime Error

Solution code:
import numpy as np
H,W,M=map(int,input().split())

h=[0]*(3*10**5)
w=[0]*(3*10**5)
l=[]
for i in range(M):
  a,b=map(int,input().split())
  h[a]+=1
  w[b]+=1
  l.append([a,b])
h=np.array(h)
w=np.array(w)
hm=np.argmax(h)
wm=np.argmax(w)
dup=0
for a,b in l:
  if a==hm and b==wm:
    dup+=1
print(max(h)+max(w)-dup)

Submission status: Runtime Error

Solution code:
import sys
from collections import Counter

stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    h, w, m = nm()
    H = []
    W = []
    for i in range(m):
        hh, ww = nm()
        H.append(hh)
        W.append(ww)
    h_m = Counter(H).most_common()[0]
    w_m = Counter(W).most_common()[0]
    ans_1 = h_m[1]
    ans_2 = w_m[1]
    WW = []
    HH = []

    for i in range(m):
        if H[i] == h_m[0]:
            continue
        WW.append(W[i])

    for i in range(m):
        if W[i] == w_m[0]:
            continue
        HH.append(H[i])

    ans_1 += Counter(WW).most_common()[0][1]
    ans_2 += Counter(HH).most_common()[0][1]

    print(max(ans_1, ans_2))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))
import numpy as np

h,w,m=nii()

h_b=[0 for i in range(h)]
w_b=[0 for i in range(w)]

l=[]
for i in range(m):
  h,w=nii()
  h-=1
  w-=1
  h_b[h]+=1
  w_b[w]+=1
  l.append([h,w])

h_s=np.argsort(h_b)
w_s=np.argsort(w_b)

ans=0
for i in range(min(100,len(h_s))):
  for j in range(min(100,len(w_s))):
    t_ans=h_b[i]+w_b[j]
    num=l.count([i,j])
    t_ans-=num
    ans=max(ans,t_ans)

print(ans)

Submission status: Runtime Error

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

board = [[0] * (W + 1) for _ in range(H + 1)]

h_cnt = [0] * (H + 1)
w_cnt = [0] * (W + 1)

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

mx = 0
for h in range(1, H + 1):
    for w in range(1, W + 1):
        val = h_cnt[h] + w_cnt[w] - board[h][w]
        mx = max(mx, val)

print(mx)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
H,W,M = map(int,input().split())
bomb = [[0]*(W+1) for _ in range(H+1)]
hdic = defaultdict(int)
wdic = defaultdict(int)
for i in range(M):
    hi,wi = map(int,input().split())
    hdic[hi] += 1
    wdic[wi] += 1
    bomb[hi][wi] = 1
hmax = 0
hlist = []
wmax = 0
wlist = []
for i in hdic.keys():
    if hdic[i] > hmax:
        hlist = [i]
        hmax = hdic[i]
    elif hdic[i] == hmax:
        hlist.append(i)
for i in wdic.keys():
    if wdic[i] > wmax:
        wlist = [i]
        wmax = wdic[i]
    elif wdic[i] == wmax:
        wlist.append(i)
for i in hlist:
    for j in wlist:
        if bomb[i][j] == 0:
            print(hmax+wmax)
            exit()
print(hmax+wmax-1)

Submission status: Runtime Error

Solution code:
import collections
import sys
import math
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())
def NIJIGEN(H): return [list(input()) for i in range(H)]
H,W,M=map(int,input().split())
A=list()
B=list()
for i in range(M):
  a,b=map(int,input().split())
  A.append(a)
  B.append(b)
c = collections.Counter(A)
s=c.most_common()
k=s[0][0]
t=s[0][1]
L=list()
for i in range(M):
  if A[i]!=k:
    L.append(B[i])
c = collections.Counter(L)
s=c.most_common()
ans=s[0][1]+t

c = collections.Counter(B)
s=c.most_common()
k=s[0][0]
t=s[0][1]
L=list()
for i in range(M):
  if B[i]!=k:
    L.append(A[i])
c = collections.Counter(L)
s=c.most_common()
ans=max(ans,s[0][1]+t)
print(ans)