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:
import sys
 
H,W,M = list(map(int, input().split()))
U = 3 * 10**5
hw = [[0] * (U) for _ in range(U)]
 
w = [0] * (U)
h = [0] * (U)
mxw = []
mxw_v = 0
mxh = []
mxh_v = 0
 
 
for i in range(M):
    h_i, w_j = list(map(int, input().split()))
    hw[h_i][w_j] = 1
    h[h_i] += 1
    w[w_j] += 1
    if mxw_v < w[w_j]:
        mxw_v = w[w_j]
        mxw = [w_j]
    elif mxw_v == w[w_j]:
        mxw.append(w_j)
 
    if mxh_v < h[h_i]:
        mxh_v = h[h_i]
        mxh = [h_i]
    elif mxh_v == h[h_i]:
        mxh.append(h_i)
a = 0
for i in mxh:
    for j in mxw:
        d = hw[i][j]
        a = max(a, mxh_v + mxw_v - d)
print(a)

Submission status: Runtime Error

Solution code:
import sys
 
H,W,M = list(map(int, input().split()))
U = 3 * 10**5
hw = [[0] * (U) for _ in range(U)]
 
w = [0] * (U)
h = [0] * (U)
mxw = []
mxw_v = 0
mxh = []
mxh_v = 0
 
 
for i in range(M):
    h_i, w_j = list(map(int, input().split()))
    hw[h_i][w_j] = 1
    h[h_i] += 1
    w[w_j] += 1
    if mxw_v < w[w_j]:
        mxw_v = w[w_j]
        mxw = [w_j]
    elif mxw_v == w[w_j]:
        mxw.append(w_j)
 
    if mxh_v < h[h_i]:
        mxh_v = h[h_i]
        mxh = [h_i]
    elif mxh_v == h[h_i]:
        mxh.append(h_i)
a = 0
for i in mxh:
    for j in mxw:
        d = hw[i][j]
        a = max(a, h[i]+w[j]-d)
print(a)

Submission status: Runtime Error

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

Solution code:
from numpy import*
z=loadtxt(open(0),int,skiprows=1)
r=zeros(9**6,int)
c=copy(r)
for y,x in z:r[y]+=1;c[x]+=1
h,w=max(r),max(c)
y,x=z.T
print(h+w-(sum(r==h)*sum(w==c)==sum(h+w==r[y]+c[x])))

Submission status: Runtime Error

Solution code:
import numpy as np
from collections import Counter
H, W, M = map(int, input().split())
h = [0] * M
w = [0] * M
bom = np.zeros((H + 1, W + 1))
for i in range(M):
    h[i], w[i] = map(int, input().split())
    bom[h[i]][w[i]] = 1
hl = Counter(h)
hl = list(reversed(sorted(hl.items(), key=lambda x: x[1])))
wl = Counter(w)
wl = list(reversed(sorted(wl.items(), key=lambda x: x[1])))
ans = 0
#print(hl)
#print(wl)
max_hl = hl[0][1]
max_wl = wl[0][1]
ans = max_hl + max_wl
end_flag = 0
max_flag = 1
for i in range(len(hl)):
    for j in range(len(wl)):
        if hl[i][1] < max_hl:
            end_flag = 1
            break
        if wl[j][1] < max_wl:
            break
        if bom[hl[i][0]][wl[j][0]] == 1:
            continue
        else:
            end_flag = 1
            max_flag = 0
            break
    else:
        continue
    if end_flag == 1:
        break
print(ans - max_flag)

Submission status: Runtime Error

Solution code:
import sys
from itertools import product
H, W, M = map(int, input().split())
hsum = [0 for i in range(H)]
wsum = [0 for i in range(W)]
s = set()
for i in range(M):
    h, w = map(int, input().split())
    h, w = h-1, w-1
    s.add((h, w))
    hsum[h] += 1
    wsum[w] += 1

hmax = max(hsum)
wmax = max(wsum)

hmax_index = []
for i in range(H):
    if hsum[i] == hmax:
        hmax_index.append(i)
wmax_index = []
for i in range(H):
    if wsum[i] == wmax:
        wmax_index.append(i)

for x in hmax_index:
    for y in wmax_index:
        if (x, y) not in s:
            print(hmax+wmax)
            sys.exit()
print(hmax+wmax-1)

Submission status: Runtime Error

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

Solution code:
import numpy as np
H, W, M = map(int, input().split())
HW = [list(map(int, input().split())) for i in range(M)]

grid = np.zeros((H, W))
for h, w in HW:
    grid[h-1][w-1] = 1

max_h = 0
at_h = -1
for h in range(H):
    tmp = np.count_nonzero(grid[h, :])
    if tmp > max_h:
        max_h = tmp
        at_h = h + 1
max_w = 0
at_w = -1
for w in range(W):
    tmp = np.count_nonzero(grid[:, w])
    if tmp > max_w:
        max_w = tmp
        at_w = w + 1

if [at_h, at_w] in HW:
    print(max_h + max_w - 1)
else:
    print(max_h + max_w)

Submission status: Runtime Error

Solution code:
h, w, m = map(int, input().split())
bomb_list = [list(map(int, input().split())) for i in range(m)]
 
bomb_list = [[bomb_list[o][p] - 1 for p in range(2)] for o in range(m)] 
 
h_count_list = [0 for j in range(h)]
w_count_list = [0 for k in range(w)]
 
max_h_count = 0
max_w_count = 0
 
x_list = []
y_list = []
 
for l in range(m):
    a = bomb_list[l][0]
    h_count_list[a] += 1
 
max_h_count = max(h_count_list)
 
for c in range(h):
    if h_count_list[c] == max_h_count:
        x_list.append(c)

w_count_list = [w_count_list[bomb_list[n][1]] + 1 for n in range(m)]
 
max_w_count = max(w_count_list)
 
for d in range(w):
    if w_count_list[d] == max_w_count:
        y_list.append(d)
 
e = 0
 
for x in x_list:
    for y in y_list:
        if bomb_list.count([x, y]) == 1 and e <= max_w_count + max_h_count - 1:
            e = max_w_count + max_h_count - 1
        elif e <= max_w_count + max_h_count:
            e = max_w_count + max_h_count
 
print(e)

Submission status: Runtime Error

Solution code:
h,w,m=map(int, input().split())
hlist=[0]*h
wlist=[0]*w
hw=[[0]*w for i in range(h)]
h1=[]
w1=[]
for i in range(m):
  x,y=map(int, input().split())
  hlist[x-1]+=1
  wlist[y-1]+=1
  hw[x-1][y-1]=1
  h1.append(x-1)
  w1.append(y-1)
maxh=max(hlist)
maxw=max(wlist)
hlist1=[]
wlist1=[]
for i in range(h):
  if hlist[i]==maxh:
    hlist1.append(i)
for i in range(w):
  if wlist[i]==maxw:
    wlist1.append(i)
cnt=0
for i in range(m):
  if h1[i]  in hlist1 and w1[i]  in wlist1:
    cnt+=1
if cnt==len(wlist1)*len(hlist1):
  num=1
else:
  num=0
print(maxh+maxw-num)

Submission status: Runtime Error

Solution code:
import sys

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

hw.sort()

hcnt = [0] * (H+1)
wcnt = [0] * (W+1)

target = []
for i, j in hw:
    hcnt[i] += 1
    wcnt[j] += 1
    target.append([str(i) + " " + str(j),1])

d = dict(target)

hlist = []
hmax = -1
for i in range(1,H+1):
    if hmax < hcnt[i]:
        hlist = [i]
        hmax = hcnt[i]
    elif hmax == hcnt[i]:
        hlist.append(i)

wlist = []
wmax = -1
for i in range(1,W+1):
    if wmax < wcnt[i]:
        wlist = [i]
        wmax = wcnt[i]
    elif wmax == wcnt[i]:
        wlist.append(i)

if len(hlist) * len(wlist) > M:
    print(hmax + wmax)
    sys.exit()

idx = 0
for h in hlist:
    for w in wlist:
        while h > hw[idx][0] or w > hw[idx][1]:
            idx += 1
            if h == hw[idx][0] and w == hw[idx][1]:
                break
        if h < hw[idx][0] or w < hw[idx][1]:
            print(hmax + wmax)
            sys.exit()
        
print(hmax + wmax - 1)

Submission status: Runtime Error

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 + max(minus)


print(solve(h_max, w_max, boms))

Submission status: Runtime Error

Solution code:
import sys

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

hw.sort()

hcnt = [0] * (H+1)
wcnt = [0] * (W+1)

target = []
for i, j in hw:
    hcnt[i] += 1
    wcnt[j] += 1
    target.append([str(i) + " " + str(j),1])

d = dict(target)

hlist = []
hmax = -1
for i in range(1,H+1):
    if hmax < hcnt[i]:
        hlist = [i]
        hmax = hcnt[i]
    elif hmax == hcnt[i]:
        hlist.append(i)

wlist = []
wmax = -1
for i in range(1,W+1):
    if wmax < wcnt[i]:
        wlist = [i]
        wmax = wcnt[i]
    elif wmax == wcnt[i]:
        wlist.append(i)

if len(hlist) * len(wlist) > M:
    print(hmax + wmax)
    sys.exit()

idx = 0
for h in hlist:
    for w in wlist:
        while h > hw[idx][0] or w > hw[idx][1]:
            idx += 1
            if h == hw[idx][0] and w == hw[idx][1]:
                break
        if h < hw[idx][0] or w < hw[idx][1]:
            print(hmax + wmax)
            sys.exit()
        
print(hmax + wmax - 1)

Submission status: Runtime Error

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:
        for k1 in kouho_1d:
            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
    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: Runtime Error

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

h = [0] * H
w = [0] * W
boms = set()

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

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

h_idx = [x + 1 for x, y in enumerate(h) if y == h_max]
w_idx = [x + 1 for x, y in enumerate(w) if y == w_max]

minus = -1
for x in h_idx:
    for y in w_idx:
        if not (x, y) in boms:
            minus = 0
            break

print(h_max + w_idx + minus)

Submission status: Runtime Error

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_max = kouho_1d[0]
    kouho_1d_min = kouho_1d[-1]
    
#    print(kouho_1d)    
    
    if kouho_1d_max <= bakuha_1d_max and kouho_1d_min >= bakuha_1d_min:
        for k1 in kouho_1d:
            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
    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: Runtime Error

Solution code:
h,w,m=[int(x) for x in input().split()]

hnm=[0]*h
wnm=[0]*w
hw=[[0]*w for _ in range(h)]
for _ in range(m):
    hi,wi=[int(x)-1 for x in input().split()]
    hnm[hi]+=1
    wnm[wi]+=1
    hw[hi][wi]+=1

hm_idx=[]
hm_val=-1
for i in range(h):
    if hnm[i]>hm_val:
        hm_idx=[i]
        hm_val=hnm[i]
    elif hnm[i]==hm_val:
        hm_idx.append(i)

mn1=True
wm_val=-1
for i in range(w):
    if wnm[i]>wm_val:
        wm_val=wnm[i]
        mn1=True
        for j in hm_idx:
            if hw[j][i]==0:
                mn1=False
                break
    elif wnm[i]==wm_val:
        for j in hm_idx:
            if hw[j][i]==0:
                mn1=False
                break
                
print(hm_val+wm_val+(-1 if mn1 else 0))

Submission status: Runtime Error

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(HW[:][0]).most_common()
c_w = Counter(HW[:][1]).most_common()

print(HW[:][0])

# 登場頻度の一番高い回数
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: Runtime Error

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(HW[:][0]).most_common()
c_w = Counter(HW[:][1]).most_common()

print(HW[:][0])

# 登場頻度の一番高い回数
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: Runtime Error

Solution code:
from collections import Counter
H, W, M = map(int, input().split())
h = [0] * M
w = [0] * M
bom = [[0] * (W+1) for _ in range(H+1)]
for i in range(M):
    h[i], w[i] = map(int, input().split())
    bom[h[i]][w[i]] = 1
hl = Counter(h)
hl = list(reversed(sorted(hl.items(), key=lambda x: x[1])))
wl = Counter(w)
wl = list(reversed(sorted(wl.items(), key=lambda x: x[1])))
ans = 0
#print(hl)
#print(wl)
max_hl = hl[0][1]
max_wl = wl[0][1]
ans = max_hl + max_wl
end_flag = 0
max_flag = 1
for i in range(len(hl)):
    for j in range(len(wl)):
        if hl[i][1] < max_hl:
            end_flag = 1
            break
        if wl[j][1] < max_wl:
            break
        if bom[hl[i][0]][wl[j][0]] == 1:
            continue
        else:
            end_flag = 1
            max_flag = 0
            break
    else:
        continue
    if end_flag == 1:
        break
print(ans - max_flag)

Submission status: Runtime Error

Solution code:
h,w,m=[int(x) for x in input().split()]

hnm=[0]*h
wnm=[0]*w
hw=[[0]*w for _ in range(h)]
for _ in range(m):
    hi,wi=[int(x)-1 for x in input().split()]
    hnm[hi]+=1
    wnm[wi]+=1
    hw[hi][wi]+=1

hm_idx=[]
hm_val=-1
for i in range(h):
    if hnm[i]>hm_val:
        hm_idx=[i]
        hm_val=hnm[i]
    elif hnm[i]==hm_val:
        hm_idx.append(i)

mn1=True
wm_val=-1
for i in range(w):
    if wnm[i]>wm_val:
        wm_val=wnm[i]
        mn1=True
        for j in hm_idx:
            if hw[j][i]==0:
                mn1=False
                break
    elif wnm[i]==wm_val:
        mn1=True
        for j in hm_idx:
            if hw[j][i]==0:
                mn1=False
                break
                
print(hm_val+wm_val+(-1 if mn1 else 0))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
import sys
def input(): return sys.stdin.readline().strip()


H, W, M = [int(x) for x in input().split()]
init = np.zeros((H,W))

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

#print(init)

row = np.sum(init, axis=1)
col = np.sum(init, axis=0)

max_H = np.where(row == row.max())[0]
max_W = np.where(col == col.max())[0]
#print("len_H:{}".format(len(max_H)))
#print("len_W:{}".format(len(max_W)))

before_point = 0
max_point = row[max_H[0]] + col[max_W[0]]
#print(max_point)
"""
if len(max_H) != 1 or len(max_W) != 1:
  for h in max_H:
    for w in max_W:
      point = row[h] + col[w]
      point -= init[h,w]
      if point > before_point:
        max_point = point
      before_point = point
else:
  max_point -= init[max_H[0],max_W[0]]
"""
print(int(max_point))

Submission status: Runtime Error

Solution code:
from collections import Counter
H, W, M = map(int, input().split())
h = [0] * M
w = [0] * M
bom = [[0] * (H+1) for _ in range(W+1)]
for i in range(M):
    h[i], w[i] = map(int, input().split())
    bom[h[i]][w[i]] = 1
hl = Counter(h)
hl = list(reversed(sorted(hl.items(), key=lambda x: x[1])))
wl = Counter(w)
wl = list(reversed(sorted(wl.items(), key=lambda x: x[1])))
ans = 0
#print(hl)
#print(wl)
max_hl = hl[0][1]
max_wl = wl[0][1]
ans = max_hl + max_wl
end_flag = 0
max_flag = 1
for i in range(len(hl)):
    for j in range(len(wl)):
        if hl[i][1] < max_hl:
            end_flag = 1
            break
        if wl[j][1] < max_wl:
            break
        if bom[hl[i][0]][wl[j][0]] == 1:
            continue
        else:
            end_flag = 1
            max_flag = 0
            break
    else:
        continue
    if end_flag == 1:
        break
print(ans - max_flag)

Submission status: Runtime Error

Solution code:
h, w, m = map(int, input().split())
dp= [[0]*w for i in range(h)]
dpcol = [0]*w
dprow = [0]*h
ypos = []
xpos = []
anscol = 0
ansrow = 0
ans = 0

for i in range(m):
    H, W = map(lambda x: int(x) - 1, input().split())
    dp[H][W] = 1
    dpcol[W] += 1
    dprow[H] += 1

for i in range(w): #列の最大値
    if dpcol[i] > anscol:
        anscol = dpcol[i]
        xpos = [i]
    elif dpcol[i] == anscol:
        xpos.append(i)

for i in range(h):
    if dprow[i] > ansrow:
        ansrow = dprow[i]
        ypos = [i]
    elif dprow[i] == ansrow:
        ypos.append(i)

for i in range(len(ypos)):
    for j in range(len(xpos)):
        if dp[ypos[i]][xpos[j]] == 1:
            ans = anscol + ansrow - 1
        else:
            ans = anscol + ansrow
            print(ans)
            exit()
print(ans)

Submission status: Runtime Error

Solution code:
import bisect


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 = ch_max + cw_max
for (y, x) in points:
    index = bisect.bisect_left(ys, y)
    if ys[index] == y and xs[index] == x:
        ans -= 1
        break

print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1<<20)
h,w,m=[int(x) for x in input().split()]

hnm=[0]*h
wnm=[0]*w
hw=[[0]*w for _ in range(h)]
for _ in range(m):
    hi,wi=[int(x)-1 for x in input().split()]
    hnm[hi]+=1
    wnm[wi]+=1
    hw[hi][wi]+=1

hm_idx=[]
hm_val=-1
for i in hnm:
    if hnm[i]>hm_val:
        hm_idx=[i]
        hm_val=hnm[i]
    elif hnm[i]==hm_val:
        hm_idx.append(i)

mn1=True
wm_val=-1
for i in wnm:
    if wnm[i]>wm_val:
        wm_val=wnm[i]
        mn1=True
        for j in hm_idx:
            if hw[j][i]==0:
                mn1=False
                break
    elif wnm[i]==wm_val:
        mn1=True
        for j in hm_idx:
            if hw[j][i]==0:
                mn1=False
                break
                
print(hm_val+wm_val+(-1 if mn1 else 0))

Submission status: Runtime Error

Solution code:
h,w,m=map(int,input().split())
g=[]
e1=[0]*h
e2=[0]*w
for c in range(m):
    a,b=map(int,input().split())
    g.append([a-1,b-1])
    e1[a-1]+=1
    e2[b-1]+=1
p1=max(e1)
p2=max(e2)
x=[1 if e1[c]==p1 else 0 for c in range(h)]
y=[1 if e2[c]==p2 else 0 for c in range(w)]
r=sum(x)*sum(y)
for i in g:
    if x[i[0]]==1 and y[i[1]]==1:
        r-=1
if r==0:
    print(p+q-1)
else:
    print(p+q)

Submission status: Runtime Error

Solution code:
H,W,M = map(int,input().split())
l = [[0]*W for _ in range(H)]
tate,yoko = [0]*W,[0]*H
ans = 0
for _ in range(M):
  h,w = map(int,input().split())
  h -= 1
  w -= 1
  tate[w] += 1
  yoko[h] += 1
  l[h][w] = 1
tateArr,yokoArr = [0,0],[0,0]
ans = 0
for i in range(W):
  if tate[i] > tateArr[0]:
    tateArr = [tate[i],i]
for i in range(H):
  if yoko[i] > yokoArr[0]:
    yokoArr = [yoko[i],i]

print(tateArr[0]+yokoArr[0]-l[yokoArr[1]][tateArr[1]])

Submission status: Runtime Error

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()
bakuha_1d_max = bakuha_1d[-1]
bakuha_1d_min = bakuha_1d[0]

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_max = kouho_1d[-1]
    kouho_1d_min = kouho_1d[0]
    
    
    if kouho_1d_max <= bakuha_1d_max and kouho_1d_min >= bakuha_1d_min:
        for k1 in kouho_1d[::-1]:
            tuzuku = True
            while tuzuku:
                b1_0 = bakuha_1d.pop()
                if b1_0 <= k1:
                    tuzuku = False
                    if b1_0 != k1:
                        ans_minus = False
    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: Runtime Error

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

mp = set()
hp = [0] * h
wp = [0] * w

for i in range(m):
    l = list(map(int, input().split()))
    mp.add(l)
    hp[l[0]-1] += 1
    wp[l[1]-1] += 1

hpdex = [i for i, x in enumerate(hp) if x == max(hp)]
wpdex = [i for i, x in enumerate(wp) if x == max(wp)]

for i in range(len(hpdex)):
    for j in range(len(wpdex)):
        if [hpdex[i] + 1, wpdex[j] + 1] not in mp:
            print(max(hp) + max(wp))
            exit()

print(max(hp) + max(wp) - 1)

Submission status: Runtime Error

Solution code:
import copy
import sys
H, W, M= map(int,input().split())
height = [0] * H
width = [0] * W
object = []
for i in range(H):
   wi = [0] * W
   object.append(wi)
for s in sys.stdin.readlines():
    h, w = map(int, s.split())
    height[h-1] += 1
    width[w-1] += 1
    object[h-1][w-1] = 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 object[cand_h[i]][cand_w[j]] == 0:
            ans = h_max + w_max 
            exit()         
else:
   ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
H, W, M = map(int, input().split())
dp= [[0]*W for i in range(H)]
dph = [0]*W
dpw = [0]*H
dphpos = []
dpwpos = []
ansh = 0
answ = 0
ans = 0

for i in range(M):
    h, w = map(lambda x: int(x) - 1, input().split())
    dp[h][w] = 1
    dph[w] += 1
    dpw[h] += 1

for i in range(W): #列の最大値
    if dph[i] > ansh:
        ansh = dph[i]
        dphpos = [i]
    elif dph[i] == ansh:
        dphpos.append(i)

for i in range(H):
    if dpw[i] > answ:
        answ = dpw[i]
        dpwpos = [i]
    elif dpw[i] == answ:
        dpwpos.append(i)

for i in range(len(dphpos)):
    for j in range(len(dpwpos)):
        if dp[dpwpos[j]][dphpos[i]] == 1:
            ans = max(ans, ansh + answ - 1)
        else:
            ans = ansh + answ
print(ans)

Submission status: Runtime Error

Solution code:
H, W, M = map(int, input().split())
H_max = [0 for _ in range(W+1)]
W_max = [0 for _ in range(H+1)]
A = set()
for _ in range(M):
    i, k = map(int, input().split())
    A.add((i,k))
    H_max[i] += 1
    W_max[k] += 1

saidai_H = max(H_max)
possible_H = [i for i, x in enumerate(H_max) if x == saidai_H]
saidai_W = max(W_max)
possible_W = [i for i, x in enumerate(W_max) if x == saidai_W]
ans = True
for h in possible_H:
    for w in possible_W:
        if (h, w) in A:
            continue
        else:
            ans = False
            break
if ans:
    print(saidai_H+saidai_W-1)
else:
    print(saidai_H+saidai_W)

Submission status: Runtime Error

Solution code:
targets = []
lines = [0]*(H+1)
rows = [0]*(W+1)
for i in range(M):
    l,r = list(map(int,input().split()))
    targets.append((l,r))
    lines[l] += 1
    rows[r] += 1
lmax = max(lines)
lmax_cand = []
rmax = max(rows)
rmax_cand = []
for i in range(H+1):
    if lines[i] == lmax:
        lmax_cand.append(i)
for i in range(W+1):
    if rows[i] == rmax:
        rmax_cand.append(i)
res = 0
import sys
for target in targets:
    if target[0] in lmax_cand and target[1] in rmax_cand:
        res = lmax + rmax - 1
    else:
        res = lmax + rmax
        print(res)
        sys.exit()
print(res)