103011: [Atcoder]ABC301 B - Fill the Gaps

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:10 Solved:0

Description

Score : $200$ points

Problem Statement

We have a sequence of length $N$ consisting of positive integers: $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 $A$ has an absolute difference of $1$, terminate the procedure.
  2. Let $A_i, A_{i+1}$ be the pair of adjacent terms nearest to the beginning of $A$ whose absolute difference is not $1$.
    • If $A_i < A_{i+1}$, insert $A_i+1,A_i+2,\ldots,A_{i+1}-1$ between $A_i$ and $A_{i+1}$.
    • If $A_i > A_{i+1}$, insert $A_i-1,A_i-2,\ldots,A_{i+1}+1$ between $A_i$ and $A_{i+1}$.
  3. Return to step 1.

Print the sequence when the procedure ends.

Constraints

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

Input

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

$N$
$S$

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)$. The procedure goes as follows.

  • Insert $3,4$ between the first term $2$ and the second term $5$, making the sequence $(2,3,4,5,1,2)$.
  • Insert $4,3,2$ between the fourth term $5$ and the fifth term $1$, making the sequence $(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.

Input

题意翻译

给你一个长度为 $n$ 的数列,保证任意相邻的两个数不相等。尽可能多的插入一些数,使得 $a_i$ 到 $a_j$ 间的这个数列具有单调性,最后输出插入后的数组。

Output

分数:200分

问题描述

我们有一个由正整数组成的长度为$N$的序列:$A=(A_1,\ldots,A_N)$。任意两个相邻的项都有不同的值。

按照以下过程在该序列中插入一些数字。

  1. 如果$A$中任意相邻的两项之间的绝对差都为1,则终止该过程。
  2. 让$A_i, A_{i+1}$是$A$中最接近开始处的绝对差不为1的相邻两项。
    • 如果$A_i < A_{i+1}$,在$A_i$和$A_{i+1}$之间插入$A_i+1,A_i+2,\ldots,A_{i+1}-1$。
    • 如果$A_i > A_{i+1}$,在$A_i$和$A_{i+1}$之间插入$A_i-1,A_i-2,\ldots,A_{i+1}+1$。
  3. 返回步骤1。

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

约束

  • $2 \leq N \leq 100$
  • $1 \leq A_i \leq 100$
  • $A_i \neq A_{i+1}$
  • 输入中的所有值都是整数。

输入

输入将从标准输入以以下格式给出:

$N$
$A_1$ $A_2$ $\ldots$ $A_N$

输出

在过程结束时,用空格分隔序列中的项。


样例输入1

4
2 5 1 2

样例输出1

2 3 4 5 4 3 2 1 2

初始序列是$(2,5,1,2)$。过程如下。

  • 在第一项$2$和第二项$5$之间插入$3,4$,使序列变为$(2,3,4,5,1,2)$。
  • 在第四项$5$和第五项$1$之间插入$4,3,2$,使序列变为$(2,3,4,5,4,3,2,1,2)$。

样例输入2

6
3 4 5 6 5 4

样例输出2

3 4 5 6 5 4

无法进行插入。

HINT

在任意两个相邻差值不为1的数字直接插入数字,使该段序列为公差为1的等差序列,最终结果是什么?

加入题单

算法标签: