CF1839C.Insert Zero and Invert Prefix
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You have a sequence a1,a2,…,an 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≤i≤n ) operation as b1,b2,…,bi−1 . On the i -th operation you choose an integer p between 0 and i−1 and replace b with b1,b2,…,bp,0,bp+1,bp+2,…,bi−1 . Here, x denotes the binary inversion. Hence, 0=1 and 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≤t≤104 ) — the number of test cases.
The first line of each test case contains one integer n ( 1≤n≤105 ) — the length of the sequence a .
The second line of each test case contains n integers a1,a2,…,an ( 0≤ai≤1 ) — the sequence a .
It is guaranteed that the sum of n over all test cases does not exceed 105 .
输出格式
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 p1,p2,…,pn ( 0≤pi≤i−1 ) in the second line — the description of sequence of operations that makes b equal to a . Here, pi 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
说明/提示
In the first test case,
- Before the first operation, b=[] . You choose p=0 and replace b with [0]
- On the second operation you choose p=0 and replace b with [0,0] .
- On the third operation you choose p=2 and replace b with [1,1,0] .
- On the fourth operation you choose p=1 and replace b with [0,0,1,0] .
- On the fifth operation you choose p=3 and replace b with [1,1,0,0,0] .
Hence, sequence b changes in the following way: [] p=0 [0] p=0 [0,0] p=2 [1,1,0] p=1 [0,0,1,0] p=3 [1,1,0,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:
- [] p=0 [0] p=0 [0,0] p=0 [0,0,0] .
- [] p=0 [0] p=0 [0,0] p=1 [1,0,0] .
- [] p=0 [0] p=0 [0,0] p=2 [1,1,0] .
- [] p=0 [0] p=1 [1,0] p=0 [0,1,0] .
- [] p=0 [0] p=1 [1,0] p=1 [0,0,0] .
- [] p=0 [0] p=1 [1,0] p=2 [0,1,0] .
None of them makes b equal to [0,1,1] , so the answer is "NO".