Pinball (Codeforces 1937D)
Tags: Implement, two pointers

There is a one-dimensional grid of length $$$n$$$. The $$$i$$$-th cell of the grid contains a character $$$s_i$$$, which is either '<' or '>'.When a pinball is placed on one of the cells, it moves according to the following rules:   If the pinball is on the $$$i$$$-th cell and $$$s_i$$$ is '<', the pinball moves one cell to the left in the next second. If $$$s_i$$$ is '>', it moves one cell to the right.  After the pinball has moved, the character $$$s_i$$$ is inverted (i. e. if $$$s_i$$$ used to be '<', it becomes '>', and vice versa).  The pinball stops moving when it leaves the grid: either from the left border or from the right one. You need to answer $$$n$$$ independent queries. In the $$$i$$$-th query, a pinball will be placed on the $$$i$$$-th cell. Note that we always place a pinball on the initial grid.For each query, calculate how many seconds it takes the pinball to leave the grid. It can be shown that the pinball will always leave the grid within a finite number of steps.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$).The second line of each test case contains a string $$$s_1s_2 \ldots s_{n}$$$ of length $$$n$$$ consisting of characters '<' and '>'.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 \cdot 10^5$$$.

Output: For each test case, for each $$$i$$$ ($$$1 \le i \le n$$$) output the answer if a pinball is initially placed on the $$$i$$$-th cell.

Note: In the first test case, the movement of the pinball for $$$i=1$$$ is shown in the following pictures. It takes the pinball $$$3$$$ seconds to leave the grid.  The movement of the pinball for $$$i=2$$$ is shown in the following pictures. It takes the pinball $$$6$$$ seconds to leave the grid.  

Sample Inputs:
3
3
><<
4
<<<<
6
<><<<>

Sample Outputs:
3 6 5 
1 2 3 4 
1 4 7 10 8 1

Solution (Python):
t = int(input())

def solve(s):
    n = len(s)
    A = []
    idx_A = []
    for i in range(0, n):
        if s[i] == '>':
            if len(idx_A) == 0:
                A.append(0)
            else:
                x = A[len(A) - 1] + (i - idx_A[len(idx_A) - 1]) * len(idx_A)
                A.append(x)
            idx_A.append(i)
    B = []
    idx_B = []
    for j in range(0, n):
        i = n - 1 - j
        if s[i] == '<':
            if len(idx_B) == 0:
                B.append(0)
            else:
                x = B[len(B) - 1] + (idx_B[len(B) - 1] - i) * len(idx_B)
                B.append(x)
            idx_B.append(i)
    l = 0
    r = len(B)
    for i in range(0, n):
        if s[i] == '>':
            if l < r:
                a = A[l]
                x = r - (l + 2)
                b = B[r - 1]
                if x >= 0:
                    b = b - B[x]
                    b = b - (idx_B[x] - idx_B[r - 1]) * (x + 1)
                b = b + (idx_B[r - 1] - i) * (l + 1)
                print(a * 2 + b * 2 + i + 1, end=' ')
            elif r == 0:
                print(n - i, end=' ')
            else:
                a = B[r - 1] + (idx_B[r - 1] - i) * r
                b = A[l - 1]
                if l - r > 0:
                    b = b - A[l - r - 1]
                    b = b - (idx_A[l - 1] - idx_A[l - r - 1]) * (l - r)
                b = b + (i - idx_A[l - 1]) * r
                print(a * 2 + b * 2 + (n - i), end=' ')
            l += 1
        else:
            r -= 1
            if l <= r:
                if l == 0:
                    print(i + 1, end=' ')
                else:
                    a = A[l - 1]
                    a = a + (i - idx_A[l - 1]) * l
                    b = B[r]
                    if r - l >= 0:
                        b = b - B[r - l]
                        b = b - (idx_B[r - l] - i) * (r - l)
                    b = b + (idx_B[r] - i) * l
                    print(a * 2 + b * 2 + i + 1, end=' ')
            elif r == 0:
                print(n - i + (i - idx_A[l - 1]) * 2, end=' ')
            else:
                a = B[r]
                b = A[l - 1]
                if l - r - 1 > 0:
                    b = b - A[l - r - 2]
                    b = b - (idx_A[l - 1] - idx_A[l - r - 2]) * (l - r - 1)
                b = b + (i - idx_A[l - 1]) * (r + 1)
                print(a * 2 + b * 2 + (n - i), end=' ')
for i in range(0, t):
    n = int(input())
    s = input()
    solve(s)
    print()

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sint = lambda : int(input())
mint = lambda : map(int, input().split())
ints = lambda : list(map(int, input().split()))
N = 500010
(l, r, lp, rp) = ([0] * N, [0] * N, [0] * N, [0] * N)
T = int(input())
for _ in range(T):
    n = int(input())
    s = input()
    (lcnt, rcnt) = (0, 0)
    for i in range(n):
        l[i + 1] = r[i + 1] = 0
        if s[i] == '<':
            l[i + 1] = 1
            lcnt += 1
            lp[lcnt] = lp[lcnt - 1] + i + 1
        else:
            r[i + 1] = 1
            rcnt += 1
            rp[rcnt] = rp[rcnt - 1] + i + 1
        l[i + 1] += l[i]
        r[i + 1] += r[i]
    for i in range(1, n + 1):
        (lright, rleft) = (r[i - 1], l[n] - l[i])
        k = min(lright, rleft)
        if s[i - 1] == '<':
            ans = i + 2 * (lp[l[i] + k] - lp[l[i]]) - 2 * (rp[lright] - rp[lright - k])
            if rleft < lright:
                ans += n + 1 - 2 * (rp[lright - k] - rp[lright - k - 1])
            print(ans, end=' ')
        else:
            ans = 2 * (lp[l[i] + k] - lp[l[i]]) - 2 * (rp[lright] - rp[lright - k]) - i + n + 1
            if lright < rleft:
                ans += 2 * (lp[l[i] + k + 1] - lp[l[i] + k]) - n - 1
            print(ans, end=' ')
    print()

Solution (Python):
from itertools import accumulate

def last(arr, times):
    n = len(arr)
    return arr[-1] - arr[max(-n, -1 - times)]

def solve(s1, n):
    pre = [i + 1 for (i, el) in enumerate(s1) if el == '>']
    totalOne = len(pre)
    preOne = list(accumulate(pre, initial=0))
    suffZero = [0]
    ans = [0] * n
    for i in range(n - 1, -1, -1):
        el = s1[i]
        if el == '>':
            (ol, zr) = (totalOne, len(suffZero) - 1)
            if ol <= zr:
                zeroInd = 2 * last(suffZero, ol)
                oneInd = 2 * preOne[-1] - last(preOne, 1)
                ans[i] = zeroInd - oneInd
            else:
                zeroInd = 2 * suffZero[-1]
                oneInd = last(preOne, zr) + last(preOne, zr + 1)
                oneInd -= last(preOne, 1)
                fi = last(preOne, zr + 1) - last(preOne, zr)
                ans[i] = zeroInd - oneInd + n + 1 - fi
            preOne.pop()
            totalOne -= 1
        else:
            suffZero.append(suffZero[-1] + i + 1)
            (ol, zr) = (totalOne, len(suffZero) - 1)
            if zr <= ol:
                zeroInd = suffZero[-1] + suffZero[-2]
                oneInd = 2 * last(preOne, zr)
                ans[i] = zeroInd - oneInd + n + 1
            else:
                zeroInd = 2 * last(suffZero, ol + 1) - last(suffZero, 1)
                oneInd = 2 * preOne[-1]
                ans[i] = zeroInd - oneInd
    print(*ans)
for case in range(int(input())):
    n = int(input())
    s1 = input()
    solve(s1, n)