P1905 Stacking Goods

Background

After seeing the city you built, Caesar was very satisfied, promoted you to the local governor of Genoa, and raised your salary by $50$ denarii. Recently, he wrote to you again: Genoa cannot rely solely on Rome’s financial support; you must develop urban trade to increase fiscal revenue. After spending $500$ denarii to open a trade route to Capua, ships from Capua keep arriving. Genoa urgently needs to build a warehouse to temporarily store goods to be shipped to Capua.

Description

There are three types of goods in Genoa: pottery, furniture, and marble, with a total of $n$ items. Their weights are $1$, $2$, and $3$ units, respectively. Now, there are $p$ ships from Capua waiting for goods from the warehouse. You should divide the goods in the warehouse into $p$ stacks according to the following rules: - Within the same stack, weights from bottom to top must be non-increasing (obviously, you cannot place marble on top of pottery or furniture). - The absolute difference between the total weights of any two stacks must not exceed $3$ units; otherwise, conflicts may arise between ships due to overly uneven distribution. Please complete this task and provide one feasible arrangement.

Input Format

The first line contains an integer, the number of goods $n$. The second line contains an integer, the number of stacks $p$. The third line contains $n$ integers; the $i$-th integer is the weight $a_i$ of the $i$-th item.

Output Format

**This problem uses a Special Judge.** Output $p$ lines. Each line contains some integers describing one stack of goods, listing the weight of each item in that stack from bottom to top.

Explanation/Hint

#### Constraints and Notes For all test points, it is guaranteed that $2 \leq n \leq 10^3$, $1 \leq p \leq \lfloor \frac{n}{2} \rfloor$, and $1 \leq a_i \leq 3$. #### Notes Thanks to @cn: 苏卿念 for providing the spj. SPJ description: ``` score:ouo:你的输出里面有不是'1','2','3',' ','\n','\r'的东西,具体以ASCII的第X号字符 score:pvp:你在第x组的时候装下了超过规定数量的'3'|'2'|'1'。 score:TAT:蛇皮judge没能读入到想要读入的东东(类似于你直接提交了一个puts(" / 特别特别多的空格/")) score:qaq:你把重量为w1的货物放到了w2上面(w1>w2)在第i组的第j个货物 score:ovo:你没有装足够的x score:qwq:你第i组和第j组的差的绝对值>3了,(要打架了),第i组的重量和为x,第j组的重量和为y。 score:pwp:答案正确,恭喜你嘤嘤嘤 ``` Translated by ChatGPT 5