#abc301b. B - Fill the Gaps

B - Fill the Gaps

Score : 200200 points

问题描述

我们有一个长度为 NN 的正整数序列:A=(A1,,AN)A=(A_1,\ldots,A_N)。其中任意两个相邻项的值都不同。

让我们按照以下步骤向该序列中插入一些数字。

  1. 如果 AA 中每一对相邻项之间的绝对差均为 11,则结束该过程。
  2. Ai,Ai+1A_i, A_{i+1} 成为 AA 开头附近绝对差不为 11 的相邻项对。
    • Ai<Ai+1A_i < A_{i+1},在 AiA_iAi+1A_{i+1} 之间插入 Ai+1,Ai+2,,Ai+11A_i+1,A_i+2,\ldots,A_{i+1}-1
    • Ai>Ai+1A_i > A_{i+1},在 AiA_iAi+1A_{i+1} 之间插入 Ai1,Ai2,,Ai+1+1A_i-1,A_i-2,\ldots,A_{i+1}+1
  3. 返回步骤 1。

当过程结束时,打印出该序列。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have a sequence of length NN consisting of positive integers: A=(A1,,AN)A=(A_1,\ldots,A_N). Any two adjacent terms have different values.

Let us insert some numbers into this sequence by the following procedure.

  1. If every pair of adjacent terms in AA has an absolute difference of 11, terminate the procedure.
  2. Let Ai,Ai+1A_i, A_{i+1} be the pair of adjacent terms nearest to the beginning of AA whose absolute difference is not 11.
    • If Ai<Ai+1A_i < A_{i+1}, insert Ai+1,Ai+2,,Ai+11A_i+1,A_i+2,\ldots,A_{i+1}-1 between AiA_i and Ai+1A_{i+1}.
    • If Ai>Ai+1A_i > A_{i+1}, insert Ai1,Ai2,,Ai+1+1A_i-1,A_i-2,\ldots,A_{i+1}+1 between AiA_i and Ai+1A_{i+1}.
  3. Return to step 1.

Print the sequence when the procedure ends.

Constraints

  • 2N1002 \leq N \leq 100
  • 1Ai1001 \leq A_i \leq 100
  • AiAi+1A_i \neq A_{i+1}
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \ldots ANA_N

Output

Print the terms in the sequence when the procedure ends, separated by spaces.

Sample Input 1

4
2 5 1 2

Sample Output 1

2 3 4 5 4 3 2 1 2

The initial sequence is (2,5,1,2)(2,5,1,2). The procedure goes as follows.

  • Insert 3,43,4 between the first term 22 and the second term 55, making the sequence (2,3,4,5,1,2)(2,3,4,5,1,2).
  • Insert 4,3,24,3,2 between the fourth term 55 and the fifth term 11, making the sequence (2,3,4,5,4,3,2,1,2)(2,3,4,5,4,3,2,1,2).

Sample Input 2

6
3 4 5 6 5 4

Sample Output 2

3 4 5 6 5 4

No insertions may be performed.

update @ 2024/3/10 08:26:44