题库练习 Insert Zero and Invert Prefix
← 上一题 下一题 →

A16040 | Insert Zero and Invert Prefix

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

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.

输入格式

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$ .

输出格式

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.

输入输出样例

输入 #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
C++ 编辑器
输入
输出