Problem p02557 (python)

Fixed submission:
import random
N=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
a=[0 for i in range(N+1)]
b=[0 for i in range(N+1)]
for i in range(N):
    a[A[i]]+=1
    b[B[i]]+=1
if max([a[i]+b[i] for i in range(1,N+1)])<=N:
    print("Yes")
else:
    print("No")
    exit()
B=B[::-1]
point=0
for i in range(N):
    if A[i]==B[i]:
        point+=1
while(point>0):
    x=random.randrange(N)
    y=random.randrange(N)
    if x==y:
        continue
    before=0
    after=0
    if A[x]==B[x]:
        before+=1
    if A[y]==B[y]:
        before+=1
    if A[x]==B[y]:
        after+=1
    if A[y]==B[x]:
        after+=1
    if before>after:
        B[x],B[y]=B[y],B[x]
        point=point+after-before
print(*B)

Buggy submission (verdict: Runtime Error):
from collections import deque
import random
N=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
if 0:
    N=2*10**5
    A=[random.randrange(1,N+1) for i in range(N)]
    B=[random.randrange(1,N+1) for i in range(N)]
    A.sort()
    B.sort()
a=[0 for i in range(N+1)]
b=[0 for i in range(N+1)]
for i in range(N):
    a[A[i]]+=1
    b[B[i]]+=1
if max([a[i]+b[i] for i in range(1,N+1)])>N:
    print("No")
    exit()
print("Yes")
ans0=[0 for i in range(N)]
ans1=[0 for i in range(N)]
flag0=1
flag1=1
q=deque([])
for i in range(N):
    q.append(B[i])
for i in range(N):
    if q[0]==q[-1] and q[0]==A[i]:
        flag0=0    
        break
    if q[-1]==A[i]:
        ans0[i]=q.popleft()
    else:
        ans0[i]=q.pop()
q=deque([])
for i in range(N):
    q.append(B[i])
for i in range(N):
    if q[0]==q[-1] and q[0]==A[i]:
        flag1=0    
        break
    if q[0]==A[i]:
        ans1[i]=q.pop()
    else:
        ans1[i]=q.popleft()
if flag0:
    print(*ans0)
elif flag1:
    print(*ans1)
else:
    while(1):
        ans2=[0 for i in range(N)]
        flag2=1
        q=deque([])
        for i in range(N):
            q.append(B[i])
        for i in range(N):
            if q[0]==q[-1] and q[0]==A[i]:
                flag1=0    
                break
            if q[0]==A[i]:
                ans2[i]=q.pop()
            elif q[-1]==A[i]:
                ans2[i]=q.popleft()
            else:
                if random.randrange(2):
                    ans2[i]=q.pop()
                else:
                    ans2[i]=q.popleft()
        if flag2:
            print(*ans)
            exit()

exit()
if min([abs(A[i]-ans[i]) for i in range(N)])==0:
    print("NG")

Buggy submission (verdict: Runtime Error):
from collections import deque
N=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
if 0:
    import random
    N=2*10**5
    A=[random.randrange(1,N+1) for i in range(N)]
    B=[random.randrange(1,N+1) for i in range(N)]
    A.sort()
    B.sort()
a=[0 for i in range(N+1)]
b=[0 for i in range(N+1)]
for i in range(N):
    a[A[i]]+=1
    b[B[i]]+=1
if max([a[i]+b[i] for i in range(1,N+1)])>N:
    print("No")
    exit()
print("Yes")
ans0=[0 for i in range(N)]
ans1=[0 for i in range(N)]
flag0=1
flag1=1
q=deque([])
for i in range(N):
    q.append(B[i])
for i in range(N):
    if q[0]==q[-1] and q[0]==A[i]:
        flag0=0    
        break
    if q[-1]==A[i]:
        ans0[i]=q.popleft()
    else:
        ans0[i]=q.pop()
q=deque([])
for i in range(N):
    q.append(B[i])
for i in range(N):
    if q[0]==q[-1] and q[0]==A[i]:
        flag1=0    
        break
    if q[0]==A[i]:
        ans1[i]=q.pop()
    else:
        ans1[i]=q.popleft()
if flag0:
    print(*ans0)
elif flag1:
    print(*ans1)
else:
    print([0][2])
exit()
if min([abs(A[i]-ans[i]) for i in range(N)])==0:
    print("NG")

Buggy submission (verdict: Runtime Error):
from collections import deque
import heapq
N=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
if 0:
    import random
    N=2*10**5
    A=[random.randrange(1,N+1) for i in range(N)]
    B=[random.randrange(1,N+1) for i in range(N)]
    A.sort()
    B.sort()
a=[0 for i in range(N+1)]
b=[0 for i in range(N+1)]
for i in range(N):
    a[A[i]]+=1
    b[B[i]]+=1
if max([a[i]+b[i] for i in range(1,N+1)])>N:
    print("No")
    exit()
print("Yes")
ans=[0 for i in range(N)]
index=sorted([i for i in range(1,N+1)],key=lambda i:-a[i]-b[i])

q=[]
for i in index:
    if a[i]+b[i]==0:
        continue
    heapq.heappush(q,(-a[i]-b[i],-b[i],-i))
num=-1
#print(q,a)
while(len(q)>0):
    #print(ans,q)
    num+=1
    num%=N
    if q[0][1]==0:
        heapq.heappop(q)
        num-=1
        continue
    if ans[num]!=0:
        continue
    r0=heapq.heappop(q)
    if -r0[2]==A[num]:
        r1=heapq.heappop(q)
        ans[num]=-r1[2]
        if r1[1]<-1:
            heapq.heappush(q,(r1[0]+1,r1[1]+1,r1[2]))
        heapq.heappush(q,r0)
    else:
        ans[num]=-r0[2]
        if r0[1]<-1:
            heapq.heappush(q,(r0[0]+1,r0[1]+1,r0[2]))
print(*ans)