Problem p02557 (python)

Fixed submission:
import collections
import sys

def solve(n, A, B):
  Ac = {}
  ABc = {}
  Bc = {}
  for a in A:
    Ac[a] = 1 + Ac.get(a, 0)
    ABc[a] = 1 + ABc.get(a, 0)
    Bc[a] = Bc.get(a, 0)
  for b in B:
    Ac[b] = Ac.get(b, 0)
    Bc[b]  = 1 + Bc.get(b, 0)
    ABc[b] = 1 + ABc.get(b, 0)
  cAB = sorted([(c, ab) for ab, c in ABc.items()])
  #print(cAB)
  pool = collections.deque()  #[bbbb]   # [(b, count)], FIFO to match at the end

  matched = {}  # {a: bbbb}    # {a: {b, count}
  current = n   # how many are not matched yet
  for c, ab in cAB[::-1]:
    if c > current + len(pool):
      print('No')
      return
    matched[ab] = []
    while c > current:
      # take c - current from pool, add to matched
      c -= 1
      matched[ab].append(pool.popleft())
    # add (ab, Bc[ab]) to pool
    current -= Bc[ab]
    for _ in range(Bc[ab]):
      pool.append(ab)
  #print(pool, matched)
  skipped = []
  for c, ab in cAB:
    # take c - matched[ab] from pool, add to matched
    nn = Ac[ab]-len(matched[ab])
    while skipped and  Ac[ab]-len(matched[ab]) > 0:
      matched[ab].append(skipped.pop())
    while Ac[ab]-len(matched[ab]) > 0:
      p = pool.popleft()
      if p == ab:
        skipped.append(p)
      else:
        matched[ab].append(p)
      #print(ab, pool, matched, skipped)

  ans = []
  for a, bb in sorted(matched.items()):
    ans += [str(b) for b in bb]
  print('Yes')
  print(' '.join(ans))


def main():
  input = sys.stdin.readline
  n = int(input())
  A = list(map(int, input().split()))
  B = list(map(int, input().split()))
  solve(n, A, B)

main()

Buggy submission (verdict: Runtime Error):
import collections
import sys

def solve(n, A, B):
  Ac = {}
  ABc = {}
  Bc = {}
  for a in A:
    Ac[a] = 1 + Ac.get(a, 0)
    ABc[a] = 1 + ABc.get(a, 0)
    Bc[a] = 0
  for b in B:
    Bc[b]  = 1 + Bc.get(b, 0)
    ABc[b] = 1 + ABc.get(b, 0)
  cAB = sorted([(c, ab) for ab, c in ABc.items()])
  #print(cAB)
  pool = collections.deque()  #[bbbb]   # [(b, count)], FIFO to match at the end

  matched = {}  # {a: bbbb}    # {a: {b, count}
  current = n   # how many are not matched yet
  for c, ab in cAB[::-1]:
    if c > current + len(pool):
      print('No')
      return
    matched[ab] = []
    while c > current:
      # take c - current from pool, add to matched
      c -= 1
      matched[ab].append(pool.popleft())
    # add (ab, Bc[ab]) to pool
    current -= Bc[ab]
    for _ in range(Bc[ab]):
      pool.append(ab)
  #print(pool, matched)
  skipped = []
  for c, ab in cAB:
    # take c - matched[ab] from pool, add to matched
    nn = Ac[ab]-len(matched[ab])
    while skipped and  Ac[ab]-len(matched[ab]) > 0:
      matched[ab].append(skipped.pop())
    while Ac[ab]-len(matched[ab]) > 0:
      p = pool.popleft()
      if p == ab:
        skipped.append(p)
      else:
        matched[ab].append(p)
      #print(ab, pool, matched, skipped)

  ans = []
  for a, bb in sorted(matched.items()):
    ans += [str(b) for b in bb]
  print('Yes')
  print(' '.join(ans))


def main():
  input = sys.stdin.readline
  n = int(input())
  A = list(map(int, input().split()))
  B = list(map(int, input().split()))
  solve(n, A, B)

main()

Buggy submission (verdict: Runtime Error):
import collections
import sys

def solve(n, A, B):
  Ac = {}
  ABc = {}
  for a in A:
    Ac[a] = 1 + Ac.get(a, 0)
    ABc[a] = 1 + ABc.get(a, 0)
  Bc = {}
  for b in B:
    Bc[b]  = 1 + Bc.get(b, 0)
    ABc[b] = 1 + ABc.get(b, 0)
  cAB = sorted([(c, ab) for ab, c in ABc.items()])
  #print(cAB)
  pool = collections.deque()  #[bbbb]   # [(b, count)], FIFO to match at the end

  matched = {}  # {a: bbbb}    # {a: {b, count}
  current = n   # how many are not matched yet
  for c, ab in cAB[::-1]:
    if c > current + len(pool):
      print('No')
      return
    matched[ab] = []
    while c > current:
      # take c - current from pool, add to matched
      c -= 1
      matched[ab].append(pool.popleft())
    # add (ab, Bc[ab]) to pool
    current -= Bc[ab]
    for _ in range(Bc[ab]):
      pool.append(ab)
  #print(pool, matched)
  skipped = []
  for c, ab in cAB:
    # take c - matched[ab] from pool, add to matched
    nn = Ac[ab]-len(matched[ab])
    while skipped and  Ac[ab]-len(matched[ab]) > 0:
      matched[ab].append(skipped.pop())
    while Ac[ab]-len(matched[ab]) > 0:
      p = pool.popleft()
      if p == ab:
        skipped.append(p)
      else:
        matched[ab].append(p)
      #print(ab, pool, matched, skipped)

  ans = []
  for a, bb in sorted(matched.items()):
    ans += [str(b) for b in bb]
  print('Yes')
  print(' '.join(ans))


def main():
  input = sys.stdin.readline
  n = int(input())
  A = list(map(int, input().split()))
  B = list(map(int, input().split()))
  solve(n, A, B)

main()

Buggy submission (verdict: Runtime Error):
import collections
import sys

def solve(n, A, B):
  Ac = {}
  ABc = {}
  for a in A:
    Ac[a] = 1 + Ac.get(a, 0)
    ABc[a] = 1 + ABc.get(a, 0)
  Bc = {}
  for b in B:
    Bc[b]  = 1 + Bc.get(b, 0)
    ABc[b] = 1 + ABc.get(b, 0)
  cAB = sorted([(c, ab) for ab, c in ABc.items()])
  #print(cAB)
  pool = collections.deque()  #[bbbb]   # [(b, count)], FIFO to match at the end

  matched = {}  # {a: bbbb}    # {a: {b, count}
  current = n   # how many are not matched yet
  for c, ab in cAB[::-1]:
    if c > current + len(pool):
      print('No')
      return
    matched[ab] = []
    while c > current:
      # take c - current from pool, add to matched
      c -= 1
      matched[ab].append(pool.popleft())
    # add (ab, Bc[ab]) to pool
    current -= Bc[ab]
    for _ in range(Bc[ab]):
      pool.append(ab)
  #print(pool, matched)
  skipped = []
  for c, ab in cAB[::-1]:
    # take c - matched[ab] from pool, add to matched
    nn = Ac[ab]-len(matched[ab])
    while skipped and  Ac[ab]-len(matched[ab]) > 0:
      matched[ab].append(skipped.pop())
    while Ac[ab]-len(matched[ab]) > 0:
      p = pool.popleft()
      if p == ab:
        skipped.append(p)
      else:
        matched[ab].append(p)
      #print(ab, pool, matched, skipped)

  ans = []
  for a, bb in sorted(matched.items()):
    ans += [str(b) for b in bb]
  print('Yes')
  print(' '.join(ans))


def main():
  input = sys.stdin.readline
  n = int(input())
  A = list(map(int, input().split()))
  B = list(map(int, input().split()))
  solve(n, A, B)

main()

Buggy submission (verdict: Runtime Error):
import collections
import sys

def solve(n, A, B):
  Ac = {}
  ABc = {}
  for a in A:
    Ac[a] = 1 + Ac.get(a, 0)
    ABc[a] = 1 + ABc.get(a, 0)
  Bc = {}
  for b in B:
    Bc[b]  = 1 + Bc.get(b, 0)
    ABc[b] = 1 + ABc.get(b, 0)
  cAB = sorted([(c, ab) for ab, c in ABc.items()])
  #print(cAB)
  pool = collections.deque()  #[bbbb]   # [(b, count)], FIFO to match at the end

  matched = {}  # {a: bbbb}    # {a: {b, count}
  current = n   # how many are not matched yet
  for c, ab in cAB[::-1]:
    if c > current + len(pool):
      print('No')
      return
    matched[ab] = []
    while c > current:
      # take c - current from pool, add to matched
      c -= 1
      matched[ab].append(pool.popleft())
    # add (ab, Bc[ab]) to pool
    current -= Bc[ab]
    for _ in range(Bc[ab]):
      pool.append(ab)
  #print(pool, matched)
  skipped = []
  for c, ab in cAB[::-1]:
    # take c - matched[ab] from pool, add to matched
    nn = Ac[ab]-len(matched[ab])
    while skipped and  Ac[ab]-len(matched[ab]) > 0:
      matched[ab].append(skipped.pop())
    while Ac[ab]-len(matched[ab]) > 0:
      p = pool.pop()
      if p == ab:
        skipped.append(p)
      else:
        matched[ab].append(p)
      #print(pool, matched)

  ans = []
  for a, bb in sorted(matched.items()):
    ans += [str(b) for b in bb]
  print('Yes')
  print(' '.join(ans))


def main():
  input = sys.stdin.readline
  n = int(input())
  A = list(map(int, input().split()))
  B = list(map(int, input().split()))
  solve(n, A, B)

main()

Buggy submission (verdict: Runtime Error):
import collections
import sys

def solve(n, A, B):
  Ac = {}
  ABc = {}
  for a in A:
    Ac[a] = 1 + Ac.get(a, 0)
    ABc[a] = 1 + ABc.get(a, 0)
  Bc = {}
  for b in B:
    Bc[b]  = 1 + Bc.get(b, 0)
    ABc[b] = 1 + ABc.get(b, 0)
  cAB = sorted([(c, ab) for ab, c in ABc.items()])
  #print(cAB)
  pool = collections.deque()  #[bbbb]   # [(b, count)], FIFO to match at the end

  matched = {}  # {a: bbbb}    # {a: {b, count}
  current = n   # how many are not matched yet
  for c, ab in cAB[::-1]:
    if c > current + len(pool):
      print('No')
      return
    matched[ab] = []
    while c > current:
      # take c - current from pool, add to matched
      c -= 1
      matched[ab].append(pool.popleft())
    # add (ab, Bc[ab]) to pool
    current -= Bc[ab]
    for _ in range(Bc[ab]):
      pool.append(ab)
  #  print(pool, matched)
  skipped = []
  for c, ab in cAB:
    # take c - matched[ab] from pool, add to matched
    nn = Ac[ab]-len(matched[ab])
    while skipped and  Ac[ab]-len(matched[ab]) > 0:
      matched[ab].append(skipped.pop())
    while Ac[ab]-len(matched[ab]) > 0:
      p = pool.popleft()
      if p == ab:
        skipped.append(p)
      else:
        matched[ab].append(p)
   # print(pool, matched)

  ans = []
  for a, bb in sorted(matched.items()):
    ans += [str(b) for b in bb]
  print('Yes')
  print(' '.join(ans))


def main():
  input = sys.stdin.readline
  n = int(input())
  A = list(map(int, input().split()))
  B = list(map(int, input().split()))
  solve(n, A, B)

main()