Codeforces Round #563 (Div. 2)B

B.Ehab Is an Odd Person

题目链接:http://codeforces.com/contest/1174/problem/B

题目

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.

Example

input

3

4 1 7

output

1 4 7

题意

给你一个长度为a的数组,选择两个数之和为奇数,可以交换这两个数,要使得这个数组字典序最小,输出变换后的这个数组。

思路

计算奇数和偶数的个数,一旦只有奇数或者只有偶数,则不存在两个数之和为奇数,故直接将原来数组输出就行,

只要都存在奇数或者偶数,则可以通过各种调法调成单调递增的数组,所以只要将其排序后输出即可。

 

 

//
// Created by hjy on 19-6-4.
//

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int main()
{
    int n;
    while(cin>>n)
    {
        ll a[maxn];
        bool flag= false,flag1=false;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            if(a[i]&1)
                flag=true;
            else
                flag1=true;

        }
        if(flag&&flag1)
            sort(a,a+n);
        copy(a,a+n,ostream_iterator<ll>(cout," "));
        cout<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Vampire6/p/10989806.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值