Description
ZZX has a sequence of boxes numbered 1, 2, ..., n. Each box can contain at most one ball.
You are given the initial configuration of the balls. For 1 ≤ i ≤ n, if the i-th box is empty then a[i]=0, otherwise the i-th box contains exactly one ball, the color of which is a[i], a positive integer. Balls with the same color cannot be distinguished.
He will perform m operations in order. At the i-th operation, he collects all the balls from boxes l[i], l[i]+1, ..., r[i]-1, r[i], and then arbitrarily put them back to these boxes. (Note that each box should always contain at most one ball)
He wants to change the configuration of the balls from a[1..n] to b[1..n] (given in the same format as a[1..n]), using these operations. Please tell him whether it is possible to achieve his goal.
Input
First line contains an integer T. Then T testcases follow.
In each testcase: First line contains two integers n and m. Second line contains a[1], a[2], ..., a[n]. Third line contains b[1], b[2], ..., b[n]. Each of the next m lines contains two integers l[i], r[i].
1 ≤ n ≤ 1000,0 ≤ m ≤ 1000, sum of n over all testcases ≤ 2000, sum of m over all testcases ≤ 2000.
0 ≤ a[i], b[i] ≤ n.
1 ≤ l[i] ≤ r[i] ≤ n.
Output
For each testcase, print "Yes" or "No" in a line.
Sample Input
5
4 1
0 0 1 1
0 1 1 1
1 4
4 1
0 0 1 1
0 0 2 2
1 4
4 2
1 0 0 0
0 0 0 1
1 3
3 4
4 2
1 0 0 0
0 0 0 1
3 4
1 3
5 2
1 1 2 2 0
2 2 1 1 0
1 3
2 4
Sample Output
No
No
Yes
No
Yes
谷歌渣翻:
ZZX 有一系列编号为 1、2、...、n的框。每个盒子最多可以装一个球。
您将获得球的初始配置。对于 1 ≤ i ≤ n,如果第i个盒子为空则a [ i ]=0,否则第i个盒子正好包含一个球,其颜色为a [ i ],一个正整数。无法区分相同颜色的球。
他将按顺序执行m 个操作。在第i次操作时,他从盒子l [ i ], l [ i ]+1, ..., r [ i ]-1, r [ i ] 中收集所有球,然后将它们任意放回这些盒子。(请注意,每个盒子应始终最多包含一个球)
他想使用这些操作将球的配置从a [1.. n ] 更改为b [1.. n ](以与a [ 1.. n ] 相同的格式给出)。请告诉他是否有可能实现他的目标。
第一行包含一个整数T。然后是T个测试用例。
在每个测试用例中:第一行包含两个整数n和m。第二行包含a [1]、a [2]、...、a [ n ]。第三行包含b [1], b [2], ..., b [ n ]。接下来的m行中的每一行都包含两个整数l [ i ], r [ i ]。
1 ≤ n ≤ 1000,0 ≤ m ≤ 1000,所有测试用例的n之和 ≤ 2000,所有测试用例的 m 之和 ≤ 2000。
0 ≤ a [ i ],b [ i ] ≤ n。
1 ≤ l [ i ] ≤ r [ i ] ≤ n。
对于每个测试用例,在一行中打印“是”或“否”。
5
4 1
0 0 1 1
0 1 1 1
1 4
4 1
0 0 1 1
0 0 2 2
1 4
4 2
1 0 0 0
0 0 0 1
1 3
3 4
4 2
1 0 0 0
0 0 0 1
3 4
1 3
5 2
1 1 2 2 0
2 2 1 1 0
1 3
2 4
否
否
是
否
是
思路:最开始先把两个数组元素的种类和数量一一对应,有对不上的直接结束,剩下那些,另开两个标记数组,因为只动a数组,对b数组的元素从头到尾进行标号1~n,遍历a数组在b数组中寻找第一次出现a[i]的元素的位置,并给赋予其相同的标号,元素一样,标号也不同。
例如1 1 2 2 0 标号3 4 1 2 5
2 2 1 1 0 标号1 2 3 4 5
第一次排1-3之间的数,直接对3 4 1排变成1 3 4 2 5,第二次对2-4之间的数排序变成1 2 3 4 5,此时两个原数组可以变成一样。
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <cmath>
using namespace std;
int n;
int a[1010];
int b[1010];
int num[1010];
bool st[1010];
int c[1020];
int d[1010];
struct nod
{
int l,r;
}node[1010];
bool cmp(int a,int b)
{
return a<b;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
c[i]=a[i];
}
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
d[i]=b[i];
}
sort(c+1,c+n+1);
sort(d+1,d+n+1);
int flag=0;
for(int i=1;i<=n;i++)
{
if(c[i]!=d[i])
{
flag=1;
break;
}
}
for(int i=1;i<=m;i++)
{
scanf("%d%d",&node[i].l,&node[i].r);
}
if(flag)
{
cout<<"No"<<endl;
continue;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i]==b[j]&&!st[j])
{
st[j]=1;
num[i]=j;
break;
}
}
}
for(int i=1;i<=m;i++)
{
int a=node[i].l;
int b=node[i].r;
sort(num+a,num+b+1);
}
int cao=0;
for(int i=1;i<=n;i++)
{
if(num[i]!=i)
cao=1;
}
if(cao)
cout<<"No"<<endl;
else
cout<<"Yes"<<endl;
memset(num,0,sizeof num);
memset(st,0,sizeof st);
}
return 0;
}