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: Accepted

Solution code:
h,w,m=map(int,input().split())
h_=[0]*(h+1)
w_=[0]*(w+1)
plot=set()
for _ in range(m):
    x,y=map(int,input().split())
    h_[x]+=1
    w_[y]+=1
    plot.add((x,y))
x=max(h_)
y=max(w_)
a=[i for i,v in enumerate(h_) if v==x]
b=[j for j,q in enumerate(w_) if q==y]
ans=x+y-1
c=False
for i in a:
    for j in b:
        if (i,j) not in plot:
          c=True
          break
    if c:
      break
if c:
  print(ans+1)
else:
  print(ans)

Submission status: Accepted

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

import collections
import sys
input=sys.stdin.readline

h,w,m=map(int,input().split())
items=set()
count_h=[0]*(h+1)
count_w=[0]*(w+1)
for _ in range(m):
    y,x=map(int,input().split())
    items.add((y,x))
    count_h[y]+=1
    count_w[x]+=1
max_h=max(count_h)
max_w=max(count_w)
cand_h=[]
cand_w=[]
for y in range(1,h+1):
  if count_h[y]==max_h:
    cand_h.append(y)
for x in range(1,w+1):
  if count_w[x]==max_w:
    cand_w.append(x)
ans=max_h+max_w
for y in cand_h:
  for x in cand_w:
    if (y,x) not in items:
      print(ans)
      exit()
else:
  print(ans-1)

Submission status: Accepted

Solution code:
H,W,M=map(int,input().split())
Y=[0 for i in range(H+1)]
X=[0 for i in range(W+1)]
HW=[]
for i in range(M):
    h,w=map(int,input().split())
    Y[h]+=1
    X[w]+=1
    HW.append([h,w])

LY=[]
LX=[]
MY=max(Y)
MX=max(X)
for i in range(H+1):
    if Y[i]==MY:
        LY.append(i)
for i in range(W+1):
    if X[i]==MX:
        LX.append(i)

count=0
for i in range(M):
    if Y[HW[i][0]]==MY and X[HW[i][1]]==MX:
        count+=1

if count==len(LY)*len(LX):
    print(MY+MX-1)
else:
    print(MY+MX)

Submission status: Accepted

Solution code:
H, W, M = map(int, input().split())
HW_L = set(tuple(int(x)-1 for x in input().split()) for y in range(M))

w_l = [0] * W
h_l = [0] * H

for _hi, _wi in HW_L:
    w_l[_wi] += 1
    h_l[_hi] += 1

max_h = max(h_l)
max_w = max(w_l)

h_idx = [i for i, x in enumerate(h_l) if x == max_h]
w_idx = [i for i, x in enumerate(w_l) if x == max_w]

ans = max_h + max_w - 1

for hi in h_idx:
    for wj in w_idx:
        if (hi, wj) not in HW_L:
            ans = max_h + max_w
            break
    else:
        continue
    break

print(ans)

Submission status: Accepted

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

coord = set()
num_h = [0] * (H+1)
num_w = [0] * (W+1)
for i in range(M):
    h, w = map(int, input().split())
    num_h[h] += 1; num_w[w] += 1
    coord.add((h, w))

max_h = []
max_w = []
nh = nw = 0
for h in range(H+1):
    n = num_h[h]
    if n > nh:
        nh = n
        max_h = [h]
    elif n == nh:
        max_h.append(h)

for w in range(W+1):
    n = num_w[w]
    if n > nw:
        nw = n
        max_w = [w]
    elif n == nw:
        max_w.append(w)

for h in max_h:
    for w in max_w:
        if (h, w) not in coord:
            print(nh + nw)
            quit()

print(nh + nw - 1)

Submission status: Accepted

Solution code:
import collections

h,w,m = map(int,input().split())
l=[]
x=[]
y=[]
for i in range(m):
    hw=list(map(int,input().split()))
    l.append(hw)
    y.append(hw[0])
    x.append(hw[1])

xc = collections.Counter(x)
yc = collections.Counter(y)

tuplex = xc.most_common()[0]
tupley = yc.most_common()[0]

mostx=tuplex[0]
mosty=tupley[0]

if tuplex[1]>tupley[1]:
    y=[]
    for i in range(m):
        if l[i][1]!=mostx:
            y.append(l[i][0])
    if len(y) != 0:
        yc = collections.Counter(y)
    tupley = yc.most_common()[0]
    mosty=tupley[0]
    ans=0
    for j in range(m):
        if l[j][0] == mosty or l[j][1] == mostx:
            ans+=1
    print(ans)

elif tuplex[1]<tupley[1]:
    x=[]
    for i in range(m):
        if l[i][0]!=mosty:
            x.append(l[i][1])
    if len(x) != 0:
        xc = collections.Counter(x)
    tuplex = xc.most_common()[0]
    mostx=tuplex[0]
    ans=0
    for j in range(m):
        if l[j][0] == mosty or l[j][1] == mostx:
            ans+=1
    print(ans)

else:
    #y基準
    ans1=0
    x1=[]
    for i in range(m):
        if l[i][0]!=mosty:
            x1.append(l[i][1])
    if len(x1) != 0:
        x1c = collections.Counter(x1)
    else:
        x1c = collections.Counter(x)
    tuplex1 = x1c.most_common()[0]
    mostx1=tuplex1[0]
    for j in range(m):
        if l[j][0] == mosty or l[j][1] == mostx1:
            ans1+=1
    #x基準
    ans2=0
    y1=[]
    for i in range(m):
        if l[i][1]!=mostx:
            y1.append(l[i][0])
    if len(y1) != 0:
        y1c =collections.Counter(y1)
    else:
        y1c = collections.Counter(y)
    tupley1 = y1c.most_common()[0]
    mosty1=tupley1[0]
    for j in range(m):
        if l[j][0] == mosty1 or l[j][1] == mostx:
            ans2+=1
    print(max(ans1,ans2))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.readline
    h, w, m = map(int, input().split())
    cnt_h = [0] * h
    cnt_w = [0] * w
    bomb = defaultdict(lambda : 0)
    for _ in range(m):
        y, x = map(lambda x:int(x) - 1, input().split())
        cnt_h[y] += 1
        cnt_w[x] += 1
        bomb[(y, x)] = 1
        
    kouho_h = []
    kouho_w = []
    max_h = max(cnt_h)
    max_w = max(cnt_w)
    for i in range(h):
        if cnt_h[i] == max_h:
            kouho_h.append(i)
    for i in range(w):
        if cnt_w[i] == max_w:
            kouho_w.append(i)
    
    for i in kouho_h:
        for j in kouho_w:
            if bomb[(i, j)] == False:
                print(max_h + max_w)
                exit()
    print(max_h + max_w - 1)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import Counter


def main():
    H, W, M = map(int, sys.stdin.readline().rstrip().split())
    Mh = [0] * H
    Mw = [0] * W
    B = []
    for i in range(M):
        h, w = map(int, sys.stdin.readline().rstrip().split())
        Mh[h - 1] += 1
        Mw[w - 1] += 1
        B.append((h - 1, w - 1))

    nh = max(Mh)
    nw = max(Mw)

    # 交差する場所に置かれた処理物があるか？
    lh = Counter(Mh)[nh]
    lw = Counter(Mw)[nw]

    out = 0
    for (h, w) in B:
        if Mh[h] == nh and Mw[w] == nw:
            out += 1

    if out == lh * lw:
        print(nh + nw - 1)
    else:
        print(nh + nw)


main()

Submission status: Accepted

Solution code:
import sys
H,W,M=map(int, input().split())
h=[]
w=[]
Hcount=[0 for _ in range(H)]
Wcount=[0 for _ in range(W)]
B=set()

for i in range(M):
    hi,wi=map(int, input().split())
    #h.append(hi)
    #w.append(wi)
    B.add((hi,wi))
    Hcount[hi-1]+=1
    Wcount[wi-1]+=1
Hcountmax=max(Hcount)
Wcountmax=max(Wcount)
h_max=[i for i, v in enumerate(Hcount) if v == Hcountmax]
w_max=[i for i, v in enumerate(Wcount) if v == Wcountmax]
#print(h_max,w_max)
for ii in range(len(h_max)):
    for jj in range(len(w_max)):
        if (h_max[ii]+1,w_max[jj]+1) not in B:
            print(max(Hcount)+max(Wcount))
            sys.exit()
print(max(Hcount)+max(Wcount)-1)

Submission status: Accepted

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

h_dict = dict()
w_dict = dict()

targets = list()

for i in range(m):
    temp_h, temp_w, = map(int,input().split())

    h_dict[temp_h] = h_dict.get(temp_h, default=0) + 1
    w_dict[temp_w] = w_dict.get(temp_w, default=0) + 1

    targets.append((temp_h, temp_w))

h_dict_values = list(h_dict.values())
w_dict_values = list(w_dict.values())

max_h = max(h_dict_values)
max_w = max(w_dict_values)

nh = h_dict_values.count(max_h)
nw = w_dict_values.count(max_w)

x = 0

for i in range(m):
    temp_h, temp_w = targets[i]
    if h_dict[temp_h]==max_h and w_dict[temp_w]==max_w:
        x += 1

ans = max_h + max_w

if x == nh * nw:
    ans -= 1

print(ans)