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.

题意

给你一个长度为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;
}

猜你喜欢

转载自www.cnblogs.com/Vampire6/p/10989806.html