CF2197B.Array and Permutation
普及-
通过率:0%
时间限制:1.50s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Given a permutation p of length n∗ and an array a of length n.
We call the permutation p generating for the array a if the array a can be obtained from the permutation p by applying some number of operations (possibly zero) of the following type:
Choose an index i (1≤i<n) and perform one of two replacements:
- pi:=pi+1;
- pi+1:=pi.
In other words, in one operation, you can choose two adjacent elements of the array and copy the value of one into the other.
You are required to report whether the permutation p is generating for the array a.
∗A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array), and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
给定一个长度为 n∗ 的排列 p 和一个长度为 n 的数组 a。
若数组 a 可通过对排列 p 执行若干次(可以为零次)如下操作得到,则称排列 p 是数组 a 的生成排列:
选择一个下标 i(满足 1≤i<n),并执行以下两种替换之一:
- pi:=pi+1;
- pi+1:=pi。
换言之,每次操作中,你可以选择数组中两个相邻元素,并将其中一个的值复制到另一个中。
你需要判断排列 p 是否是数组 a 的生成排列。
∗ 长度为 n 的排列是指由 1 到 n 中互不相同的 n 个整数以任意顺序组成的数组。例如,[2,3,1,5,4] 是一个排列,但 [1,2,2] 不是排列(数字 2 在数组中出现了两次),[1,3,4] 也不是排列(此时 n=3,但数组中出现了 4)。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (2≤n≤2⋅105) — the length of the array and the permutation.
The second line of each test case contains n integers p1,p2,…,pn (1≤pi≤n) — the elements of the permutation.
The third line of each test case contains n integers a1,a2,…,an (1≤ai≤n) — the elements of the array.
It is guaranteed that the sum of n across all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤2⋅105)—— 数组与排列的长度。
每个测试用例的第二行包含 n 个整数 p1,p2,…,pn(1≤pi≤n)—— 排列的元素。
每个测试用例的第三行包含 n 个整数 a1,a2,…,an(1≤ai≤n)—— 数组的元素。
保证所有测试用例中 n 的总和不超过 2⋅105。
输出格式
For each test case, output "YES" if the permutation p is generating for the array a, otherwise output "NO".
You may output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.
对于每个测试用例,如果排列 p 是数组 a 的生成排列,则输出 "YES";否则输出 "NO"。
你可以以任意大小写形式输出每个字母(小写或大写)。例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 均被视为正确答案。
输入输出样例
输入#1
6 3 1 2 3 1 2 2 4 3 1 2 4 3 4 2 2 5 1 3 2 5 4 3 3 3 5 4 7 3 7 4 2 1 6 5 3 3 4 4 5 6 5 7 1 2 3 4 5 6 7 7 7 7 7 7 7 7 7 1 3 2 7 5 4 6 2 2 7 7 7 5 6
输出#1
YES NO YES NO YES YES
说明/提示
In the first test case, the array is generated from the permutation using one operation:
- i=2 and pi+1:=pi.
In the second test case, it is impossible to obtain a from p.
In the third test case, it is necessary to perform 2 operations:
- i=1 and pi:=pi+1;
- i=2 and pi+1:=pi.
在第一个测试用例中,数组是通过对排列执行一次操作生成的:
- i=2,且 pi+1:=pi。
在第二个测试用例中,无法通过排列 p 得到数组 a。
在第三个测试用例中,需要执行 2 次操作:
- i=1,且 pi:=pi+1;
- i=2,且 pi+1:=pi。
输入解题思路,AI测评打分。不知道怎么写?