codeforces round #732 div2 C题详解

C: 题目描述:

题目链接:Problem - 1546C - Codeforces

AquaMoon has nn friends. They stand in a row from left to right, and the ii-th friend from the left wears a T-shirt with a number a_iai​ 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 nn 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 tt (1 \leq t \leq 501≤t≤50) — the number of test cases.

The first line of each test case contains a single integer nn (1 \leq n \leq 10^51≤n≤105) — the number of Aquamoon's friends.

The second line contains nn integers a_1, a_2, \dots, a_na1​,a2​,…,an​ (1 \leq a_i \leq 10^51≤ai​≤105) — the numbers, written on the T-shirts.

It is guaranteed that the sum of nn for all test cases does not exceed 10^5105.

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

Sample 1

InputcopyOutputcopy
3
4
4 3 2 5
4
3 3 2 2
5
1 2 3 5 4
YES
YES
NO

Note

The possible list of operations in the first test case:

  1. Swap a_1a1​ and a_2a2​. The resulting sequence is 3, 4, 2, 53,4,2,5. The directions are: left, left, right, right.
  2. Swap a_2a2​ and a_3a3​. The resulting sequence is 3, 2, 4, 53,2,4,5. The directions are: left, left, right, right.
  3. Swap a_1a1​ and a_2a2​. The resulting sequence is 2, 3, 4, 52,3,4,5. The directions are: right, right, right, right.

题意:
一列数,每个数有方向,向左或者向右,初始方向都是向右。可以选择相邻的两个数交换位置,两个数的方向要取反。
问,是否可以将数列交换成非递减数列,并且每个数的方向都向右?

思考:
关键是要发现:两个同奇偶位置上的数经过中间数交换后,其方向都不会改变!
那么,奇数位置上的数可以互换,偶数位置上的数也可以互换,其方向仍是初始方向。
所以就是要看,整个数列能不能只通过奇数位置上的互换和偶数位置上的互换,使得 整个数列不递减!

做法:
将奇数位置上的数,偶数位置上的数分别放到一个数组排好序。
然后依次取出首个元素将两个数组归并。看最终归并的数组是否是不递减的!

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int n,m,a[N],b[N],s[N];
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n;
		int cnt1=0,cnt2=0,cnt=0;
		for(int i=1;i<=n;i++)
		{
			int x;
			cin>>x;
			if(i%2) //如果是偶数则放入数组a中
			a[++cnt1]=x;
			else     //奇数则放入数组b中
			b[++cnt2]=x;
		}
		
		//将a,b数组分别进行排序
		sort(a+1,a+cnt1+1);
		sort(b+1,b+cnt2+1);
		
		//将奇数偶数对应位置排放在数组s的对应位置
		int i=1,j=1;
		for(int k=1;k<=n;k++)
		{
			if(k%2) 
			s[++cnt]=a[i],i++;
			else 
			s[++cnt]=b[j],j++;
		}
		
		int flag=0;
		for(int i=1;i<=n;i++)
		{
			if(s[i]<s[i-1]) //如果排序之后不是从小到大进行,定义flag=1;不符合题目要求
			flag=1;
		}
		
		if(flag) 
		cout<<"NO"<<endl;
		else 
		cout<<"YES"<<endl;
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值