Restore the Array恢复数组

time limit per test

1 second

memory limit per test

256 megabytes

Kristina had an array aa of length nn consisting of non-negative integers.

She built a new array bb of length n−1n−1, such that bi=max(ai,ai+1)bi=max(ai,ai+1) (1≤i≤n−11≤i≤n−1).

For example, suppose Kristina had an array aa = [3,0,4,0,53,0,4,0,5] of length 55. Then she did the following:

  1. Calculated b1=max(a1,a2)=max(3,0)=3b1=max(a1,a2)=max(3,0)=3;
  2. Calculated b2=max(a2,a3)=max(0,4)=4b2=max(a2,a3)=max(0,4)=4;
  3. Calculated b3=max(a3,a4)=max(4,0)=4b3=max(a3,a4)=max(4,0)=4;
  4. Calculated b4=max(a4,a5)=max(0,5)=5b4=max(a4,a5)=max(0,5)=5.

As a result, she got an array bb = [3,4,4,53,4,4,5] of length 44.

You only know the array bb. Find any matching array aa that Kristina may have originally had.

Input

The first line of input data contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The description of the test cases follows.

The first line of each test case contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of elements in the array aa that Kristina originally had.

The second line of each test case contains exactly n−1n−1 non-negative integer — elements of array bb (0≤bi≤1090≤bi≤109).

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105, and that array bb was built correctly from some array aa.

Output

For each test case on a separate line, print exactly nn non-negative integers — the elements of the array aa that Kristina originally had.

If there are several possible answers — output any of them.

Example

Input

Copy

 

11

5

3 4 4 5

4

2 2 1

5

0 0 0 0

6

0 3 4 4 3

2

10

4

3 3 3

5

4 2 5 5

4

3 3 3

4

2 1 0

3

4 4

6

8 1 3 5 10

Output

Copy

3 0 4 0 5
2 2 1 1
0 0 0 0 0
0 0 3 4 3 3
10 10
3 3 3 1
4 2 2 5 5
3 3 3 3
2 1 0 0
2 4 4
8 1 1 3 5 10

Note

The first test case is explained in the problem statement.

In the second test case, we can get array bb = [2,2,12,2,1] from the array aa = [2,2,1,12,2,1,1]:

  • b1=max(a1,a2)=max(2,2)=2b1=max(a1,a2)=max(2,2)=2;
  • b2=max(a2,a3)=max(2,1)=2b2=max(a2,a3)=max(2,1)=2;
  • b3=max(a3,a4)=max(1,1)=1b3=max(a3,a4)=max(1,1)=1.

In the third test case, all elements of the array bb are zeros. Since each bibi is the maximum of two adjacent elements of array aa, array aa can only consist entirely of zeros.

In the fourth test case, we can get array bb = [0,3,4,4,30,3,4,4,3] from the array aa = [0,0,3,4,3,30,0,3,4,3,3] :

  • b1=max(a1,a2)=max(0,0)=0b1=max(a1,a2)=max(0,0)=0;
  • b2=max(a2,a3)=max(0,3)=3b2=max(a2,a3)=max(0,3)=3;
  • b3=max(a3,a4)=max(3,4)=4b3=max(a3,a4)=max(3,4)=4;
  • b4=max(a4,a5)=max(4,3)=4b4=max(a4,a5)=max(4,3)=4;
  • b5=max(a5,a6)=max(3,3)=3b5=max(a5,a6)=max(3,3)=3.

每个测试的时间限制 1 秒
每个测试的内存限制 256 兆字节
Kristina 有一个长度为 n 的数组 a
,由非负整数组成。

她建立了一个长度为 n−1 的新数组 b
,使得 bi=max(ai,ai+1)
(1≤i≤n−1
)。

例如,假设 Kristina 有一个长度为 5 的数组 a
= [3,0,4,0,5
]
。然后她做了以下事情:

计算出 b1=max(a1,a2)=max(3,0)=3
;
计算出 b2=max(a2,a3)=max(0,4)=4
;
计算出 b3=max(a3,a4)=max(4,0)=4
;
计算出 b4=max(a4,a5)=max(0,5)=5

结果,她得到了一个长度为 4 的数组 b
= [3,4,4,5
]。

你只知道数组 b
。 找到 Kristina 最初可能拥有的任何匹配数组 a

输入
输入数据的第一行包含一个整数 t
(1≤t≤104
) — 测试用例的数量。

测试用例的描述如下。

每个测试用例的第一行包含一个整数 n
(2≤n≤2⋅105
) — Kristina 最初拥有的数组 a
中的元素数量。

每个测试用例的第二行恰好包含 n−1
个非负整数 — 数组 b
的元素 (0≤bi≤109
)。

保证所有测试用例的 n
之和不超过 2⋅105
,并且数组 b
是从某个数组 a
正确构建的。

输出
对于每个测试用例,在单独的一行上打印恰好 n
个非负整数 — Kristina 最初拥有的数组 a
的元素。

如果有多个可能的答案 — 输出其中任何一个。

示例
InputCopy
11
5
3 4 4 5
4
2 2 1
5
0 0 0 0
6
0 3 4 4 3
2
10
4
3 3 3
5
4 2 5 5
4
3 3 3
4
2 1 0
3
4 4
6
8 1 3 5 10
OutputCopy
3 0 4 0 5
2 2 1 1
0 0 0 0 0
0 0 3 4 3 3
10 10
3 3 3 1
4 2 2 5 5
3 3 3 3
2 1 0 0
2 4 4
8 1 1 3 5 10
注意
第一个测试用例在问题陈述中进行了解释。

在第二个测试用例中,我们可以从数组 a
= [2,2,1,1
] 中得到数组 b
= [2,2,1
]:

b1=max(a1,a2)=max(2,2)=2
;
b2=max(a2,a3)=max(2,1)=2
;
b3=max(a3,a4)=max(1,1)=1

在第三个测试用例中,数组 b
的所有元素均为零。由于每个 bi
都是数组 a
中两个相邻元素的最大值,因此数组 a
只能完全由零组成。

在第四个测试用例中,我们可以从数组 a
= [0,0,3,4,3,3
] 中得到数组 b
= [0,3,4,4,3
]:

b1=max(a1,a2)=max(0,0)=0
;
b2=max(a2,a3)=max(0,3)=3
;
b3=最大(a3,a4)=最大(3,4)=4
;
b4=最大(a4,a5)=最大(4,3)=4
;
b5=最大(a5,a6)=最大(3,3)=3

代码:
 

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void solve() {
    int t;
    cin >> t;

    while (t--) {
        int n;
        cin >> n;

        vector<int> b(n - 1);
        for (int i = 0; i < n - 1; i++) {
            cin >> b[i];
        }

        vector<int> a(n);

        a[0] = b[0];
        for (int i = 1; i < n - 1; i++) {
            a[i] = min(b[i - 1], b[i]);
        }
        a[n - 1] = b[n - 2];

        for (int i = 0; i < n; i++) {
            cout << a[i] << " ";
        }
        cout << endl;
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    solve();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值