CF2167C.Isamatdin and His Magic Wand!

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Isamatdin has nn toys arranged in a row. The ii-th toy has an integer aia_i. He wanted to sort them because otherwise, his mother would scold him.

However, Isamatdin never liked arranging toys in order, so his friend JahonaliX gave him a magic wand to help. Unfortunately, JahonaliX made a small mistake while creating the wand.

But Isamatdin couldn't wait any longer and decided to use the broken wand anyway. The wand can only swap two toys if their integers have different parity (one is even, the other is odd). In other words, you can swap toys in positions (i,j)(i, j) only if ai mod 2≠aj mod 2a_i \bmod 2 \neq a_j \bmod 2, where  mod \bmod — is the remainder of integer division.

Now he wants to know the lexicographically smallest∗^{\text{∗}} arrangement he can achieve using this broken wand.

∗^{\text{∗}}A sequence pp is lexicographically smaller than a sequence qq if there exists an index ii such that pj=qjp_j = q_j for all j<ij \lt i, and pi<qip_i \lt q_i.

伊萨马特丁有 nn 个玩具排成一排。第 ii 个玩具上标有一个整数 aia_i。他想把这些玩具按顺序排列,否则他的妈妈会责骂他。

然而,伊萨马特丁向来不喜欢按顺序整理玩具,于是他的朋友贾洪阿里克斯送给他一根魔法棒来帮忙。不幸的是,贾洪阿里克斯在制作这根魔法棒时犯了一个小错误。

但伊萨马特丁已等不及了,决定无论如何都要使用这根损坏的魔法棒。该魔法棒仅允许交换两个整数奇偶性不同的玩具(即一个为偶数,另一个为奇数)。换言之,你只能在位置 (i,j)(i, j) 处交换玩具,当且仅当 ai mod 2≠aj mod 2a_i \bmod 2 \neq a_j \bmod 2,其中  mod \bmod 表示整数除法的余数。

现在,他想知道利用这根损坏的魔法棒所能得到的字典序最小∗^{\text{∗}} 的排列。

∗^{\text{∗}} 序列 pp 比序列 qq 字典序更小,当且仅当存在某个下标 ii,使得对所有 j<ij \lt i 都有 pj=qjp_j = q_j,且 pi<qip_i \lt q_i。

输入格式

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 (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of toys.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1091 \le a_i \le 10^9) — the integers of the toys.

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(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 玩具的数量。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1091 \le a_i \le 10^9)—— 玩具对应的整数。

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

输出格式

For each test case, output nn integers — the lexicographically smallest sequence that can be obtained using the described operation.

对于每个测试用例,输出 nn 个整数——即通过执行上述操作所能得到的字典序最小的序列。

输入输出样例

  • 输入#1

    7
    4
    2 3 1 4
    5
    3 2 1 3 4
    4
    3 7 5 1
    2
    1000000000 2
    3
    1 3 5
    5
    2 5 3 1 7
    4
    2 4 8 6

    输出#1

    1 2 3 4 
    1 2 3 3 4 
    3 7 5 1 
    1000000000 2 
    1 3 5 
    1 2 3 5 7 
    2 4 8 6

说明/提示

In the first test case, we can swap positions (1,3)(1, 3) and then (2,3)(2, 3).

In the second test case, we can swap positions (1,2)(1, 2), (1,3)(1, 3), and then (2,3)(2, 3).

In the third and fourth test cases, we can't swap any positions because all toy integers have the same parity.

在第一个测试用例中,我们可以交换位置 (1,3)(1, 3),然后交换位置 (2,3)(2, 3)。

在第二个测试用例中,我们可以依次交换位置 (1,2)(1, 2)、(1,3)(1, 3) 和 (2,3)(2, 3)。

在第三和第四个测试用例中,我们无法交换任何位置,因为所有玩具整数的奇偶性均相同。

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

首页