最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

CF 1851B - Parity Sort

2023-08-16 10:17 作者:您是打尖兒還是住店呢  | 我要投稿

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

Swap two elements ai and aj such that i≠j, ai and aj 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 a = [7,10,1,3,2]. Then we can perform 3 operations to sort the array:

Swap a3=1 and a1=7, since 1 and 7 are odd. We get a = [1,10,7,3,2];

Swap a2=10 and a5=2, since 10 and 2 are even. We get a = [1,2,7,3,10];

Swap a4=3 and a3=7, since 3 and 7 are odd. We get a = [1,2,3,7,10].

Input

The first line of input data contains a single integer t

?(1≤t≤104) — the number of test cases.

The description of the test cases follows.

The first line of each test case contains one integer n (1≤n≤2?105) — the length of array a.

The second line of each test case contains exactly n positive integers a1,a2,…,an (1≤ai≤109) — the elements of array a.


It is guaranteed that the sum of n over all test cases does not exceed 2?105.


Output

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).

------------------------------------------------------------

您有一個長度為 n 的整數(shù)數(shù)組 a。 您可以對給定數(shù)組應用以下操作:

交換兩個元素 ai 和 aj,使得 i≠j,ai 和 aj 要么都是偶數(shù),要么都是奇數(shù)。

通過執(zhí)行任意次數(shù)(可能為零)的操作,確定是否可以按非降序對數(shù)組進行排序。

例如,令 a = [7,10,1,3,2]。 然后我們可以執(zhí)行 3 個操作來對數(shù)組進行排序:

交換 a3=1 和 a1=7,因為 1 和 7 是奇數(shù)。 我們得到 a = [1,10,7,3,2];

交換 a2=10 和 a5=2,因為 10 和 2 是偶數(shù)。 我們得到 a = [1,2,7,3,10];

交換 a4=3 和 a3=7,因為 3 和 7 是奇數(shù)。 我們得到 a = [1,2,3,7,10]。

輸入

輸入數(shù)據(jù)的第一行包含一個整數(shù) t

? (1≤t≤104) — 測試用例的數(shù)量。

測試用例的描述如下。

每個測試用例的第一行包含一個整數(shù) n (1≤n≤2?105) — 數(shù)組 a 的長度。

每個測試用例的第二行恰好包含 n 個正整數(shù) a1,a2,…,an (1≤ai≤109) — 數(shù)組 a 的元素。


保證所有測試用例的 n 之和不超過 2?105。


輸出

對于每個測試用例,在單獨的行上輸出:

如果可以通過對數(shù)組應用多次操作來對數(shù)組進行排序,則為 YES;

否則不行。

您可以在任何情況下輸出 YES 和 NO(例如,字符串 yEs、yes、Yes 和 YES 將被識別為肯定響應)。

-----------------------------------

一開始用2個list去存儲奇數(shù)跟偶數(shù),然后根據(jù)順序去判斷是否成立,但是這種情況會超時,于是就換成了2個數(shù)組直接存儲數(shù)據(jù),然后排序其中一個數(shù)組,去跟之前那個數(shù)組去比較數(shù)字的奇偶性,這樣還是會超時,真的是奇了怪了,于是把另一個數(shù)組的類型從int改成Integer,結果就過了,不懂為什么了,(可能是引用類型的原因)下面是代碼:


CF 1851B - Parity Sort的評論 (共 條)

分享到微博請遵守國家法律
兴文县| 湄潭县| 崇仁县| 炉霍县| 吉首市| 腾冲县| 闸北区| 长沙县| 浪卡子县| 永平县| 浮梁县| 五大连池市| 织金县| 灵宝市| 和硕县| 平利县| 改则县| 海兴县| 龙南县| 龙陵县| 木兰县| 乌鲁木齐县| 富锦市| 苏尼特右旗| 安顺市| 象州县| 浮梁县| 郯城县| 上饶县| 丽水市| 达拉特旗| 镶黄旗| 兴安县| 定南县| 怀宁县| 韶山市| 开阳县| 呼玛县| 科尔| 广水市| 突泉县|