Problem p02557 (python)

Fixed submission:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

from collections import deque
from collections import Counter
import bisect
def main():
    N = I()
    A = LI()
    B = LI()
    A_cnt = Counter(A)
    B_cnt = Counter(B)
    for key in B_cnt.keys():
        x = A_cnt[key]
        if B_cnt[key] > N - x:
            print('No')
            exit()
    print('Yes')
    A_m = A_cnt.most_common()
    A_ = []
    for a_m in A_m:
        A_ += [a_m[0]] * a_m[1]
    B_m = deque(B_cnt.most_common())
    ANS = []
    for i in range(N):
        x = B_m[0][0]
        if A_[i] != x:
            ANS.append([A_[i], x])
            B_cnt[x] -= 1
            if B_cnt[x] == 0:
                B_m.popleft()
        elif A_[i] == x:
            if len(B_m) != 1:
                B_m.popleft()
                B_m.append((x, B_cnt[x]))
                y = B_m[0][0]
                ANS.append([x, y])
                B_cnt[y] -= 1
                if B_cnt[y] == 0:
                    B_m.popleft()
            else:
                for ans in ANS:
                    if ans[0] != x and ans[1] != x:
                        ANS.append([x, ans[1]])
                        ans[1] = x
                        break

    ANS.sort(key=lambda tup: tup[0])
    ANS_ = []
    for ans in ANS:
        ANS_.append(ans[1])
    print(' '.join(map(str, ANS_)))

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

from collections import deque
from collections import Counter
import bisect
def main():
    N = I()
    A = LI()
    B = LI()
    A_cnt = Counter(A)
    B_cnt = Counter(B)
    for key in B_cnt.keys():
        x = A_cnt[key]
        if B_cnt[key] > N - x:
            print('No')
            exit()
    A_m = A_cnt.most_common()
    A_ = []
    C = deque()
    for a_m in A_m:
        a = a_m[0]
        A_ += [a] * a_m[1]
        if B_cnt[a] != 0:
            C.appendleft(a)
    ANS = []
    for i in range(N):
        x = C[0]
        if A_[i] != x:
            ANS.append((A_[i], x))
            B_cnt[x] -= 1
            if B_cnt[x] == 0:
                C.popleft()
        elif A_[i] == x:
            C.popleft()
            C.append(x)
            y = C[0]
            ANS.append((x, y))
            B_cnt[y] -= 1
            if B_cnt[y] == 0:
                C.popleft()

    ANS.sort(key=lambda tup: tup[0])
    ANS_ = []
    for ans in ANS:
        ANS_.append(ans[1])

    print('Yes')
    print(' '.join(map(str, ANS_)))

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

from collections import deque
from collections import Counter
import bisect
def main():
    N = I()
    A = LI()
    B = LI()
    A_cnt = Counter(A)
    B_cnt = Counter(B)
    for key in B_cnt.keys():
        x = A_cnt[key]
        if B_cnt[key] > N - x:
            print('No')
            exit()
    print('Yes')
    z = len(B_cnt.keys())
    A_m = A_cnt.most_common()
    A_ = []
    for a_m in A_m:
        A_ += [a_m[0]] * a_m[1]
    B_m = deque(B_cnt.most_common())
    ANS = []
    num_list = [0] * N
    cnt = 0
    for i in range(N):
        x = B_m[0][0]
        while num_list[x] == 1 and cnt < z:
            B_m.popleft()
            B_m.append((x, B_cnt[x]))
            x = B_m[0][0]
            cnt += 1
        cnt = 0

        if A_[i] != x:
            ANS.append((A_[i], x))
            B_cnt[x] -= 1
            if num_list[A_[i]] == 0:
                num_list[A_[i]] = 1
            if B_cnt[x] == 0:
                B_m.popleft()
        elif A_[i] == x:
            B_m.popleft()
            B_m.append((x, B_cnt[x]))
            y = B_m[0][0]
            ANS.append((x, y))
            B_cnt[y] -= 1
            if num_list[A_[i]] == 0:
                num_list[A_[i]] = 1
            if B_cnt[y] == 0:
                B_m.popleft()

    ANS.sort(key=lambda tup: tup[0])
    ANS_ = []
    for ans in ANS:
        ANS_.append(ans[1])
    print(' '.join(map(str, ANS_)))

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

from collections import deque
from collections import Counter
import bisect
def main():
    N = I()
    A = LI()
    B = LI()
    A_cnt = Counter(A)
    B_cnt = Counter(B)
    for key in B_cnt.keys():
        x = A_cnt[key]
        if B_cnt[key] > N - x:
            print('No')
            exit()
    print('Yes')
    A_m = A_cnt.most_common()
    A_ = []
    for a_m in A_m:
        A_ += [a_m[0]] * a_m[1]
    B_m = deque(B_cnt.most_common())
    ANS = []
    for i in range(N):
        x = B_m[0][0]
        if A_[i] != x:
            ANS.append((A_[i], x))
            B_cnt[x] -= 1
            if B_cnt[x] == 0:
                B_m.popleft()
        elif A_[i] == x:
            y = B_m[1][0]
            ANS.append((x, y))
            B_cnt[y] -= 1
            if B_cnt[y] == 0:
                B_m.popleft()
                B_m.popleft()
                B_m.appendleft((x, B_cnt[x]))
    ANS.sort(key=lambda tup: tup[0])
    ANS_ = []
    for ans in ANS:
        ANS_.append(ans[1])
    print(' '.join(map(str, ANS_)))

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

from collections import deque
from collections import Counter
import bisect
def main():
    N = I()
    A = LI()
    B = LI()
    A_cnt = Counter(A)
    B_cnt = Counter(B)
    for key in B_cnt.keys():
        x = A_cnt[key]
        if B_cnt[key] > N - x:
            print('No')
            exit()
    print('Yes')
    A_m = A_cnt.most_common()
    A_ = []
    for a_m in A_m:
        A_ += [a_m[0]] * a_m[1]
    B_m = deque(B_cnt.most_common())
    ANS = []
    for i in range(N):
        x = B_m[0][0]
        if A_[i] != x:
            ANS.append((A_[i], x))
            B_cnt[x] -= 1
            if B_cnt[x] == 0:
                B_m.popleft()
        if A_[i] == x:
            y = B_m[1][0]
            ANS.append((x, y))
            B_cnt[y] -= 1
            if B_cnt[y] == 0:
                B_m.popleft()
                B_m.popleft()
                B_m.appendleft((x, B_cnt[x]))
    ANS.sort(key=lambda tup: tup[0])
    ANS_ = []
    for ans in ANS:
        ANS_.append(ans[1])
    print(' '.join(map(str, ANS_)))

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

from collections import deque
from collections import Counter
import bisect
def main():
    N = I()
    A = LI()
    B = LI()
    A_cnt = Counter(A)
    B_cnt = Counter(B)
    for key in B_cnt.keys():
        x = A_cnt[key]
        if B_cnt[key] > N - x:
            print('No')
            exit()
    print('Yes')
    A_m = A_cnt.most_common()
    A_ = []
    for a_m in A_m:
        A_ += [a_m[0]] * a_m[1]
    B_m = deque(B_cnt.most_common())
    ANS = []
    for i in range(N):
        x = B_m[0][0]
        if len(B_m) >= 2:
            y = B_m[1][0]
        if A_[i] != x:
            ANS.append((A_[i], x))
            B_cnt[x] -= 1
            if B_cnt[x] == 0:
                B_m.popleft()
        if A_[i] == x:
            ANS.append((x, y))
            B_cnt[y] -= 1
            if B_cnt[y] == 0:
                B_m.popleft()
                B_m.popleft()
                B_m.appendleft((x, B_cnt[x]))
    ANS.sort(key=lambda tup: tup[0])
    ANS_ = []
    for ans in ANS:
        ANS_.append(ans[1])
    print(' '.join(map(str, ANS_)))

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

from collections import deque
from collections import Counter
import bisect
def main():
    N = I()
    A = LI()
    B = LI()
    A_cnt = Counter(A)
    B_cnt = Counter(B)
    for key in B_cnt.keys():
        x = A_cnt[key]
        if B_cnt[key] > N - x:
            print('No')
            exit()
    print('Yes')
    B_m = deque(B_cnt.most_common())
    ANS = [0] * N
    for i in range(N):
        x = B_m[0][0]
        if len(B_m) >= 2:
            y = B_m[1][0]
        if A[i] != x:
            ANS[i] = x
            B_cnt[x] -= 1
            if B_cnt[x] == 0:
                B_m.popleft()
        if A[i] == x:
            ANS[i] = y
            B_cnt[y] -= 1
            if B_cnt[y] == 0:
                B_m.popleft()
                B_m.popleft()
                B_m.appendleft((x, B_cnt[x]))
    print(' '.join(map(str, ANS)))

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

from collections import deque
from collections import Counter
import bisect
def main():
    N = I()
    A = LI()
    B = LI()
    A_cnt = Counter(A)
    B_cnt = Counter(B)
    for key in B_cnt.keys():
        x = A_cnt[key]
        if B_cnt[key] > N - x:
            print('No')
            exit()
    print('Yes')
    B_m = deque(B_cnt.most_common())
    ANS = [0] * N
    for i in range(N):
        x = B_m[0][0]
        if len(B_m) >= 2:
            y = B_m[1][0]
        if A[i] != x:
            ANS[i] = x
            B_cnt[x] -= 1
            if B_cnt[x] == 0:
                B_m.popleft()
        else:
            ANS[i] = y
            B_cnt[y] -= 1
            if B_cnt[y] == 0:
                B_m.popleft()
                B_m.popleft()
                B_m.appendleft((x, B_cnt[x]))
    print(' '.join(map(str, ANS)))

if __name__ == "__main__":
    main()