This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
ProgrammingChallenges/gnomes.py

38 lines
699 B
Python
Raw Permalink Normal View History

2019-09-12 19:59:31 +00:00
#!/usr/bin/env python3
import heapq
def gnome_seq(A, H, n):
R = []
i = 0
while i < len(A):
if not len(H) == 0 and H[0] < A[i]:
R.append(heapq.heappop(H))
else:
R.append(A[i])
i += 1
while len(H) > 0:
R.append(heapq.heappop(H))
return R
if __name__ == "__main__":
string = input().split()
n = int(string[0])
m = int(string[1])
A = []
E = [False] * (n+1)
for i in range(m):
num = int(input())
A.append(num)
E[num] = True
H = []
for i in range(1, n + 1):
if not E[i]:
heapq.heappush(H, i)
for n in gnome_seq(A, H, n):
print(n)