Codeforces Round #563 (Div. 2) 1174B . Ehab Is an Odd Person

B. Ehab Is an Odd Person

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You're given an array a of length n. You can perform the following operation on it as many times as you want:

Pick two integers i and j (1≤i,j≤n) such that ai+aj is odd, then swap ai and aj.
What is lexicographically the smallest array you can obtain?

An array x is lexicographically smaller than an array y if there exists an index i such that xi<yi, and xj=yj for all 1≤j<i. Less formally, at the first index i in which they differ, xi<yi
Input
The first line contains an integer n (1≤n≤105) — the number of elements in the array a.

The second line contains n space-separated integers a1, a2, …, an (1≤ai≤109) — the elements of the array a.

Output

The only line contains n space-separated integers, the lexicographically smallest array you can obtain.

Examples

input

3
4 1 7

output

1 4 7 

input

2
1 1

output

1 1 

Note

In the first example, we can swap 1 and 4 since 1+4=5, which is odd.

思路:

数组中偶数和奇数只要其中一个存在一个,那么就可以把所有从小到大排序。样例:6 8 4 2 1

操作:

只要偶数和奇数只要其中一个存在一个,就sort排序

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define ll long long
#define mes(x,y) memset(x,y,sizeof(x))
#define maxn 2147483648+30
using namespace std;
int main(){
	 std::ios::sync_with_stdio(false);
	 ll x,y,i,j,k,a,b,v[100030];
	 while(cin>>x){
	 	a=0;b=0;
	 	for(i=0;i<x;i++){
	 		cin>>v[i];
	 		if(v[i]%2==0)a++;
	 		else b++;
		 }
		 if(a!=0&&b!=0){
		 	sort(v,v+x);
		 }
		  for(i=0;i<x;i++)cout<<v[i]<<" ";cout<<endl;
	 }
}

发布了148 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44417851/article/details/90761441
今日推荐