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