Swaps Again CodeForces - 1365F(思维+构造)

outputstandard output
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid.

Each test case consists of an integer n and two arrays a and b, of size n. If after some (possibly zero) operations described below, array a can be transformed into array b, the input is said to be valid. Otherwise, it is invalid.

An operation on array a is:

select an integer k (1≤k≤⌊n2⌋)
swap the prefix of length k with the suffix of length k
For example, if array a initially is {1,2,3,4,5,6}, after performing an operation with k=2, it is transformed into {5,6,3,4,1,2}.

Given the set of test cases, help them determine if each one is valid or invalid.

Input
The first line contains one integer t (1≤t≤500) — the number of test cases. The description of each test case is as follows.

The first line of each test case contains a single integer n (1≤n≤500) — the size of the arrays.

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

The third line of each test case contains n integers b1, b2, …, bn (1≤bi≤109) — elements of array b.

Output
For each test case, print “Yes” if the given input is valid. Otherwise print “No”.

You may print the answer in any case.

Example
inputCopy
5
2
1 2
2 1
3
1 2 3
1 2 3
3
1 2 4
1 3 4
4
1 2 3 2
3 1 2 2
3
1 2 3
1 3 2
outputCopy
yes
yes
No
yes
No
Note
For the first test case, we can swap prefix a[1:1] with suffix a[2:2] to get a=[2,1].

For the second test case, a is already equal to b.

For the third test case, it is impossible since we cannot obtain 3 in a.

For the fourth test case, we can first swap prefix a[1:1] with suffix a[4:4] to obtain a=[2,2,3,1]. Now we can swap prefix a[1:2] with suffix a[3:4] to obtain a=[3,1,2,2].

For the fifth test case, it is impossible to convert a to b.

真的思维太差了,思维这东西难道真的是天生的吗?
思路:我们可以发现,对于第i个元素和第n-i+1个元素,无论怎么交换,他们两个交换后的位置和,都是n+1.这也就提示我们要把位置和相加为(n+1)的数对放到一起考虑。我们可以把a数组的数对处理出来,b数组的数对处理出来。然后排序之后,查看是否相等就可以了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=5e2+10;
struct node{
	int x,y;
	bool operator<(const node &a)const{
		if(x!=a.x) return x<a.x;
		else return y<a.y;
	}
}p1[maxx],p2[maxx];
int a[maxx],b[maxx];
int n;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		for(int i=1;i<=n;i++) scanf("%d",&b[i]);
		if((n&1)&&(a[n/2+1]!=b[n/2+1])) cout<<"No"<<endl;
		else
		{
			for(int i=1;i<=n/2;i++)
			{
				p1[i].x=a[i];
				p1[i].y=a[n-i+1];
				if(p1[i].x>p1[i].y) swap(p1[i].x,p1[i].y);
				p2[i].x=b[i];
				p2[i].y=b[n-i+1];
				if(p2[i].x>p2[i].y) swap(p2[i].x,p2[i].y);
			}
			sort(p1+1,p1+1+n/2);
			sort(p2+1,p2+1+n/2);
			int flag=1;
			for(int i=1;i<=n/2;i++)
			{
				if(p1[i].x==p2[i].x&&p1[i].y==p2[i].y) {}
				else flag=0;
			}
			if(flag) cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}

努力加油a啊,(o)/~

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值