1741E.md (3322B)
1 Source: https://codeforces.com/problemset/problem/1741/E 2 3 E. Sending a Sequence Over the Network 4 5 time limit per test 6 7 1 second 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 The sequence $a$ is sent over the network as follows: 22 23 1. sequence $a$ is split into segments (each element of the sequence belongs to exactly one segment, each segment is a group of consecutive elements of sequence); 24 2. for each segment, its length is written next to it, either to the left of it or to the right of it; 25 3. the resulting sequence $b$ is sent over the network. 26 27 For example, we needed to send the sequence $a = [1, 2, 3, 1, 2, 3]$. Suppose it was split into segments as follows: $[\color{red}{1}] + [\color{blue}{2, 3, 1}] + [\color{green}{2, 3}]$. Then we could have the following sequences: 28 29 * $b = [1, \color{red}{1}, 3, \color{blue}{2, 3, 1}, \color{green}{2, 3}, 2]$, 30 * $b = [\color{red}{1}, 1, 3, \color{blue}{2, 3, 1}, 2, \color{green}{2, 3}]$, 31 * $b = [\color{red}{1}, 1, \color{blue}{2, 3, 1}, 3, 2, \color{green}{2, 3}]$, 32 * $b = [\color{red}{1}, 1,\color{blue}{2, 3, 1}, 3, \color{green}{2, 3}, 2]$. 33 34 If a different segmentation had been used, the sent sequence might have been different. 35 36 The sequence $b$ is given. Could the sequence $b$ be sent over the network? In other words, is there such a sequence $a$ that converting $a$ to send it over the network could result in a sequence $b$? 37 38 Input 39 40 The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. 41 42 Each test case consists of two lines. 43 44 The first line of the test case contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the size of the sequence $b$. 45 46 The second line of test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$) — the sequence $b$ itself. 47 48 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. 49 50 Output 51 52 For each test case print on a separate line: 53 54 * YES if sequence $b$ could be sent over the network, that is, if sequence $b$ could be obtained from some sequence $a$ to send $a$ over the network. 55 * NO otherwise. 56 57 You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response). 58 59 Example 60 61 Input 62 63 ``` 64 7 65 66 9 67 68 1 1 2 3 1 3 2 2 3 69 70 5 71 72 12 1 2 7 5 73 74 6 75 76 5 7 8 9 10 3 77 78 4 79 80 4 8 6 2 81 82 2 83 84 3 1 85 86 10 87 88 4 6 2 1 9 4 9 3 4 2 89 90 1 91 92 1 93 ``` 94 95 Output 96 97 ``` 98 YES 99 YES 100 YES 101 NO 102 YES 103 YES 104 NO 105 ``` 106 107 Note 108 109 In the first case, the sequence $b$ could be obtained from the sequence $a = [1, 2, 3, 1, 2, 3]$ with the following partition: $[\color{red}{1}] + [\color{blue}{2, 3, 1}] + [\color{green}{2, 3}]$. The sequence $b$: $[\color{red}{1}, 1, \color{blue}{2, 3, 1}, 3, 2, \color{green}{2, 3}]$. 110 111 In the second case, the sequence $b$ could be obtained from the sequence $a = [12, 7, 5]$ with the following partition: $[\color{red}{12}] + [\color{green}{7, 5}]$. The sequence $b$: $[\color{red}{12}, 1, 2, \color{green}{7, 5}]$. 112 113 In the third case, the sequence $b$ could be obtained from the sequence $a = [7, 8, 9, 10, 3]$ with the following partition: $[\color{red}{7, 8, 9, 10, 3}]$. The sequence $b$: $[5, \color{red}{7, 8, 9, 10, 3}]$. 114 115 In the fourth case, there is no sequence $a$ such that changing $a$ for transmission over the network could produce a sequence $b$.