You are given two arrays of integers a1,a2,…,an and b1,b2,…,bn.
Let's define a transformation of the array a:
- Choose any non-negative integer k such that 0≤k≤n.
- Choose k distinct array indices 1≤i1<i2<…<ik≤n.
- Add 1 to each of ai1,ai2,…,aik, all other elements of array a remain unchanged.
- Permute the elements of array a in any order.
Is it possible to perform some transformation of the array a exactly once, so that the resulting array is equal to b?
Input
The first line contains a single integer t (1≤t≤100) — the number of test cases. Descriptions of test cases follow.
The first line of each test case contains a single integer n (1≤n≤100) — the size of arrays a and b.
The second line of each test case contains n integers a1,a2,…,an (−100≤ai≤100).
The third line of each test case contains n integers b1,b2,…,bn (−100≤bi≤100).
Output
For each test case, print "YES" (without quotes) if it is possible to perform a transformation of the array a, so that the resulting array is equal to b. Print "NO" (without quotes) otherwise.
You can print each letter in any case (upper or lower).
Example
input
Copy
3 3 -1 1 0 0 0 2 1 0 2 5 1 2 3 4 5 1 2 3 4 5
output
Copy
YES NO YES
Note
In the first test case, we can make the following transformation:
- Choose k=2.
- Choose i1=1, i2=2.
- Add 1 to a1 and a2. The resulting array is [0,2,0].
- Swap the elements on the second and third positions.
In the second test case there is no suitable transformation.
In the third test case we choose k=0 and do not change the order of elements.
思路:(类似于 st[a[i]] 这种方法的,我喜欢叫做哈希,笑死),由上向下推(答案推条件)当a于b两数组的每种数字的个数相等时,我们即可判定为成功,因为a数组的顺序可以随便更改,哈希时可以在每个数上都加上100,它们的每种的个数不变,因为哈希下标不能为负数。用sum来记录a哈希与b哈希的数量(就是记录两个哈希的变化)。因为最多加一次,加1,我们能知道两个相邻的数个数必须相等(a与b混合时),如果出现断层那就可以直接判负,如0与2少了1,0加1怎么也得不到2.a中的数必须先开始,要不然就成了b的数组由减得到a了,最后两个哈希值相加要为0,要不然就是a的数多了,或b的数多了。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
int a[210]={0},b[210]={0},x;
cin>>n;
for(int i=0;i<n;i++)cin>>x,a[x+102]++;
for(int i=0;i<n;i++)cin>>x,b[x+102]++;
ll sum=0;
for(int i=2;i<210;i++)
{
sum=b[i]-a[i]+sum;//累计b比a多出的个数
if(sum>0||sum<-a[i])//sum>0说明b先开始,b有更小的||即下一次没有回升,即连着两次是a,即a=-1,a=0,而b=1,相差大于1
{ //并且在sum与a[i]比较时不能出现断层,即0与2的样例输入,没有1就会出现断层,差值会大于1
cout<<"NO"<<endl;
break;
}
}
if(sum==0)
{
cout<<"YES"<<endl;
}
}
}