Prefix Purchase (Codeforces 1870D)
Tags: Greedy, sortings

You have an array $$$a$$$ of size $$$n$$$, initially filled with zeros ($$$a_1 = a_2 = \ldots = a_n = 0$$$). You also have an array of integers $$$c$$$ of size $$$n$$$.Initially, you have $$$k$$$ coins. By paying $$$c_i$$$ coins, you can add $$$1$$$ to all elements of the array $$$a$$$ from the first to the $$$i$$$-th element ($$$a_j \mathrel{+}= 1$$$ for all $$$1 \leq j \leq i$$$). You can buy any $$$c_i$$$ any number of times. A purchase is only possible if $$$k \geq c_i$$$, meaning that at any moment $$$k \geq 0$$$ must hold true.Find the lexicographically largest array $$$a$$$ that can be obtained.An array $$$a$$$ is lexicographically smaller than an array $$$b$$$ of the same length if and only if in the first position where $$$a$$$ and $$$b$$$ differ, the element in array $$$a$$$ is smaller than the corresponding element in $$$b$$$.

Input: The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. This is followed by a description of the test cases.The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the size of arrays $$$a$$$ and $$$c$$$.The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 10^9$$$) — the array $$$c$$$.The third line of each test case contains a single integer $$$k$$$ ($$$1 \leq k \leq 10^9$$$) — the number of coins you have.It is guaranteed that the sum of all $$$n$$$ values across all test cases does not exceed $$$2 \cdot 10^5$$$.

Output: For each test case, output $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the lexicographically largest array $$$a$$$ that can be obtained.

Note: In the first test case, $$$a_1$$$ cannot be greater than $$$5$$$, and if we buy $$$c_1$$$ five times, we will run out of money, so $$$a = [5, 0, 0]$$$.In the second test case, $$$a_1$$$ cannot be greater than $$$2$$$, but we can buy $$$c_1$$$ and $$$c_2$$$ once each (buying $$$c_2$$$ twice is not possible), so $$$a = [2, 1]$$$.

Sample Inputs:
4
3
1 2 3
5
2
3 4
7
3
3 2 1
2
6
10 6 4 6 3 4
7

Sample Outputs:
5 0 0 
2 1 
2 2 2 
2 2 2 2 2 1

Solution (Python):
def read_int():
    return int(input())

def read_list():
    return list(map(int, input().split()))

def main():
    t = int(input())
    while t > 0:
        solve()
        t -= 1

def solve():
    n = read_int()
    c = read_list()
    k = read_int()
    l = 0
    ans = []
    while k > 0 and l < n:
        pos = l
        f = k // c[l]
        for i in range(l, n):
            if k // c[i] >= f:
                pos = i
                f = k // c[i]
        ans.append((pos, f))
        for i in range(pos + 1, n):
            c[i] -= c[pos]
        k %= c[pos]
        l = pos + 1
    ret = [0 for _ in range(n)]
    i = 0
    for (pos, f) in ans:
        while i <= pos:
            ret[i] = f
            i += 1
    print(' '.join(map(str, ret)))
if __name__ == '__main__':
    main()

Solution (Python):
def solve():
    n = int(input())
    c = [int(x) for x in input().split()]
    k = int(input())
    s = []
    for (i, cc) in enumerate(c):
        while s and cc <= s[-1][0]:
            s.pop()
        s.append((cc, i))
    used = [0] * len(s)
    used[0] = k // s[0][0]
    tot = used[0]
    leftover = k - used[0] * s[0][0]
    for i in range(1, len(s)):
        cost_diff = s[i][0] - s[i - 1][0]
        added = min(used[i - 1], leftover // cost_diff)
        leftover -= cost_diff * added
        used[i - 1] -= added
        used[i] += added
    diff_a = [0] * n
    diff_a[0] = tot
    for ((c, i), u) in zip(s, used):
        if i + 1 < n:
            diff_a[i + 1] = -u
    acc = 0
    for i in range(n):
        acc += diff_a[i]
        diff_a[i] = acc
    print(' '.join(map(str, diff_a)))
t = int(input())
for _ in range(t):
    solve()

Solution (Python):
import sys
t = int(input())
ANS = []
for _ in range(t):
    n = int(input())
    c = list(map(int, input().split()))
    k = int(input())
    for i in range(n - 2, -1, -1):
        c[i] = min(c[i], c[i + 1])
    ans = [k // c[0]] + [0] * (n - 1)
    k %= c[0]
    for i in range(1, n):
        cost = c[i] - c[i - 1]
        if cost == 0:
            ans[i] = ans[i - 1]
        else:
            x = k // cost
            k -= cost * min(x, ans[i - 1])
            ans[i] = min(x, ans[i - 1])
    ANS.append(' '.join([str(x) for x in ans]))
for a in ANS:
    print(a)