1638B.md (1827B)
1 Source: https://codeforces.com/problemset/problem/1638/B 2 3 B. Odd Swap Sort 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You are given an array $a_1, a_2, \dots, a_n$. You can perform operations on the array. In each operation you can choose an integer $i$ ($1 \le i \lt n$), and swap elements $a_i$ and $a_{i+1}$ of the array, if $a_i + a_{i+1}$ is odd. 22 23 Determine whether it can be sorted in non-decreasing order using this operation any number of times. 24 25 Input 26 27 Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. Description of the test cases follows. 28 29 The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the length of the array. 30 31 The second line of each test case contains $n$ integers $a_1,a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the elements of the array. 32 33 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. 34 35 Output 36 37 For each test case, print "Yes" or "No" depending on whether you can or can not sort the given array. 38 39 You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer). 40 41 Example 42 43 Input 44 45 ``` 46 4 47 48 4 49 50 1 6 31 14 51 52 2 53 54 4 2 55 56 5 57 58 2 9 6 7 10 59 60 3 61 62 6 6 6 63 ``` 64 65 Output 66 67 ``` 68 Yes 69 No 70 No 71 Yes 72 ``` 73 74 Note 75 76 In the first test case, we can simply swap $31$ and $14$ ($31 + 14 = 45$ which is odd) and obtain the non-decreasing array $[1,6,14,31]$. 77 78 In the second test case, the only way we could sort the array is by swapping $4$ and $2$, but this is impossible, since their sum $4 + 2 = 6$ is even. 79 80 In the third test case, there is no way to make the array non-decreasing. 81 82 In the fourth test case, the array is already non-decreasing.