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 ? of length ? .You can perform the following operation on it as many times as you want:Pick two integers ? and ? (1≤?,?≤?) such that ??+?? is odd, then swap ??
and ??.What is lexicographically the smallest array you can obtain?
An array ? is lexicographically smaller than an array ? if there exists an index ? such that ??<??, and ??=??for all 1≤?<?. Less formally, at the first index ? in which they differ, ??<??
Input
The first line contains an integer ?
n
(1≤?≤105) — the number of elements in the array ?
The second line contains ?
space-separated integers ?1, ?2, …, ?? (1≤??≤109) — the elements of the array ?
.
Output
The only line contains ? space-separated integers, the lexicographically smallest array you can obtain.
Examples
inputCopy
3
4 1 7
outputCopy
1 4 7
inputCopy
2
1 1
outputCopy
1 1
Note
In the first example, we can swap 1
1
and 4
4
since 1+4=5, which is odd.
#include "iostream"
#include "cstdio"
#include "algorithm"
using namespace std;
const int Max = 100010;
int n, a[Max], b[Max], c[Max], ans[Max], pb, pc, ppb,ppc;
int main()
{
scanf("%d",&n);
for(int i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
if(a[i] & 1)
b[++pb] = a[i];
else
c[++pc] = a[i];
}
if(pb)
sort(c+1, pc+1+c);
if(pc)
sort(b+1,b+pb+1);
ppb = ppc = 1;
for(int i = 1; i <= n; i++)
if(ppb == pb + 1)
ans[i] = b[ppb++];
else if(ppc == pb + 1)
ans[i] = c[ppc++];
else
{
if(b[ppb] == c[ppc])
ans[i] = b[ppb++];
else
ans[i] = c[ppc++];
}
for(int i = 1; i <= n; i++)
printf("%d ",ans[i]);
return 0;
}