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<yi, andxj=yj for all 1≤j<i1≤j<i. Less formally, at the first index i in which they differ, xi<yi
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<iostream>
#include<algorithm>
#include<cmath>
#include<string.h>
#include<map>
using namespace std;
long long int n,m,k,flag=0,a[1000009];
map<int,int>b;
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<<a[i]<<' ';
        cout<<endl;

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值