Codeforces Round #563 (Div. 2) B. Ehab Is an Odd Person(排序)
题目链接:https://codeforces.com/contest/1174/problem/B
题意
有一个大小为 $n$ 的数组,每次可以交换两个奇偶性不同的数,输出数组可以排到的最小字典序。
题解
CF1365B 应该是借鉴了这道题吧。
如果有奇数也有偶数,那么一定可以从小到大排序,否则无法排序,按照原数组输出。
代码
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n] = {}; bool odd = false, even = false; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] & 1) odd = true; else even = true; } if (odd and even) sort(a, a + n); for (int i = 0; i < n; i++) cout << a[i] << ' '; }

浙公网安备 33010602011771号