DAY 4 A. AquaMoon and Strange Sort

题目要求AquaMoon通过操作使朋友们T恤上的数字非递减,并且所有朋友朝向变为右。每次操作可以选择两个相邻的朋友交换位置并翻转方向。判断是否能达成目标。示例和解释揭示操作次数应为偶数,且与数字原本位置有关。
摘要由CSDN通过智能技术生成

Problem:
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≤t≤50) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤105) — the number of Aquamoon’s friends.
The second line contains n integers a1,a2,…,an (1≤ai≤105) — the numbers, written on the T-shirts.
It is guaranteed that the sum of n for all test cases does not exceed 105.

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
3
4
4 3 2 5
4
3 3 2 2
5
1 2 3 5 4

output
YES
YES
NO

Note
The possible list of operations in the first test case:
Swap a1 and a2. The resulting sequence is 3,4,2,5. The directions are: left, left, right, right.
Swap a2 and a3. The resulting sequence is 3,2,4,5. The directions are: left, left, right, right.
Swap a1 and a2. The resulting sequence is 2,3,4,5. The directions are: right, right, right, right.

题目大致意思为:一共有n个数,每次可以交换一次相邻数字,求在一定条件限制下,能否经过数次操作,最后完成升序排列。通过案例和一些例子不难发现,如果要实现交换到正确位置,那么操作次数必定是(该数字的下标-该数字原本的位置),并且操作次数一定是要偶数次,否则无法还原成right。
直接上代码

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		int n;
		cin >> n;
		int* a = new int[n];
		for (int i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		vector<pair<int, int>>x;
		vector<pair<int, int>>y;
		//用vector记录该数字变换前的下标是奇还是偶 
		//如果变换后的下标与变换前的下标奇偶性一致 那么说明经历了偶数次操作
		for (int i = 0; i < n; i++)
		{
			x.push_back({ a[i], i % 2 });
		}
		sort(a, a + n);
		for (int i = 0; i < n; i++)
		{
			y.push_back({ a[i],i % 2 });
		}
		sort(x.begin(), x.end());
		sort(y.begin(), y.end());
		//将记录的进行排序 判断奇偶性 如果0对0 1对1 那么就是偶数
		if (x == y)
		{
			cout << "YES" << endl;
		}
		else
		{
			cout << "NO" << endl;
		}
		x.clear();
		y.clear();
		delete a;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值