Problem p02580 (python)

Fixed submission:
H, W, m = map(int, input().split())
hs = [0] * (H+1)
ws = [0] * (W+1)
from collections import defaultdict
d = defaultdict(bool)
N = 10**6
for i in range(m):
    hi,wi = map(int, input().split())
    hi -= 1
    wi -= 1
    hs[hi] += 1
    ws[wi] += 1
    d[hi*10**6+wi] = True

hmx = max(hs)
hset = set()
for i,h in enumerate(hs):
    if h == hmx:
        hset.add(i)
wmx = max(ws)
wset = set()
for j, w in enumerate(ws):
    if w == wmx:
        wset.add(j)

flg = False
ans = 0
cnt = 0
for i in hset:
    for j in wset:
        ans = max(ans, hmx+wmx-d[i*N+j]) 
        if cnt >= m:
            flg = True
            break
        cnt += 1
    if flg:break

print(ans)

Buggy submission (verdict: Runtime Error):
H, W, m = map(int, input().split())
hs = [0] * (H+1)
ws = [0] * (W+1)
fs = [[0]*(W+1) for i in range(H+1)]
for i in range(m):
    hi,wi = map(int, input().split())
    hs[hi-1] += 1
    ws[wi-1] += 1
    fs[hi-1][wi-1] += 1

Buggy submission (verdict: Runtime Error):
H, W, m = map(int, input().split())
hs = [0] * (H+1)
ws = [0] * (W+1)
fs = [[0]*(W+1) for i in range(H+1)]
for i in range(m):
    hi,wi = map(int, input().split())
    hs[hi-1] += 1
    ws[wi-1] += 1
    fs[hi-1][wi-1] += 1

hmx = max(hs)
hset = set()
for i,h in enumerate(hs):
    if h == hmx:
        hset.add(i)
wmx = max(ws)
wset = set()
for j, w in enumerate(ws):
    if w == wmx:
        wset.add(j)

flg = False
ans = 0
cnt = 0
for i in hset:
    for j in wset:
        ans = max(ans, hmx+wmx-fs[i][j]) 
        if cnt >= m:
            flg = True
            break
        cnt += 1
    if flg:break

print(ans)

Buggy submission (verdict: Runtime Error):
H, W, m = map(int, input().split())
hs = [0] * (H+1)
ws = [0] * (W+1)
fs = [[0]*(W+1) for i in range(H+1)]
for i in range(m):
    hi,wi = map(int, input().split())
    hs[hi-1] += 1
    ws[wi-1] += 1
    fs[hi-1][wi-1] += 1

N = 10**6
hmx = max(hs)
hset = set()
for h in hs:
    if h == hmx:
        hset.add(h)
wmx = max(ws)
wset = set()
for w in ws:
    if w == wmx:
        wset.add(w)

flg = False
ans = 0
cnt = 0
for i in hset:
    for j in wset:
        ans = max(ans, hmx+wmx-fs[i][j]) 
        if cnt >= m:
            flg = True
            break
        cnt += 1
    if flg:break

print(ans)

Buggy submission (verdict: Runtime Error):
H, W, m = map(int, input().split())
hs = [0] * (H+1)
ws = [0] * (W+1)
fs = [[0]*(W+1) for i in range(H+1)]
for i in range(m):
    hi,wi = map(int, input().split())
    hs[hi-1] += 1
    ws[wi-1] += 1
    fs[hi-1][wi-1] += 1

N = 10**6
Hs = [h*N+i for i,h in enumerate(hs)]
Ws = [w*N+i for i,w in enumerate(ws)]
Hs.sort(reverse=True)
Ws.sort(reverse=True)
hmx = Hs[0]//N
wmx = Hs[0]//N
mx = 0
cnt = 0

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
ch, cw = map(int, input().split())
dh_, dw_ = map(int, input().split())

mvs = [(-1,0),(1,0),(0,1),(0,-1)]
fs = []

for i in range(H):
    r = list(input())
    fs.append(r)

INF = 1<<50
dps = [[INF]*W for i in range(H)]
dps[ch-1][cw-1] = 0
from collections import deque
q = deque([])
q.append((ch-1,cw-1))
while len(q)>0:
    h,w = q.popleft()
    cnt = dps[h][w]
    # 先に魔法を使っておく
    for dh in range(-2,3):
        for dw in range(-2,3):
            hn = h + dh
            wn = w + dw
            if (dh,dw) in mvs:
                if 0<=hn<H and 0<=wn<W:
                    if dps[hn][wn] <= cnt:continue
                    if fs[hn][wn] == ".":
                        q.append((hn, wn))
                        dps[hn][wn] = min(dps[h][w], cnt)
            else:
                if 0<=hn<H and 0<wn<W:
                    if dps[hn][wn] <= cnt+1:continue
                    if fs[hn][wn] == ".":
                        q.appendleft((hn,wn))
                        dps[hn][wn] = min(dps[hn][wn], cnt + 1)

# for r in dps:
#     print(*r)
ans = dps[dh_-1][dw_-1]
print(ans if ans != INF else -1)

Buggy submission (verdict: Runtime Error):
H, W, m = map(int, input().split())
hs = [0] * (H+1)
ws = [0] * (W+1)
fs = [[0]*(W+1) for i in range(H+1)]
for i in range(m):
    hi,wi = map(int, input().split())
    hs[hi-1] += 1
    ws[wi-1] += 1
    fs[hi-1][wi-1] += 1

N = 10**6
Hs = [h*N+i for i,h in enumerate(hs)]
Ws = [w*N+i for i,w in enumerate(ws)]
Hs.sort(reverse=True)
Ws.sort(reverse=True)

mx = 0
cnt = 0
for h_ in Hs:
    nh, ih = divmod(h_,N)
    for w_ in Ws:
        nw, iw = divmod(w_,N)
        mx = max(mx,nh+nw-fs[ih][iw])
        cnt += 1
        if cnt > m:
            print(mx)
            exit()
print(mx)

Buggy submission (verdict: Runtime Error):
H, W, m = map(int, input().split())
hs = [0] * H
ws = [0] * W
fs = [[0]*W for i in range(H)]
for i in range(m):
    hi,wi  = map(int, input().split())
    hs[hi-1] += 1
    ws[wi-1] += 1
    fs[hi-1][wi-1] += 1
N = 10**6
Hs = [h*N+i for i,h in enumerate(hs)]
Ws = [w*N+i for i,w in enumerate(ws)]
Hs.sort(reverse=True)
Ws.sort(reverse=True)
cnt = 0
# for r in fs:
#     print(*r)
mx = 0
for h_ in Hs:
    nh, ih = divmod(h_,N)
    for w_ in Ws:
        nw, iw = divmod(w_,N)
        mx = max(mx, nh+nw-fs[ih][iw])
        cnt += 1
        if cnt >= m:
            print(mx)
            exit()
print(mx)

Buggy submission (verdict: Runtime Error):
H, W, m = map(int, input().split())
hs = [0] * H
ws = [0] * W
fs = [[0]*W for i in range(H)]
ps = []
for i in range(m):
    hi,wi  = map(int, input().split())
    hs[hi-1] += 1
    ws[wi-1] += 1
    fs[hi-1][wi-1] += 1
    ps.append((hi-1,wi-1))
mx = 0
Hs = [(h,i) for i,h in enumerate(hs)]
Ws = [(w,i) for i,w in enumerate(ws)]
Hs.sort(reverse=True)
Ws.sort(reverse=True)
cnt = 0
hmx = Hs[0][0]
wmx = Ws[0][0]
# for r in fs:
#     print(*r)
mx = 0
for nh, ih in Hs:
    for nw, iw in Ws:
        if nh < hmx and nw < wmx: continue
        if fs[ih][iw] == 0:
            mx = max(mx, nh+nw)
        if fs[ih][iw] == 1:
            mx = max(mx, nh+nw-1)
        cnt += 1
        if cnt >= m:
            print(mx)
            exit()
print(mx)

Buggy submission (verdict: Runtime Error):
H, W, m = map(int, input().split())
hs = [0] * H
ws = [0] * W
fs = [[0]*W for i in range(H)]
ps = []
for i in range(m):
    hi,wi  = map(int, input().split())
    hs[hi-1] += 1
    ws[wi-1] += 1
    fs[hi-1][wi-1] += 1
    ps.append((hi-1,wi-1))
mx = 0
Hs = [(h,i) for i,h in enumerate(hs)]
Ws = [(w,i) for i,w in enumerate(ws)]
Hs.sort(reverse=True)
Ws.sort(reverse=True)
cnt = 0
hmx = Hs[0][0]
wmx = Ws[0][0]
# for r in fs:
#     print(*r)
mx = 0
for nh, ih in Hs:
    for nw, iw in Ws:
        if nh < hmx and nw < wmx: continue
        if fs[ih][iw] == 0:
            mx = max(mx, nh+nw)
        if fs[ih][iw] == 1:
            mx = max(mx, nh+nw-1)
        cnt += 1
        if cnt >= m:
            print(mx)
            exit()
print(mx)