CodeForces_1365F Swaps Again(排序)

Swaps Again

time limit per test:2 seconds
memory limit per test:256 megabytes
Problem Description

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 n 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 k k ( 1 ≤ k ≤ ⌊ n 2 ⌋ 1≤k≤⌊\frac{n}{2}⌋ 1k2n)
  • swap the prefix of length k with the suffix of length k k k

For example, if array a initially is {1,2,3,4,5,6}, after performing an operation with k = 2 k=2 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 t t ( 1 ≤ t ≤ 500 1≤t≤500 1t500) — 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 n n ( 1 ≤ n ≤ 500 1≤n≤500 1n500) — the size of the arrays.

The second line of each test case contains n integers a 1 a_1 a1, a 2 a_2 a2, …, a n a_n an ( 1 ≤ a i ≤ 1 0 9 1≤a_i≤10^9 1ai109) — elements of array a.

The third line of each test case contains n integers b 1 b_1 b1, b 2 b_2 b2, …, b n b_n bn ( 1 ≤ b i ≤ 1 0 9 1≤b_i≤10^9 1bi109) — 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.

Sample Input

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

Sample Output

yes
yes
No
yes
No

题意

对于一个长度为n的序列,可以执行以下操作:
选择一个正整数k( 1 ≤ k ≤ ⌊ n 2 ⌋ 1≤k≤⌊\frac{n}{2}⌋ 1k2n);
将序列前k个与后k个数交换位置,换句话说就是对于 1 ≤ i ≤ k 1\le i \le k 1ik, swap( a i a_i ai, a n − k + i a_{n-k+i} ank+i).
有两个长度相同的数组a,b。能对a进行任意次上述操作,能否使两个数组相等。

题解

观察后可以发现数组原下标为 ( i , n + 1 − i ) (i,n+1-i) (i,n+1i)的二元组,无论怎么交换,两者的下标和恒为 n + 1 n+1 n+1,两者的前后关系可变。
将数组中间位置作为对称轴,从对称轴向边界考虑。当考虑到 ( b i , b n + 1 − i ) (b_i,b_{n+1-i}) (bi,bn+1i)时,查找数组a中是否有值相同的二元组(顺序无所谓,且 i + 1 i+1 i+1到对称轴的元素已确定,不再考虑)。设此时找到一个满足条件的二元组,下标为 j j j,可令 k = j k = j k=j执行操作,则次二元组移动至(1,n)的位置(若两者顺序相反,执行一次 k = 1 k = 1 k=1操作即可),然后再执行 k = i k= i k=i操作,即可令 b i = a i b_i = a_i bi=ai b n + 1 − i = a n + 1 − i b_{n+1-i} = a_{n+1-i} bn+1i=an+1i
分别对两个数组求出二元组,判断能否一一对应即可。

#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<iterator>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define eps 1e-6
   
using namespace std;
typedef long long LL;   
typedef pair<int, int> P;
const int maxn = 200100;
const int mod = 1000000007;
struct node{
    int a, b;
}p1[maxn], p2[maxn];
int a[maxn], b[maxn];
bool cmp(node a, node b);

int main()
{
    int t, n, m, i, j, k, sig;
    scanf("%d", &t);
    while(t--)
    {
        sig = 1;
        scanf("%d", &n);
        m = n/2;
        for(i=0;i<n;i++)
            scanf("%d", &a[i]);
        for(i=0;i<n;i++)
            scanf("%d", &b[i]);
        if(n%2 && a[m] != b[m])sig = 0;
        for(i=0;i<m;i++){
            p1[i].a = a[i];
            p1[i].b = a[n-1-i];
            if(p1[i].a > p1[i].b)swap(p1[i].a, p1[i].b);
            p2[i].a = b[i];
            p2[i].b = b[n-1-i];
            if(p2[i].a > p2[i].b)swap(p2[i].a, p2[i].b);
        }
        sort(p1, p1+m, cmp);
        sort(p2, p2+m, cmp);
        for(i=0;i<m;i++)
            if(p1[i].a != p2[i].a || p1[i].b != p2[i].b)sig = 0;
        if(sig)printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

bool cmp(node a, node b)
{
    if(a.a == b.a)return a.b < b.b;
    return a.a < b.a;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值