310473: CF1839C. Insert Zero and Invert Prefix

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

C. Insert Zero and Invert Prefixtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

You have a sequence $a_1, a_2, \ldots, a_n$ of length $n$, each element of which is either $0$ or $1$, and a sequence $b$, which is initially empty.

You are going to perform $n$ operations. On each of them you will increase the length of $b$ by $1$.

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

You can find examples of operations in the Notes section.

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

Input

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

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

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — the sequence $a$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output

For each test case:

  • output "NO", if it is impossible to make $b$ equal to $a$ using the given operations;
  • otherwise, output "YES" in the first line and $n$ integers $p_1, p_2, \ldots, p_n$ ($0 \le p_i \le i-1$) in the second line — the description of sequence of operations that makes $b$ equal to $a$. Here, $p_i$ should be the integer you choose on the $i$-th operation. If there are multiple solutions, you can output any of them.
ExampleInput
4
5
1 1 0 0 0
1
1
3
0 1 1
6
1 0 0 1 1 0
Output
YES
0 0 2 1 3
NO
NO
YES
0 1 0 2 4 2
Note

In the first test case,

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

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

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

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

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

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

Input

题意翻译

你有一个长度为 $n$ 的 01 序列 $a$,以及一个初始为空的序列 $b$。你接下来需要执行 $n$ 次操作,每次操作将会使序列 $b$ 的长度增加 $1$。 - 在第 $i$ 次操作,你需要选择一个在 $[0,i)$ 之间的整数 $p$。然后在第 $p$ 位之后插入一个数字 $0$,并翻转之前的所有数字。 - 即,设在第 $i$ 次操作前的 $b$ 为 $b_1,b_2,...,b_{i-1}$。在第 $i$ 次操作选择在 $[0,i)$ 之间的整数 $p$,并使 $b$ 变为 $\overline{b_1}, \overline{b_2}, \ldots, \overline{b_{p}}, 0, b_{p+1}, b_{p+2}, \ldots, b_{i-1}$。其中,$\overline{x}$ 表示二进制翻转操作,即 $\overline{0}=1,\overline{1}=0$。 对给定的数组 $a$,判断是否存在一组操作使得 $b$ 变为 $a$,若存在则输出方案。 多组数据。

Output

题目大意:给定一个长度为n的01序列a,以及一个空序列b。你将执行n个操作,每次操作使序列b的长度增加1。

在第i次操作中,你需要在0到i-1之间选择一个整数p。在序列b的第p+1位(即前p个元素之后)插入0,然后翻转序列b的前p个元素。

更形式化地说,设序列b在第i次操作之前为b1, b2, ..., bi-1。在第i次操作中,你选择一个0到i-1之间的整数p,并用\(\overline{b_1}, \overline{b_2}, \ldots, \overline{b_{p}}, 0, b_{p+1}, b_{p+2}, \ldots, b_{i-1}\)替换b。这里,\(\overline{x}\)表示二进制翻转。即\(\overline{0} = 1\)和\(\overline{1} = 0\)。

你需要确定是否存在一系列操作使得b等于a。如果存在这样的操作序列,请找到它。

输入数据格式:第一行包含一个整数t(1≤t≤10^4)——测试用例的数量。每个测试用例的第一行包含一个整数n(1≤n≤10^5)——序列a的长度。每个测试用例的第二行包含n个整数\(a_1, a_2, \ldots, a_n\)(0≤\(a_i\)≤1)——序列a。保证所有测试用例的n之和不超过10^5。

输出数据格式:对于每个测试用例:
- 如果无法使用给定的操作使b等于a,输出"NO";
- 否则,第一行输出"YES",第二行输出n个整数\(p_1, p_2, \ldots, p_n\)(0≤\(p_i\)≤i-1)——使b等于a的操作序列的描述。如果有多个解决方案,可以输出其中任何一个。题目大意:给定一个长度为n的01序列a,以及一个空序列b。你将执行n个操作,每次操作使序列b的长度增加1。 在第i次操作中,你需要在0到i-1之间选择一个整数p。在序列b的第p+1位(即前p个元素之后)插入0,然后翻转序列b的前p个元素。 更形式化地说,设序列b在第i次操作之前为b1, b2, ..., bi-1。在第i次操作中,你选择一个0到i-1之间的整数p,并用\(\overline{b_1}, \overline{b_2}, \ldots, \overline{b_{p}}, 0, b_{p+1}, b_{p+2}, \ldots, b_{i-1}\)替换b。这里,\(\overline{x}\)表示二进制翻转。即\(\overline{0} = 1\)和\(\overline{1} = 0\)。 你需要确定是否存在一系列操作使得b等于a。如果存在这样的操作序列,请找到它。 输入数据格式:第一行包含一个整数t(1≤t≤10^4)——测试用例的数量。每个测试用例的第一行包含一个整数n(1≤n≤10^5)——序列a的长度。每个测试用例的第二行包含n个整数\(a_1, a_2, \ldots, a_n\)(0≤\(a_i\)≤1)——序列a。保证所有测试用例的n之和不超过10^5。 输出数据格式:对于每个测试用例: - 如果无法使用给定的操作使b等于a,输出"NO"; - 否则,第一行输出"YES",第二行输出n个整数\(p_1, p_2, \ldots, p_n\)(0≤\(p_i\)≤i-1)——使b等于a的操作序列的描述。如果有多个解决方案,可以输出其中任何一个。

加入题单

上一题 下一题 算法标签: