cswork / OA打开练习工作台

PinterestOA #4

Assign Pins to the Shortest Column

估计简单可在线判题

考点(AI 标注)单调栈与堆模拟/实现

There are k columns, each with initial height 0. Process pins[i] in the original order: place each pin on the column whose current total height is lowest; on a tie in height, choose the column with the smallest index. Return the column index for each pin and the final heights of all columns.

输入格式

The first line contains n k; the second line contains n positive integers pins[i]. Source constraints: 1≤k≤n≤200000, 1≤pins[i]≤10⁹.

输出格式

The first line contains the n assigned indices, and the second line contains the final heights of the k columns. Column indices start from 0; total heights use 64-bit integers.

样例 1

输入:

5 2
1 2 3 4 5

输出:

0 1 0 1 0
9 6

样例 2

输入:

3 3
10 20 30

输出:

0 1 2
10 20 30

样例 3

输入:

6 2
5 5 5 5 5 5

输出:

0 1 0 1 0 1
15 15