Shifting Stacks (Codeforces 1486A)
Tags: Greedy, implementation

You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.

Input: First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases.The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks.It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.

Output: For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise.You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).

Note: In the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.

Sample Inputs:
6
2
1 2
2
1 0
3
4 4 4
2
0 0
3
0 1 0
4
1000000000 1000000000 1000000000 1000000000

Sample Outputs:
YES
YES
YES
NO
NO
YES

Solution (Python):
def input_to_intlist():
    ans = list(map(int, input().split()))
    return ans

def solve(n, test):
    puffer = 0
    for i in range(n):
        puffer = puffer + (test[i] - i)
        if puffer < 0:
            return 'no'
    return 'yes'
ans = []
t = int(input())
for _ in range(t):
    n = int(input())
    test = input_to_intlist()
    ans.append(solve(n, test))
for element in ans:
    print(element)

Solution (Python):
for s in [*open(0)][2::2]:
    i = t = f = 0
    for x in s.split():
        t += int(x) - i
        f |= t < 0
        i += 1
    print('YNEOS'[f::2])

Solution (Python):
t = int(input())
mas = []
for i in range(t):
    n = int(input())
    h = list(map(int, input().split()))
    g = 0
    for j in range(n):
        if h[j] > j:
            g += h[j] - j
            h[j] = j
        elif h[j] < j:
            if h[j] + g >= j:
                g = g - (j - h[j])
                h[j] = j
            else:
                mas.append('NO')
                break
    else:
        mas.append('YES')
for i in mas:
    print(i)