Codeforces Round 1115 (Div. 2)
D
数组替换
Array Replacement
Codeforces
1700
原题
← CE →

数组替换AI (CI翻译)

You are given an array $a$ of length $n$.

给定一个长度为 $n$ 的数组 $a$。AI (CI翻译)

You can perform the following operation any number of times (possibly zero):

你可以执行任意次(也可以不执行)以下操作:AI (CI翻译)

  • Choose an index $i$ ($2 \le i \le n - 1$) such that $a_{i - 1}$ and $a_{i + 1}$ have the same parity.
  • Replace $a_i$ with $a_{i - 1} - a_i + a_{i + 1}$.
  • 选择一个下标 $i$($2 \le i \le n - 1$),满足 $a_{i - 1}$ 和 $a_{i + 1}$ 的奇偶性相同。
  • 将 $a_i$ 替换为 $a_{i - 1} - a_i + a_{i + 1}$。
AI (CI翻译)

Find the lexicographically smallest array that can be obtained after any number of operations.

求经过任意次操作后可以得到的字典序最小数组。AI (CI翻译)

A sequence $x$ is lexicographically smaller than a sequence $y$ of the same length if and only if, in the first position where $x$ and $y$ differ, the element in $x$ is strictly smaller than the corresponding element in $y$.

当且仅当在序列 $x$ 和 $y$ 第一个不同的位置上,$x$ 中的元素严格小于 $y$ 中对应的元素时,长度相同的序列 $x$ 的字典序小于序列 $y$。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

每个输入包含多组测试用例。第一行包含测试用例数 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)

The first line of each test case contains a single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the length of the array $a$.

每个测试用例的第一行包含一个整数 $n$($3 \le n \le 2 \cdot 10^5$)——数组 $a$ 的长度。AI (CI翻译)

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$).

第二行包含 $n$ 个整数 $a_1, a_2, \ldots, a_n$($-10^9 \le a_i \le 10^9$)。AI (CI翻译)

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

保证所有测试用例的 $n$ 之和不超过 $2 \cdot 10^5$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, output $n$ integers — the lexicographically smallest array that can be obtained.

对于每个测试用例,输出 $n$ 个整数,即可以得到的字典序最小数组。AI (CI翻译)

Note

说明

AI (CI翻译)

In the second test case, the initial array is $[1, 2, 3]$. The only valid index to choose is $i = 2$, because $a_1 = 1$ and $a_3 = 3$ have the same parity. Replacing $a_2$ with $1 - 2 + 3 = 2$ leaves the array unchanged. Thus, the minimal array is $[1, 2, 3]$.

在第二个测试用例中,初始数组为 [1, 2, 3]。唯一可以选择的下标是 $i = 2$,因为 $a_1 = 1$ 和 $a_3 = 3$ 的奇偶性相同。将 $a_2$ 替换为 $1 - 2 + 3 = 2$ 后,数组保持不变。因此,最小数组为 [1, 2, 3]。AI (CI翻译)

In the third test case, the initial array is $[10, 10, 8, 4]$. We can perform the following sequence of operations:

在第三个测试用例中,初始数组为 [10, 10, 8, 4]。我们可以按以下顺序执行操作:AI (CI翻译)

  • Choose $i = 2$ ($a_1 = 10$ and $a_3 = 8$ are both even). Replace $a_2$ with $10 - 10 + 8 = 8$. The array becomes $[10, 8, 8, 4]$.
  • Choose $i = 3$ ($a_2 = 8$ and $a_4 = 4$ are both even). Replace $a_3$ with $8 - 8 + 4 = 4$. The array becomes $[10, 8, 4, 4]$.
  • Choose $i = 2$ ($a_1 = 10$ and $a_3 = 4$ are both even). Replace $a_2$ with $10 - 8 + 4 = 6$. The array becomes $[10, 6, 4, 4]$.
  • 选择 $i = 2$($a_1 = 10$ 和 $a_3 = 8$ 均为偶数)。将 $a_2$ 替换为 $10 - 10 + 8 = 8$。数组变为 [10, 8, 8, 4]。
  • 选择 $i = 3$($a_2 = 8$ 和 $a_4 = 4$ 均为偶数)。将 $a_3$ 替换为 $8 - 8 + 4 = 4$。数组变为 [10, 8, 4, 4]。
  • 选择 $i = 2$($a_1 = 10$ 和 $a_3 = 4$ 均为偶数)。将 $a_2$ 替换为 $10 - 8 + 4 = 6$。数组变为 [10, 6, 4, 4]。
AI (CI翻译)

It can be shown that $[10, 6, 4, 4]$ is the lexicographically smallest array obtainable.

可以证明,[10, 6, 4, 4] 是能够得到的字典序最小数组。AI (CI翻译)

样例

Input #1
3
10
100 108 114 118 120 5 7 19 13 11
3
1 2 3
4
10 10 8 4
Output #1
100 102 106 112 120 5 -1 -3 -1 11
1 2 3
10 6 4 4
InputCopy OutputCopy
题目信息
题号
2252D
时间限制
2 s
内存限制
256 MB
标签
贪心
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。