Loading

Codeforces Round #732 (Div. 2) C. AquaMoon and Strange Sort (思维/排序)

AquaMoon has 𝑛n friends. They stand in a row from left to right, and the 𝑖i-th friend from the left wears a T-shirt with a number 𝑎𝑖ai written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.

AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.

AquaMoon hopes that after some operations, the numbers written on the T-shirt of 𝑛n friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.

Input

The input consists of multiple test cases. The first line contains a single integer 𝑡t (1≤𝑡≤501≤t≤50) — the number of test cases.

The first line of each test case contains a single integer 𝑛n (1≤𝑛≤1051≤n≤105) — the number of Aquamoon's friends.

The second line contains 𝑛n integers 𝑎1,𝑎2,…,𝑎𝑛a1,a2,…,an (1≤𝑎𝑖≤1051≤ai≤105) — the numbers, written on the T-shirts.

It is guaranteed that the sum of 𝑛n for all test cases does not exceed 105105.

Output

For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes).

You can print each letter in any case (upper or lower).

Example

input

Copy

3
4
4 3 2 5
4
3 3 2 2
5
1 2 3 5 4

output

Copy

YES
YES
NO

题意等价于判断一个序列能否通过交换相邻的数使序列单调不减,同时每个数只能被交换偶数次。

由“交换相邻的数” 以及每个数只能被交换偶数次可知,在奇数位置的数只能被交换到奇数位置,偶数位置的数只能被交换到偶数位置,因此分别对奇数和偶数位置的数排序,再把排序后的两个序列的数按位置插回到原来的序列看是否有序即可。

#include <bits/stdc++.h>
using namespace std;
int n, a[100005];
int main() {
	//一个数列 每个数被交换了偶数次(只能和相邻位置交换) 能否得到一个单调不减数列
	//注意数的范围和数列长度是一个数量级的
	//总的交换次数是偶数次
	int t;
	cin >> t;
	while(t--) {
		cin >> n;
		vector<int> v1, v2;
		for(int i = 1; i <= n; i++) {
			cin >> a[i];
			if(i & 1) v1.push_back(a[i]);
			else v2.push_back(a[i]);
		}
		sort(v1.begin(), v1.end());
		sort(v2.begin(), v2.end());
		int cnt1 = 0, cnt2 = 0;
		vector<int> fin;
		for(int i = 1; i <= n; i++) {
			if(i & 1) fin.push_back(v1[cnt1++]);
			else fin.push_back(v2[cnt2++]);
		}
		bool flag = 1;
		for(int i = 1; i < fin.size(); i++) {
			if(fin[i] < fin[i - 1]) {
				flag = 0;
				break;
			}
		}
		if(flag) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}
posted @ 2021-07-13 12:00  脂环  阅读(165)  评论(0编辑  收藏  举报