2197B.md (2680B)
1 Source: https://codeforces.com/problemset/problem/2197/B 2 3 B. Array and Permutation 4 5 time limit per test 6 7 1.5 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Given a permutation $p$ of length $n$$^{\text{∗}}$ and an array $a$ of length $n$. 22 23 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: 24 25 Choose an index $i$ ($1 \le i \lt n$) and perform one of two replacements: 26 27 * $p_{i} := p_{i + 1}$; 28 * $p_{i + 1} := p_{i}$. 29 30 In other words, in one operation, you can choose two adjacent elements of the array and copy the value of one into the other. 31 32 You are required to report whether the permutation $p$ is generating for the array $a$. 33 34 $^{\text{∗}}$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). 35 36 Input 37 38 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. 39 40 The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^{5}$) — the length of the array and the permutation. 41 42 The second line of each test case contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_{i} \le n$) — the elements of the permutation. 43 44 The third line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_{i} \le n$) — the elements of the array. 45 46 It is guaranteed that the sum of $n$ across all test cases does not exceed $2 \cdot 10^{5}$. 47 48 Output 49 50 For each test case, output "YES" if the permutation $p$ is generating for the array $a$, otherwise output "NO". 51 52 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. 53 54 Example 55 56 Input 57 58 ``` 59 6 60 61 3 62 63 1 2 3 64 65 1 2 2 66 67 4 68 69 3 1 2 4 70 71 3 4 2 2 72 73 5 74 75 1 3 2 5 4 76 77 3 3 3 5 4 78 79 7 80 81 3 7 4 2 1 6 5 82 83 3 3 4 4 5 6 5 84 85 7 86 87 1 2 3 4 5 6 7 88 89 7 7 7 7 7 7 7 90 91 7 92 93 1 3 2 7 5 4 6 94 95 2 2 7 7 7 5 6 96 ``` 97 98 Output 99 100 ``` 101 YES 102 103 NO 104 105 YES 106 107 NO 108 109 YES 110 111 YES 112 ``` 113 114 Note 115 116 In the first test case, the array is generated from the permutation using one operation: 117 118 * $i = 2$ and $p_{i + 1} := p_{i}$. 119 120 In the second test case, it is impossible to obtain $a$ from $p$. 121 122 In the third test case, it is necessary to perform $2$ operations: 123 124 * $i = 1$ and $p_{i} := p_{i + 1}$; 125 * $i = 2$ and $p_{i + 1} := p_{i}$.