P7912 [CSP-J 2021] Little Bear’s Fruit Baskets

Description

In Little Bear’s fruit shop, there is a row of $n$ fruits. Each fruit is either an apple or an orange, numbered from left to right with positive integers $1, 2, \ldots, n$. A consecutive segment of the same kind of fruit is called a “block”. Little Bear wants to pick the fruits in this row into several baskets. The method is as follows: each time, the leftmost fruit in every “block” is picked out simultaneously to form one basket. Repeat this operation until all fruits are picked. Note that after picking one basket, the “blocks” may change. For example, if the only orange between two apple “blocks” is picked out, then the two apple “blocks” will merge into one “block”. Please help Little Bear determine which fruits are contained in each basket.

Input Format

The first line contains a positive integer $n$, the number of fruits. The second line contains $n$ integers separated by spaces. The $i$-th number indicates the type of the fruit numbered $i$: $1$ represents an apple, and $0$ represents an orange.

Output Format

Output several lines. The $i$-th line describes the basket formed by the $i$-th picking operation. Output the indices of all fruits in that basket in increasing order, separated by one space between every two indices.

Explanation/Hint

**Sample Explanation #1** This is the sample description for the first set of testdata. Initially, the fruits are $[1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0]$, with a total of $6$ blocks. During the first picking operation, fruits numbered $1, 3, 5, 8, 9, 11$ are picked out. After that, the remaining fruits are $[1, 0, 1, 1, 1, 0]$, with a total of $4$ blocks. During the second picking operation, fruits numbered $2, 4, 6, 12$ are picked out. After that, the remaining fruits are $[1, 1]$, with only $1$ block. During the third picking operation, the fruit numbered $7$ is picked out. Finally, the remaining fruits are $[1]$, with only $1$ block. During the fourth picking operation, the fruit numbered $10$ is picked out. **Constraints** For $10\%$ of the data, $n \le 5$. For $30\%$ of the data, $n \le 1000$. For $70\%$ of the data, $n \le 50000$. For $100\%$ of the data, $1 \le n \le 2 \times {10}^5$. **Hint** Because the data size is large, C/C++ users are advised to use `scanf` and `printf` for input and output. Translated by ChatGPT 5