CF2252D.Array Replacement

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an array aa of length nn.

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

  • Choose an index ii (2≤i≤n−12 \le i \le n - 1) such that ai−1a_{i - 1} and ai+1a_{i + 1} have the same parity.
  • Replace aia_i with ai−1−ai+ai+1a_{i - 1} - a_i + a_{i + 1}.

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

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

给你一个长度为 nn 的数组 aa。

你可以执行以下操作任意次(包括零次):

  • 选择一个下标 ii(满足 2≤i≤n−12 \le i \le n - 1),使得 ai−1a_{i - 1} 与 ai+1a_{i + 1} 具有相同的奇偶性;
  • 将 aia_i 替换为 ai−1−ai+ai+1a_{i - 1} - a_i + a_{i + 1}。

求经过任意次操作后能得到的字典序最小的数组。

当且仅当在 xx 与 yy 首次出现不同元素的位置上,xx 中该位置的元素严格小于 yy 中对应位置的元素时,称序列 xx 的字典序小于同长度的序列 yy。

输入格式

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

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

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (−109≤ai≤109-10^9 \le a_i \le 10^9).

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(3≤n≤2⋅1053 \le n \le 2 \cdot 10^5)——数组 aa 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(−109≤ai≤109-10^9 \le a_i \le 10^9)。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

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

对于每个测试用例,输出 nn 个整数——所能得到的字典序最小的数组。

输入输出样例

  • 输入#1

    3
    10
    100 108 114 118 120 5 7 19 13 11
    3
    1 2 3
    4
    10 10 8 4

    输出#1

    100 102 106 112 120 5 -1 -3 -1 11 
    1 2 3 
    10 6 4 4

说明/提示

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

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

  • Choose i=2i = 2 (a1=10a_1 = 10 and a3=8a_3 = 8 are both even). Replace a2a_2 with 10−10+8=810 - 10 + 8 = 8. The array becomes [10,8,8,4][10, 8, 8, 4].
  • Choose i=3i = 3 (a2=8a_2 = 8 and a4=4a_4 = 4 are both even). Replace a3a_3 with 8−8+4=48 - 8 + 4 = 4. The array becomes [10,8,4,4][10, 8, 4, 4].
  • Choose i=2i = 2 (a1=10a_1 = 10 and a3=4a_3 = 4 are both even). Replace a2a_2 with 10−8+4=610 - 8 + 4 = 6. The array becomes [10,6,4,4][10, 6, 4, 4].

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

在第二个测试用例中,初始数组为 [1,2,3][1, 2, 3]。唯一可选的有效下标是 i=2i = 2,因为 a1=1a_1 = 1 和 a3=3a_3 = 3 具有相同的奇偶性。将 a2a_2 替换为 1−2+3=21 - 2 + 3 = 2 后,数组保持不变。因此,字典序最小的数组为 [1,2,3][1, 2, 3]。

在第三个测试用例中,初始数组为 [10,10,8,4][10, 10, 8, 4]。我们可以执行以下操作序列:

  • 选择 i=2i = 2(a1=10a_1 = 10 和 a3=8a_3 = 8 均为偶数)。将 a2a_2 替换为 10−10+8=810 - 10 + 8 = 8,数组变为 [10,8,8,4][10, 8, 8, 4]。
  • 选择 i=3i = 3(a2=8a_2 = 8 和 a4=4a_4 = 4 均为偶数)。将 a3a_3 替换为 8−8+4=48 - 8 + 4 = 4,数组变为 [10,8,4,4][10, 8, 4, 4]。
  • 选择 i=2i = 2(a1=10a_1 = 10 和 a3=4a_3 = 4 均为偶数)。将 a2a_2 替换为 10−8+4=610 - 8 + 4 = 6,数组变为 [10,6,4,4][10, 6, 4, 4]。

可以证明,[10,6,4,4][10, 6, 4, 4] 是所能得到的字典序最小的数组。

输入解题思路,AI测评打分。不知道怎么写?

首页