CF1839C.Insert Zero and Invert Prefix

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You have a sequence a1,a2,…,ana_1, a_2, \ldots, a_n of length nn, each element of which is either 00 or 11, and a sequence bb, which is initially empty.

You are going to perform nn operations. On each of them you will increase the length of bb by 11.

  • On the ii-th operation you choose an integer pp between 00 and i−1i-1. You insert 00 in the sequence bb on position p+1p+1 (after the first pp elements), and then you invert the first pp elements of bb.
  • More formally: let's denote the sequence bb before the ii-th (1≤i≤n1 \le i \le n) operation as b1,b2,…,bi−1b_1, b_2, \ldots, b_{i-1}. On the ii-th operation you choose an integer pp between 00 and i−1i-1 and replace bb with b1‾,b2‾,…,bp‾,0,bp+1,bp+2,…,bi−1\overline{b_1}, \overline{b_2}, \ldots, \overline{b_{p}}, 0, b_{p+1}, b_{p+2}, \ldots, b_{i-1}. Here, x‾\overline{x} denotes the binary inversion. Hence, 0‾=1\overline{0} = 1 and 1‾=0\overline{1} = 0.

You can find examples of operations in the Notes section.

Determine if there exists a sequence of operations that makes bb equal to aa. If such sequence of operations exists, find it.

你有一个长度为 nn 的序列 a1,a2,…,ana_1, a_2, \ldots, a_n,其中每个元素均为 00 或 11;另有一个初始为空的序列 bb。

你将执行 nn 次操作。每次操作都会使 bb 的长度增加 11。

  • 在第 ii 次操作中,你选择一个介于 00 与 i−1i-1 之间的整数 pp。你在 bb 的第 p+1p+1 个位置(即前 pp 个元素之后)插入一个 00,然后将 bb 的前 pp 个元素取反。
  • 更形式化地:设第 ii 次操作(1≤i≤n1 \le i \le n)之前序列 bb 为 b1,b2,…,bi−1b_1, b_2, \ldots, b_{i-1}。在第 ii 次操作中,你选择一个介于 00 与 i−1i-1 之间的整数 pp,并将 bb 替换为 b1‾,b2‾,…,bp‾,0,bp+1,bp+2,…,bi−1\overline{b_1}, \overline{b_2}, \ldots, \overline{b_{p}}, 0, b_{p+1}, b_{p+2}, \ldots, b_{i-1}。此处 x‾\overline{x} 表示二进制取反,即 0‾=1\overline{0} = 1,1‾=0\overline{1} = 0。

操作示例可参见“注释”部分。

请判断是否存在一系列操作,使得最终 b=ab = a。若存在这样的操作序列,请找出它。

输入格式

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

The first line of each test case contains one integer nn (1≤n≤1051 \le n \le 10^5) — the length of the sequence aa.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai≤10 \le a_i \le 1) — the sequence aa.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5),表示序列 aa 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai≤10 \le a_i \le 1),表示序列 aa。

保证所有测试用例的 nn 之和不超过 10510^5。

输出格式

For each test case:

  • output "NO", if it is impossible to make bb equal to aa using the given operations;
  • otherwise, output "YES" in the first line and nn integers p1,p2,…,pnp_1, p_2, \ldots, p_n (0≤pi≤i−10 \le p_i \le i-1) in the second line — the description of sequence of operations that makes bb equal to aa. Here, pip_i should be the integer you choose on the ii-th operation. If there are multiple solutions, you can output any of them.

对于每个测试用例:

  • 如果无法通过给定的操作使 bb 等于 aa,则输出 "NO";
  • 否则,第一行输出 "YES",第二行输出 nn 个整数 p1,p2,…,pnp_1, p_2, \ldots, p_n(满足 0≤pi≤i−10 \le p_i \le i-1)—— 这些整数描述了一组可使 bb 变为 aa 的操作序列。其中,pip_i 表示第 ii 次操作中所选择的整数。若存在多种解法,输出任意一种即可。

输入输出样例

  • 输入#1

    4
    5
    1 1 0 0 0
    1
    1
    3
    0 1 1
    6
    1 0 0 1 1 0

    输出#1

    YES
    0 0 2 1 3
    NO
    NO
    YES
    0 1 0 2 4 2

说明/提示

In the first test case,

  1. Before the first operation, b=[ ]b = [\,]. You choose p=0p = 0 and replace bb with [ 0‾ ][\, \underline{0} \,]
  2. On the second operation you choose p=0p = 0 and replace bb with [ 0‾,0 ][\, \underline{0}, 0 \,].
  3. On the third operation you choose p=2p = 2 and replace bb with [ 1,1,0‾ ][\, 1, 1, \underline{0} \,].
  4. On the fourth operation you choose p=1p = 1 and replace bb with [ 0,0‾,1,0 ][\, 0, \underline{0}, 1, 0 \,].
  5. On the fifth operation you choose p=3p = 3 and replace bb with [ 1,1,0,0‾,0 ][\, 1, 1, 0, \underline{0}, 0 \,].

Hence, sequence bb changes in the following way: [ ][\,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾ ][\, \underline{0} \,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾,0 ][\, \underline{0}, 0 \,] →p = 2\xrightarrow{p \, = \, 2} [ 1,1,0‾ ][\, 1, 1, \underline{0} \,] →p = 1\xrightarrow{p \, = \, 1} [ 0,0‾,1,0 ][\, 0, \underline{0}, 1, 0 \,] →p = 3\xrightarrow{p \, = \, 3} [ 1,1,0,0‾,0 ][\, 1, 1, 0, \underline{0}, 0 \,]. In the end the sequence bb is equal to the sequence aa, so this way to perform operations is one of the correct answers.

In the second test case, n=1n = 1 and the only achiveable sequence bb is [ 0 ][\, 0 \, ].

In the third test case, there are six possible sequences of operations:

  1. [ ][\,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾ ][\, \underline{0} \,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾,0 ][\, \underline{0}, 0 \,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾,0,0 ][\, \underline{0}, 0, 0 \,].
  2. [ ][\,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾ ][\, \underline{0} \,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾,0 ][\, \underline{0}, 0 \,] →p = 1\xrightarrow{p \, = \, 1} [ 1,0‾,0 ][\, 1, \underline{0}, 0 \,].
  3. [ ][\,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾ ][\, \underline{0} \,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾,0 ][\, \underline{0}, 0 \,] →p = 2\xrightarrow{p \, = \, 2} [ 1,1,0‾ ][\, 1, 1, \underline{0} \,].
  4. [ ][\,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾ ][\, \underline{0} \,] →p = 1\xrightarrow{p \, = \, 1} [ 1,0‾ ][\, 1, \underline{0} \,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾,1,0 ][\, \underline{0}, 1, 0 \,].
  5. [ ][\,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾ ][\, \underline{0} \,] →p = 1\xrightarrow{p \, = \, 1} [ 1,0‾ ][\, 1, \underline{0} \,] →p = 1\xrightarrow{p \, = \, 1} [ 0,0‾,0 ][\, 0, \underline{0}, 0 \,].
  6. [ ][\,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾ ][\, \underline{0} \,] →p = 1\xrightarrow{p \, = \, 1} [ 1,0‾ ][\, 1, \underline{0} \,] →p = 2\xrightarrow{p \, = \, 2} [ 0,1,0‾ ][\, 0, 1, \underline{0} \,].

None of them makes bb equal to [ 0,1,1 ][\, 0, 1, 1 \,], so the answer is "NO".

在第一个测试用例中,

  1. 第一次操作前,b=[ ]b = [\,]。你选择 p=0p = 0,并将 bb 替换为 [ 0‾ ][\, \underline{0} \,];
  2. 第二次操作中,你选择 p=0p = 0,并将 bb 替换为 [ 0‾,0 ][\, \underline{0}, 0 \,];
  3. 第三次操作中,你选择 p=2p = 2,并将 bb 替换为 [ 1,1,0‾ ][\, 1, 1, \underline{0} \,];
  4. 第四次操作中,你选择 p=1p = 1,并将 bb 替换为 [ 0,0‾,1,0 ][\, 0, \underline{0}, 1, 0 \,];
  5. 第五次操作中,你选择 p=3p = 3,并将 bb 替换为 [ 1,1,0,0‾,0 ][\, 1, 1, 0, \underline{0}, 0 \,]。

因此,序列 bb 的变化过程如下:
[ ][\,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾ ][\, \underline{0} \,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾,0 ][\, \underline{0}, 0 \,] →p = 2\xrightarrow{p \, = \, 2} [ 1,1,0‾ ][\, 1, 1, \underline{0} \,] →p = 1\xrightarrow{p \, = \, 1} [ 0,0‾,1,0 ][\, 0, \underline{0}, 1, 0 \,] →p = 3\xrightarrow{p \, = \, 3} [ 1,1,0,0‾,0 ][\, 1, 1, 0, \underline{0}, 0 \,]。
最终序列 bb 等于序列 aa,因此该操作序列是正确答案之一。

在第二个测试用例中,n=1n = 1,唯一可达成的序列 bb 是 [ 0 ][\, 0 \, ]。

在第三个测试用例中,共有六种可能的操作序列:

  1. [ ][\,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾ ][\, \underline{0} \,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾,0 ][\, \underline{0}, 0 \,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾,0,0 ][\, \underline{0}, 0, 0 \,];
  2. [ ][\,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾ ][\, \underline{0} \,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾,0 ][\, \underline{0}, 0 \,] →p = 1\xrightarrow{p \, = \, 1} [ 1,0‾,0 ][\, 1, \underline{0}, 0 \,];
  3. [ ][\,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾ ][\, \underline{0} \,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾,0 ][\, \underline{0}, 0 \,] →p = 2\xrightarrow{p \, = \, 2} [ 1,1,0‾ ][\, 1, 1, \underline{0} \,];
  4. [ ][\,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾ ][\, \underline{0} \,] →p = 1\xrightarrow{p \, = \, 1} [ 1,0‾ ][\, 1, \underline{0} \,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾,1,0 ][\, \underline{0}, 1, 0 \,];
  5. [ ][\,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾ ][\, \underline{0} \,] →p = 1\xrightarrow{p \, = \, 1} [ 1,0‾ ][\, 1, \underline{0} \,] →p = 1\xrightarrow{p \, = \, 1} [ 0,0‾,0 ][\, 0, \underline{0}, 0 \,];
  6. [ ][\,] →p = 0\xrightarrow{p \, = \, 0} [ 0‾ ][\, \underline{0} \,] →p = 1\xrightarrow{p \, = \, 1} [ 1,0‾ ][\, 1, \underline{0} \,] →p = 2\xrightarrow{p \, = \, 2} [ 0,1,0‾ ][\, 0, 1, \underline{0} \,]。

以上任意一种操作序列均无法使 bb 变为 [ 0,1,1 ][\, 0, 1, 1 \,],因此答案为 “NO”。

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

首页