2016 Multi-University Training 8 Ball

该博客讨论了一个关于操作序列的问题,ZZX有一个装有球的盒子,每个盒子最多装一个球。给定一系列操作,包括收集和重新分配球,目标是判断能否通过这些操作将球的初始配置转换为特定的目标配置。输入包括操作次数、盒子数量、初始和目标配置,以及操作的范围。博主提出了一个解决方案,首先检查初始和目标配置是否有匹配的球种类和数量,然后再通过排序和标记的方法判断是否可能完成变换。示例展示了某些情况下的正确答案。
摘要由CSDN通过智能技术生成

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个测试用例。

在每个测试用例中:第一行包含两个整数nm。第二行包含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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值