首页 > 其他 > 详细

Codeforces Round #563 (Div. 2)B

时间:2019-06-08 10:37:25      阅读:127      评论:0      收藏:0      [点我收藏+]

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

 

Codeforces Round #563 (Div. 2)B

原文:https://www.cnblogs.com/Vampire6/p/10989806.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!