CF1839C.Insert Zero and Invert Prefix
普及-
通过率:0%
时间限制:2.00s
内存限制:256MB
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.
你有一个长度为 n 的序列 a1,a2,…,an,其中每个元素均为 0 或 1;另有一个初始为空的序列 b。
你将执行 n 次操作。每次操作都会使 b 的长度增加 1。
- 在第 i 次操作中,你选择一个介于 0 与 i−1 之间的整数 p。你在 b 的第 p+1 个位置(即前 p 个元素之后)插入一个 0,然后将 b 的前 p 个元素取反。
- 更形式化地:设第 i 次操作(1≤i≤n)之前序列 b 为 b1,b2,…,bi−1。在第 i 次操作中,你选择一个介于 0 与 i−1 之间的整数 p,并将 b 替换为 b1,b2,…,bp,0,bp+1,bp+2,…,bi−1。此处 x 表示二进制取反,即 0=1,1=0。
操作示例可参见“注释”部分。
请判断是否存在一系列操作,使得最终 b=a。若存在这样的操作序列,请找出它。
输入格式
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.
每个测试包含多个测试用例。第一行包含一个整数 t(1≤t≤104),表示测试用例的数量。
每个测试用例的第一行包含一个整数 n(1≤n≤105),表示序列 a 的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(0≤ai≤1),表示序列 a。
保证所有测试用例的 n 之和不超过 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.
对于每个测试用例:
- 如果无法通过给定的操作使 b 等于 a,则输出
"NO"; - 否则,第一行输出
"YES",第二行输出 n 个整数 p1,p2,…,pn(满足 0≤pi≤i−1)—— 这些整数描述了一组可使 b 变为 a 的操作序列。其中,pi 表示第 i 次操作中所选择的整数。若存在多种解法,输出任意一种即可。
输入输出样例
输入#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".
在第一个测试用例中,
- 第一次操作前,b=[]。你选择 p=0,并将 b 替换为 [0];
- 第二次操作中,你选择 p=0,并将 b 替换为 [0,0];
- 第三次操作中,你选择 p=2,并将 b 替换为 [1,1,0];
- 第四次操作中,你选择 p=1,并将 b 替换为 [0,0,1,0];
- 第五次操作中,你选择 p=3,并将 b 替换为 [1,1,0,0,0]。
因此,序列 b 的变化过程如下:
[] 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]。
最终序列 b 等于序列 a,因此该操作序列是正确答案之一。
在第二个测试用例中,n=1,唯一可达成的序列 b 是 [0]。
在第三个测试用例中,共有六种可能的操作序列:
- [] 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]。
以上任意一种操作序列均无法使 b 变为 [0,1,1],因此答案为 “NO”。
输入解题思路,AI测评打分。不知道怎么写?