Problem p02557 (python)

Fixed submission:
# -*- coding: utf-8 -*-
#<参考>https://atcoder.jp/contests/abc178/submissions/16700679
import sys
from heapq import *
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
A = list(map(int,readline().split()))
B = list(map(int,readline().split()))

countA = [0]*(N+1)
countB = [0]*(N+1)
for i in range(N):
    countA[A[i]] += 1
    countB[B[i]] += 1

q = []
onlyA = []
onlyB = []
for i in range(N+1):
    if countA[i] and countB[i]:
        heappush(q,(-countA[i]-countB[i],i))
        continue
    for _ in range(countA[i]):
        onlyA.append(i)
    for _ in range(countB[i]):
        onlyB.append(i)
        
heapify(q)

if q and -q[0][0] > N:
    print('No')
    sys.exit()
print('Yes')

ans = []

while q:
    _,x = heappop(q)
    if q:
        _,y = heappop(q)
        flag = True
    else:
        y = onlyB.pop()
        flag = False
    countA[x] -= 1 
    countB[y] -= 1
    ans.append((x,y))
    if countA[x]:
        heappush(q,(-countA[x]-countB[x],x))
    else:
        for _ in range(countB[x]):
            onlyB.append(x)
            
    if flag and countB[y]:
        heappush(q,(-countA[y]-countB[y],y))
    if flag and not countB[y]:
        for _ in range(countA[y]):
            onlyA.append(y)

while onlyA:
    x,y = onlyA.pop(),onlyB.pop()
    ans.append((x,y))

ans.sort()
X,Y = zip(*ans)
print(*Y)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
#<参考>https://atcoder.jp/contests/abc178/submissions/16700679
import sys
from heapq import *
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
A = list(map(int,readline().split()))
B = list(map(int,readline().split()))

countA = [0]*(N+1)
countB = [0]*(N+1)
for i in range(N):
    countA[A[i]] += 1
    countB[B[i]] += 1

q = []
onlyA = []
onlyB = []
for i in range(N):
    if countA[i] and countB[i]:
        heappush(q,(-countA[i]-countB[i],i))
        continue
    for _ in range(countA[i]):
        onlyA.append(i)
    for _ in range(countB[i]):
        onlyB.append(i)
        
heapify(q)

if q and -q[0][0] > N:
    print('No')
    sys.exit()
print('Yes')

ans = []

while q:
    _,x = heappop(q)
    if q:
        _,y = heappop(q)
        flag = True
    else:
        y = onlyB.pop()
        flag = False
    countA[x] -= 1 
    countB[y] -= 1
    ans.append((x,y))
    if countA[x]:
        heappush(q,(-countA[x]-countB[x],x))
    else:
        for _ in range(countB[x]):
            onlyB.append(x)
            
    if flag and countB[y]:
        heappush(q,(-countA[y]-countB[y],y))
    if flag and not countB[y]:
        for _ in range(countA[y]):
            onlyA.append(y)

while onlyA:
    x,y = onlyA.pop(),onlyB.pop()
    ans.append((x,y))

ans.sort()
X,Y = zip(*ans)
print(*Y)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
#<参考>https://atcoder.jp/contests/abc178/submissions/16700679
import sys
from heapq import *
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
A = list(map(int,readline().split()))
B = list(map(int,readline().split()))

countA = [0]*(N+1)
countB = [0]*(N+1)
for i in range(N):
    countA[A[i]] += 1
    countB[B[i]] += 1

q = []
onlyA = []
onlyB = []
for i in range(N):
    if countA[i] and countB[i]:
        heappush(q,(-countA[i]-countB[i],i))
    elif countA[i]:
        onlyA.append(i)
    else:
        onlyB.append(i)
        
heapify(q)

if q and -q[0][0] > N:
    print('No')
    sys.exit()
print('Yes')

ans = []

while q:
    _,x = heappop(q)
    if q:
        _,y = heappop(q)
        flag = True
    else:
        y = onlyB.pop()
        flag = False
    countA[x] -= 1 
    countB[y] -= 1
    ans.append((x,y))
    if countA[x]:
        heappush(q,(-countA[x]-countB[x],x))
    else:
        for _ in range(countB[x]):
            onlyB.append(x)
            
    if flag and countB[y]:
        heappush(q,(-countA[y]-countB[y],y))
    if flag and not countB[y]:
        for _ in range(countA[y]):
            onlyA.append(y)

while onlyA:
    x,y = onlyA.pop(),onlyB.pop()
    ans.append((x,y))

ans.sort()
X,Y = zip(*ans)
print(*Y)