B. Ehab Is an Odd Person

B. Ehab Is an Odd Person

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)(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 Input
The first line contains an integer nn (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 nn space-separated integers, the lexicographically smallest array you can obtain.

题意为找ai+aj=奇数就交换这两个数尽可能的是最后的序列字典序最小。
显然如果这个数列全是奇数或者全是偶数就没必要交换否则就直接sort。

#include
#include
#include
#include
#include
using namespace std;
long long int n,m,k,flag=0,a[1000009];
mapb;
int main(){
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        int odd=0,pdd=0;
         for(int i=1;i<=n;i++)
        {
            if(a[i]%2==1)
            {
                odd++;
            }
            else
                pdd++;
        }
        if(odd&&pdd)
            sort(a+1,a+1+n);
        for(int i=1;i<=n;i++)
            cout<

你可能感兴趣的:(题)