B. 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 ? 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值