Sending a Sequence Over the Network (Codeforces 1741E)
Tags: DP, dp

The sequence $$$a$$$ is sent over the network as follows:  sequence $$$a$$$ is split into segments (each element of the sequence belongs to exactly one segment, each segment is a group of consecutive elements of sequence);  for each segment, its length is written next to it, either to the left of it or to the right of it;  the resulting sequence $$$b$$$ is sent over the network. For example, we needed to send the sequence $$$a = [1, 2, 3, 1, 2, 3]$$$. Suppose it was split into segments as follows: $$$[\color{red}{1}] + [\color{blue}{2, 3, 1}] + [\color{green}{2, 3}]$$$. Then we could have the following sequences:   $$$b = [1, \color{red}{1}, 3, \color{blue}{2, 3, 1}, \color{green}{2, 3}, 2]$$$,  $$$b = [\color{red}{1}, 1, 3, \color{blue}{2, 3, 1}, 2, \color{green}{2, 3}]$$$,  $$$b = [\color{red}{1}, 1, \color{blue}{2, 3, 1}, 3, 2, \color{green}{2, 3}]$$$,  $$$b = [\color{red}{1}, 1,\color{blue}{2, 3, 1}, 3, \color{green}{2, 3}, 2]$$$. If a different segmentation had been used, the sent sequence might have been different.The sequence $$$b$$$ is given. Could the sequence $$$b$$$ be sent over the network? In other words, is there such a sequence $$$a$$$ that converting $$$a$$$ to send it over the network could result in a sequence $$$b$$$?

Input: The first line of input data contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.Each test case consists of two lines.The first line of the test case contains an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the sequence $$$b$$$.The second line of test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 10^9$$$) — the sequence $$$b$$$ itself.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output: For each test case print on a separate line:  YES if sequence $$$b$$$ could be sent over the network, that is, if sequence $$$b$$$ could be obtained from some sequence $$$a$$$ to send $$$a$$$ over the network.  NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response).

Note: In the first case, the sequence $$$b$$$ could be obtained from the sequence $$$a = [1, 2, 3, 1, 2, 3]$$$ with the following partition: $$$[\color{red}{1}] + [\color{blue}{2, 3, 1}] + [\color{green}{2, 3}]$$$. The sequence $$$b$$$: $$$[\color{red}{1}, 1, \color{blue}{2, 3, 1}, 3, 2, \color{green}{2, 3}]$$$.In the second case, the sequence $$$b$$$ could be obtained from the sequence $$$a = [12, 7, 5]$$$ with the following partition: $$$[\color{red}{12}] + [\color{green}{7, 5}]$$$. The sequence $$$b$$$: $$$[\color{red}{12}, 1, 2, \color{green}{7, 5}]$$$.In the third case, the sequence $$$b$$$ could be obtained from the sequence $$$a = [7, 8, 9, 10, 3]$$$ with the following partition: $$$[\color{red}{7, 8, 9, 10, 3}]$$$. The sequence $$$b$$$: $$$[5, \color{red}{7, 8, 9, 10, 3}]$$$.In the fourth case, there is no sequence $$$a$$$ such that changing $$$a$$$ for transmission over the network could produce a sequence $$$b$$$.

Sample Inputs:
7
9
1 1 2 3 1 3 2 2 3
5
12 1 2 7 5
6
5 7 8 9 10 3
4
4 8 6 2
2
3 1
10
4 6 2 1 9 4 9 3 4 2
1
1

Sample Outputs:
YES
YES
YES
NO
YES
YES
NO

Solution (Python):
def dp_send(n, lst):
    dp = [0] * (n + 1)
    dp[0] = 1
    for i in range(1, n + 1):
        if i + lst[i] <= n:
            if dp[i - 1]:
                dp[i + lst[i]] = 1
        if i - 1 - lst[i] >= 0:
            if dp[i - lst[i] - 1]:
                dp[i] = 1
    if dp[n]:
        return 'YES'
    else:
        return 'NO'
casenum = int(input())
finaloutput = []
for i in range(casenum):
    n = int(input())
    targetlst = [0] + [int(num) for num in input().split()]
    temp = dp_send(n, targetlst)
    finaloutput.append(temp)
for item in finaloutput:
    print(item)

Solution (Python):
def main():
    T = int(input())
    for _ in range(T):
        n = int(input())
        a = list(map(int, input().split()))
        dp = [False] * (n + 1)
        dp[0] = True
        for i in range(1, n + 1):
            if a[i - 1] + 1 <= i and dp[i - a[i - 1] - 1]:
                dp[i] = True
            if i + a[i - 1] <= n and dp[i - 1]:
                dp[i + a[i - 1]] = True
        if dp[n]:
            print('Yes')
        else:
            print('No')
if __name__ == '__main__':
    main()

Solution (Python):
def solve(n, a):
    b = [0] * (n + 1)
    b[0] = 1
    for i in range(1, n + 1):
        if b[i - 1] and a[i] + i <= n:
            b[a[i] + i] = 1
        if i - a[i] >= 1 and b[i - a[i] - 1] == 1:
            b[i] = 1
    return 'YES' if b[n] else 'NO'
if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        n = int(input())
        a = [0] + list(map(int, input().split()))
        result = solve(n, a)
        print(result)