给定长度为 m 的序列 T,求一个长度为 n 且字典序最小的排列.并且要求序列 T 为所求排列的子序列(1<=m<=n<=1e6)
思路:长度为n,看样例发现是只包含1~n的元素,因为是要求T只是子序列,直接用双指针比较谁小谁就在前面
def solve(A, n, m):
vis, ans, B = [False for i in range(n+1)], [0 for i in range(n)], []
for x in A:
vis[x] = True
for i in range(1,n+1):
if not vis[i]:
B.append(i)
i, j, k = 0, 0, 0
while i < len(A) and j < len(B):
if A[i] < B[j]: ans[k], k, i = A[i], k+1, i+1
else: ans[k], k, j = B[j], k+1, j+1
while i < len(A): ans[k], k, i = A[i], k+1, i+1
while j < len(B): ans[k], k, j = B[j], k+1, j+1
return ans
n, m = map(int, input().split())
A = list(map(int, input().split()))
ans = solve(A, n, m)
for x in ans:
print(x, end=' ')