CF1741E.Sending a Sequence Over the Network

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The sequence aa is sent over the network as follows:

  1. sequence aa is split into segments (each element of the sequence belongs to exactly one segment, each segment is a group of consecutive elements of sequence);
  2. for each segment, its length is written next to it, either to the left of it or to the right of it;
  3. the resulting sequence bb is sent over the network.

For example, we needed to send the sequence a=[1,2,3,1,2,3]a = [1, 2, 3, 1, 2, 3]. Suppose it was split into segments as follows: [1]+[2,3,1]+[2,3][\color{red}{1}] + [\color{blue}{2, 3, 1}] + [\color{green}{2, 3}]. Then we could have the following sequences:

  • b=[1,1,3,2,3,1,2,3,2]b = [1, \color{red}{1}, 3, \color{blue}{2, 3, 1}, \color{green}{2, 3}, 2],
  • b=[1,1,3,2,3,1,2,2,3]b = [\color{red}{1}, 1, 3, \color{blue}{2, 3, 1}, 2, \color{green}{2, 3}],
  • b=[1,1,2,3,1,3,2,2,3]b = [\color{red}{1}, 1, \color{blue}{2, 3, 1}, 3, 2, \color{green}{2, 3}],
  • b=[1,1,2,3,1,3,2,3,2]b = [\color{red}{1}, 1,\color{blue}{2, 3, 1}, 3, \color{green}{2, 3}, 2].

If a different segmentation had been used, the sent sequence might have been different.

The sequence bb is given. Could the sequence bb be sent over the network? In other words, is there such a sequence aa that converting aa to send it over the network could result in a sequence bb?

序列 aa 通过网络传输的方式如下:

  1. 将序列 aa 划分为若干段(序列中的每个元素恰好属于一段,每段均由序列中连续的若干元素组成);
  2. 对每一段,在该段的左侧或右侧紧邻位置写上其长度;
  3. 将由此得到的序列 bb 通过网络发送。

例如,我们需要发送序列 a=[1,2,3,1,2,3]a = [1, 2, 3, 1, 2, 3]。假设将其划分为如下几段:[1]+[2,3,1]+[2,3][\color{red}{1}] + [\color{blue}{2, 3, 1}] + [\color{green}{2, 3}]。那么可能得到以下几种序列 bb:

  • b=[1,1,3,2,3,1,2,3,2]b = [1, \color{red}{1}, 3, \color{blue}{2, 3, 1}, \color{green}{2, 3}, 2],
  • b=[1,1,3,2,3,1,2,2,3]b = [\color{red}{1}, 1, 3, \color{blue}{2, 3, 1}, 2, \color{green}{2, 3}],
  • b=[1,1,2,3,1,3,2,2,3]b = [\color{red}{1}, 1, \color{blue}{2, 3, 1}, 3, 2, \color{green}{2, 3}],
  • b=[1,1,2,3,1,3,2,3,2]b = [\color{red}{1}, 1,\color{blue}{2, 3, 1}, 3, \color{green}{2, 3}, 2]。

若采用不同的分段方式,则发送的序列也可能不同。

现给定序列 bb。该序列 bb 是否可能是通过上述网络传输方式发送的?换言之,是否存在某个序列 aa,使得将 aa 按照上述规则转换后,恰好得到序列 bb?

输入格式

The first line of input data contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

Each test case consists of two lines.

The first line of the test case contains an integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the size of the sequence bb.

The second line of test case contains nn integers b1,b2,…,bnb_1, b_2, \dots, b_n (1≤bi≤1091 \le b_i \le 10^9) — the sequence bb itself.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

输入数据的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例由两行组成。

测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 序列 bb 的长度。

测试用例的第二行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \dots, b_n(1≤bi≤1091 \le b_i \le 10^9)—— 序列 bb 本身。

保证所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case print on a separate line:

  • YES if sequence bb could be sent over the network, that is, if sequence bb could be obtained from some sequence aa to send aa over the network.
  • NO otherwise.

You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response).

对于每个测试用例,在单独一行中输出:

  • 若序列 bb 可能被发送至网络(即:存在某个序列 aa,使得将 aa 发送至网络后可得到序列 bb),则输出 YES;
  • 否则输出 NO。

YES 和 NO 的大小写不限(例如,字符串 yEs、yes、Yes 和 YES 均被视为肯定回答)。

输入输出样例

  • 输入#1

    7
    9
    1 1 2 3 1 3 2 2 3
    5
    12 1 2 7 5
    6
    5 7 8 9 10 3
    4
    4 8 6 2
    2
    3 1
    10
    4 6 2 1 9 4 9 3 4 2
    1
    1

    输出#1

    YES
    YES
    YES
    NO
    YES
    YES
    NO

说明/提示

In the first case, the sequence bb could be obtained from the sequence a=[1,2,3,1,2,3]a = [1, 2, 3, 1, 2, 3] with the following partition: [1]+[2,3,1]+[2,3][\color{red}{1}] + [\color{blue}{2, 3, 1}] + [\color{green}{2, 3}]. The sequence bb: [1,1,2,3,1,3,2,2,3][\color{red}{1}, 1, \color{blue}{2, 3, 1}, 3, 2, \color{green}{2, 3}].

In the second case, the sequence bb could be obtained from the sequence a=[12,7,5]a = [12, 7, 5] with the following partition: [12]+[7,5][\color{red}{12}] + [\color{green}{7, 5}]. The sequence bb: [12,1,2,7,5][\color{red}{12}, 1, 2, \color{green}{7, 5}].

In the third case, the sequence bb could be obtained from the sequence a=[7,8,9,10,3]a = [7, 8, 9, 10, 3] with the following partition: [7,8,9,10,3][\color{red}{7, 8, 9, 10, 3}]. The sequence bb: [5,7,8,9,10,3][5, \color{red}{7, 8, 9, 10, 3}].

In the fourth case, there is no sequence aa such that changing aa for transmission over the network could produce a sequence bb.

在第一种情况下,序列 bb 可由序列 a=[1,2,3,1,2,3]a = [1, 2, 3, 1, 2, 3] 经如下划分得到:[1]+[2,3,1]+[2,3][\color{red}{1}] + [\color{blue}{2, 3, 1}] + [\color{green}{2, 3}]。所得序列 bb 为:[1,1,2,3,1,3,2,2,3][\color{red}{1}, 1, \color{blue}{2, 3, 1}, 3, 2, \color{green}{2, 3}]。

在第二种情况下,序列 bb 可由序列 a=[12,7,5]a = [12, 7, 5] 经如下划分得到:[12]+[7,5][\color{red}{12}] + [\color{green}{7, 5}]。所得序列 bb 为:[12,1,2,7,5][\color{red}{12}, 1, 2, \color{green}{7, 5}]。

在第三种情况下,序列 bb 可由序列 a=[7,8,9,10,3]a = [7, 8, 9, 10, 3] 经如下划分得到:[7,8,9,10,3][\color{red}{7, 8, 9, 10, 3}]。所得序列 bb 为:[5,7,8,9,10,3][5, \color{red}{7, 8, 9, 10, 3}]。

在第四种情况下,不存在序列 aa,使得对 aa 进行网络传输时的变换能生成序列 bb。

输入解题思路,AI测评打分。不知道怎么写?

首页