C. Isamatdin and His Magic Wand!

time limit per test

2 seconds

memory limit per test

256 megabytes

Isamatdin has n toys arranged in a row. The i-th toy has an integer ai. 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) only if aimod2≠ajmod2, where mod --- is the remainder of integer division.

Now he wants to know the lexicographically smallest∗ arrangement he can achieve using this broken wand.

∗A sequence p is lexicographically smaller than a sequence q if there exists an index i such that pj=qj for all j<i, and pi<qi.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤2⋅105) --- the number of toys.

The second line of each test case contains n integers a1,a2,...,an (1≤ai≤109) --- the integers of the toys.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output

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

Example

Input

Copy

复制代码

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

Output

Copy

复制代码
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 

Note

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

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

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

解题说明:此题其实就判断数列中是否同时存在奇数和偶数,如果都存在就能排序,否则无法排序。

cpp 复制代码
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main() 
{
	int q; 
	cin >> q;
	while (q--)
	{
		int n; 
		cin >> n;
		int a[n + 1] = { 0 };
		int f1 = 0, f2 = 0;
		for (int i = 0; i < n; i++) 
		{
			cin >> a[i];
			if (a[i] % 2 == 0)
			{
				f1 = 1;
			}
			else
			{
				f2 = 1;
			}
		}
		if (f1 && f2)
		{
			sort(a, a + n);
		}
		for (int i = 0; i < n; i++)
		{
			cout << a[i] << ' ';
		}
		cout << endl;
	}
	return 0;
}
相关推荐
凯子坚持 c13 分钟前
C++大模型SDK开发实录(一):spdlog日志封装、通用数据结构定义与策略模式应用
数据结构·c++·sdk·策略模式
漫随流水24 分钟前
leetcode算法(513.找树左下角的值)
数据结构·算法·leetcode·二叉树
全栈游侠44 分钟前
数据结构 -数组
数据结构
囊中之锥.1 小时前
机器学习算法详解:DBSCAN 聚类原理、实现流程与优缺点分析
算法·机器学习·聚类
一路往蓝-Anbo1 小时前
【第42期】调试进阶(一):IDE中的Register与Memory窗口
c语言·开发语言·ide·stm32·单片机·嵌入式硬件
AlenTech1 小时前
152. 乘积最大子数组 - 力扣(LeetCode)
算法·leetcode·职场和发展
Piar1231sdafa1 小时前
基于yolo13-C3k2-RVB的洗手步骤识别与检测系统实现_1
人工智能·算法·目标跟踪
做科研的周师兄1 小时前
【MATLAB 实战】|多波段栅格数据提取部分波段均值——批量处理(NoData 修正 + 地理信息保真)_后附完整代码
前端·算法·机器学习·matlab·均值算法·分类·数据挖掘
天赐学c语言2 小时前
1.18 - 滑动窗口最大值 && 子类的指针转换为父类的指针,指针的值是否会改变
数据结构·c++·算法·leecode
黑不溜秋的2 小时前
C++ 线性探测法哈希表
数据结构·散列表