B - Fill the Gaps Editorial by evima
Proposer's ImplementationInstead of the given procedure, you can do the following to get the same output.
- Print \(A_1\).
- If \(A_1 < A_2\), print \(A_1 + 1, A_1 + 2, \ldots, A_2 - 1\) in this order. Otherwise, print \(A_1 - 1, A_1 - 2, \ldots, A_2 + 1\) in this order. (When \(A_2 - A_1 = \pm 1\), nothing will be printed.)
- Print \(A_2\).
- Print the numbers between \(A_2\) and \(A_3\), similarly to the above.
- Print \(A_3\).
- \(\vdots\)
- Print \(A_{N-1}\).
- Print the numbers between \(A_{N-1}\) and \(A_N\), similarly to the above.
- Print \(A_N\).
Sample Implementation (Python)
N, A = int(input()), list(map(int, input().split()))
for i in range(N - 1):
print(A[i], end=' ')
if A[i] < A[i + 1]:
for x in range(A[i] + 1, A[i + 1]):
print(x, end=' ')
else:
for x in range(A[i] - 1, A[i + 1], -1):
print(x, end=' ')
print(A[-1])
posted:
last update: