codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1839C.md (4623B)


      1 Source: https://codeforces.com/problemset/problem/1839/C
      2 
      3 C. Insert Zero and Invert Prefix
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 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.
     22 
     23 You are going to perform $n$ operations. On each of them you will increase the length of $b$ by $1$.
     24 
     25 * 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$.
     26 * 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$.
     27 
     28 You can find examples of operations in the Notes section.
     29 
     30 Determine if there exists a sequence of operations that makes $b$ equal to $a$. If such sequence of operations exists, find it.
     31 
     32 Input
     33 
     34 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.
     35 
     36 The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the sequence $a$.
     37 
     38 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$.
     39 
     40 It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
     41 
     42 Output
     43 
     44 For each test case:
     45 
     46 * output "NO", if it is impossible to make $b$ equal to $a$ using the given operations;
     47 * 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.
     48 
     49 Example
     50 
     51 Input
     52 
     53 ```
     54 4
     55 
     56 5
     57 
     58 1 1 0 0 0
     59 
     60 1
     61 
     62 1
     63 
     64 3
     65 
     66 0 1 1
     67 
     68 6
     69 
     70 1 0 0 1 1 0
     71 ```
     72 
     73 Output
     74 
     75 ```
     76 YES
     77 0 0 2 1 3
     78 NO
     79 NO
     80 YES
     81 0 1 0 2 4 2
     82 ```
     83 
     84 Note
     85 
     86 In the first test case,
     87 
     88 1. Before the first operation, $b = [\,]$. You choose $p = 0$ and replace $b$ with $[\, \underline{0} \,]$
     89 2. On the second operation you choose $p = 0$ and replace $b$ with $[\, \underline{0}, 0 \,]$.
     90 3. On the third operation you choose $p = 2$ and replace $b$ with $[\, 1, 1, \underline{0} \,]$.
     91 4. On the fourth operation you choose $p = 1$ and replace $b$ with $[\, 0, \underline{0}, 1, 0 \,]$.
     92 5. On the fifth operation you choose $p = 3$ and replace $b$ with $[\, 1, 1, 0, \underline{0}, 0 \,]$.
     93 
     94 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.
     95 
     96 In the second test case, $n = 1$ and the only achiveable sequence $b$ is $[\, 0 \, ]$.
     97 
     98 In the third test case, there are six possible sequences of operations:
     99 
    100 1. $[\,]$ $\xrightarrow{p \, = \, 0}$ $[\, \underline{0} \,]$ $\xrightarrow{p \, = \, 0}$ $[\, \underline{0}, 0 \,]$ $\xrightarrow{p \, = \, 0}$ $[\, \underline{0}, 0, 0 \,]$.
    101 2. $[\,]$ $\xrightarrow{p \, = \, 0}$ $[\, \underline{0} \,]$ $\xrightarrow{p \, = \, 0}$ $[\, \underline{0}, 0 \,]$ $\xrightarrow{p \, = \, 1}$ $[\, 1, \underline{0}, 0 \,]$.
    102 3. $[\,]$ $\xrightarrow{p \, = \, 0}$ $[\, \underline{0} \,]$ $\xrightarrow{p \, = \, 0}$ $[\, \underline{0}, 0 \,]$ $\xrightarrow{p \, = \, 2}$ $[\, 1, 1, \underline{0} \,]$.
    103 4. $[\,]$ $\xrightarrow{p \, = \, 0}$ $[\, \underline{0} \,]$ $\xrightarrow{p \, = \, 1}$ $[\, 1, \underline{0} \,]$ $\xrightarrow{p \, = \, 0}$ $[\, \underline{0}, 1, 0 \,]$.
    104 5. $[\,]$ $\xrightarrow{p \, = \, 0}$ $[\, \underline{0} \,]$ $\xrightarrow{p \, = \, 1}$ $[\, 1, \underline{0} \,]$ $\xrightarrow{p \, = \, 1}$ $[\, 0, \underline{0}, 0 \,]$.
    105 6. $[\,]$ $\xrightarrow{p \, = \, 0}$ $[\, \underline{0} \,]$ $\xrightarrow{p \, = \, 1}$ $[\, 1, \underline{0} \,]$ $\xrightarrow{p \, = \, 2}$ $[\, 0, 1, \underline{0} \,]$.
    106 
    107 None of them makes $b$ equal to $[\, 0, 1, 1 \,]$, so the answer is "NO".