← 返回 pinterest 的题目列表Assign Pins to the Shortest Column (Min-Heap Scheduling)
类型:online_judge
Problem: Assign Pins to the Shortest Column (Min-Heap)
You are given a list of pins, each with a height h. There are k columns, each starting with total height 0.
Process pins in the given order:
Append the current pin to the column with the smallest current total height.
If multiple columns tie for the smallest height, choose the one with the smallest column index.
After appending, that column’s total height increases by h.
Input
Line 1: two integers n k — number of pins and number of columns.
Line 2: n integers h1 h2 ... hn — pin heights in processing order.
Output
Print two lines:
n integers — the assigned column index (0-based) for each pin.
k integers — the final total height of each column.
Constraints (for complexity analysis)
1 <= k <= n
n can be large; provide time and space complexity
Example
Input:
5 2
1 2 3 4 5
Output:
0 1 0 1 0
9 6
Example
Input
5 2
1 2 3 4 5
Output
0 1 0 1 0
9 6