Codeforces Round #563 (Div. 2)B

B.Ehab Is an Odd Person

Topic links: http://codeforces.com/contest/1174/problem/B

topic

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.

The meaning of problems

Give you an array of length a, and two numbers selected odd, exchange the two numbers, to minimize the lexicographic array, the array output after conversion.

Thinking

Count the number of odd and even, odd or only even only once, then there is an odd number and the number of two, so that the original array output directly on the line,

As long as there is even or odd, it may be adjusted by various methods into a monotonically increasing array, so long as the output can be sorted after it.

 

 

//
// 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;
}

 

Guess you like

Origin www.cnblogs.com/Vampire6/p/10989806.html