CF1851B.Parity Sort

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You have an array of integers aa of length nn. You can apply the following operation to the given array:

  • Swap two elements aia_i and aja_j such that i≠ji \neq j, aia_i and aja_j are either both even or both odd.

Determine whether it is possible to sort the array in non-decreasing order by performing the operation any number of times (possibly zero).

For example, let aa = [7,10,1,3,27, 10, 1, 3, 2]. Then we can perform 33 operations to sort the array:

  1. Swap a3=1a_3 = 1 and a1=7a_1 = 7, since 11 and 77 are odd. We get aa = [1,10,7,3,21, 10, 7, 3, 2];
  2. Swap a2=10a_2 = 10 and a5=2a_5 = 2, since 1010 and 22 are even. We get aa = [1,2,7,3,101, 2, 7, 3, 10];
  3. Swap a4=3a_4 = 3 and a3=7a_3 = 7, since 33 and 77 are odd. We get aa = [1,2,3,7,101, 2, 3, 7, 10].

你有一个长度为 nn 的整数数组 aa。你可以对给定数组执行以下操作:

  • 交换两个元素 aia_i 和 aja_j,其中 i≠ji \neq j,且 aia_i 与 aja_j 要么均为偶数,要么均为奇数。

判断是否可以通过任意次数(包括零次)执行该操作,将数组排序为非递减顺序。

例如,设 a=[7,10,1,3,2]a = [7, 10, 1, 3, 2]。那么我们可以通过 33 次操作将数组排序:

  1. 交换 a3=1a_3 = 1 与 a1=7a_1 = 7,因为 11 和 77 均为奇数。得到 a=[1,10,7,3,2]a = [1, 10, 7, 3, 2];
  2. 交换 a2=10a_2 = 10 与 a5=2a_5 = 2,因为 1010 和 22 均为偶数。得到 a=[1,2,7,3,10]a = [1, 2, 7, 3, 10];
  3. 交换 a4=3a_4 = 3 与 a3=7a_3 = 7,因为 33 和 77 均为奇数。得到 a=[1,2,3,7,10]a = [1, 2, 3, 7, 10]。

输入格式

The first line of input data contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The description of the test cases follows.

The first line of each test case contains one integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the length of array aa.

The second line of each test case contains exactly nn positive integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1091 \le a_i \le 10^9) — the elements of array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

输入数据的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示测试用例的数量。

接下来是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 表示数组 aa 的长度。

每个测试用例的第二行包含恰好 nn 个正整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1091 \le a_i \le 10^9)—— 表示数组 aa 的元素。

保证所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output on a separate line:

  • YES if the array can be sorted by applying the operation to it some number of times;
  • NO otherwise.

You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response).

对于每个测试用例,在单独的一行上输出:

  • 如果可以通过对该数组执行若干次该操作使其变为有序,则输出 YES;
  • 否则输出 NO。

YES 和 NO 的大小写不限(例如,字符串 yEs、yes、Yes 和 YES 均被视为肯定回答)。

输入输出样例

  • 输入#1

    6
    5
    7 10 1 3 2
    4
    11 9 3 5
    5
    11 3 15 3 2
    6
    10 7 8 1 2 3
    1
    10
    5
    6 6 4 1 6

    输出#1

    YES
    YES
    NO
    NO
    YES
    NO

说明/提示

The first test case is explained in the problem statement.

第一个测试用例在题目描述中已作解释。

输入解题思路,AI测评打分。不知道怎么写?

首页