Shortest and Longest LIS (Codeforces 1304D)
Tags: Greedy, two pointers

Gildong recently learned how to find the longest increasing subsequence (LIS) in $$$O(n\log{n})$$$ time for a sequence of length $$$n$$$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $$$n$$$ distinct integers between $$$1$$$ and $$$n$$$, inclusive, to test his code with your output.The quiz is as follows.Gildong provides a string of length $$$n-1$$$, consisting of characters '<' and '>' only. The $$$i$$$-th (1-indexed) character is the comparison result between the $$$i$$$-th element and the $$$i+1$$$-st element of the sequence. If the $$$i$$$-th character of the string is '<', then the $$$i$$$-th element of the sequence is less than the $$$i+1$$$-st element. If the $$$i$$$-th character of the string is '>', then the $$$i$$$-th element of the sequence is greater than the $$$i+1$$$-st element.He wants you to find two possible sequences (not necessarily distinct) consisting of $$$n$$$ distinct integers between $$$1$$$ and $$$n$$$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible.

Input: Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$).Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $$$n-1$$$.It is guaranteed that the sum of all $$$n$$$ in all test cases doesn't exceed $$$2 \cdot 10^5$$$.

Output: For each test case, print two lines with $$$n$$$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $$$1$$$ and $$$n$$$, inclusive, and should satisfy the comparison results.It can be shown that at least one answer always exists.

Note: In the first case, $$$1$$$ $$$2$$$ $$$3$$$ is the only possible answer.In the second case, the shortest length of the LIS is $$$2$$$, and the longest length of the LIS is $$$3$$$. In the example of the maximum LIS sequence, $$$4$$$ '$$$3$$$' $$$1$$$ $$$7$$$ '$$$5$$$' $$$2$$$ '$$$6$$$' can be one of the possible LIS.

Sample Inputs:
3
3 <<
7 >><>><
5 >>><

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

Solution (Python):
import sys

def reverse(s):
    return s[::-1]

def main():
    N = int(sys.stdin.readline().strip())
    for _ in range(N):
        (n, s) = sys.stdin.readline().strip().split()
        n = int(n)
        a0 = [0] * n
        cnt0 = 1
        for i in list(range(n - 1))[::-1]:
            if s[i] == '>':
                a0[i + 1] = cnt0
                cnt0 += 1
        a1 = a0[:]
        cnt1 = cnt0
        for i in list(range(n))[::-1]:
            if a1[i] == 0 and (i == 0 or a1[i - 1] != 0):
                j = i
                while j < n and a1[j] == 0:
                    a1[j] = cnt1
                    cnt1 += 1
                    j += 1
        print(' '.join((str(x) for x in a1)))
        a2 = a0[:]
        cnt2 = cnt0
        for i in list(range(n)):
            if a2[i] == 0 and (i == 0 or a2[i - 1] != 0):
                j = i
                while j < n and a2[j] == 0:
                    a2[j] = cnt2
                    cnt2 += 1
                    j += 1
        print(' '.join((str(x) for x in a2)))
main()

Solution (Python):
def makeRepeat(s):
    repeat = [[s[0], 0]]
    for ch in s:
        if ch == repeat[-1][0]:
            repeat[-1][1] += 1
        else:
            repeat.append([ch, 1])
    return repeat

def solve(N, S):
    assert len(S) == N - 1
    curr = 0
    repeat = makeRepeat(S)
    longest = list(range(1, N + 1))
    shortest = list(reversed(range(1, N + 1)))
    for (ch, count) in repeat:
        if ch == '>':
            longest[curr:curr + count + 1] = reversed(longest[curr:curr + count + 1])
        else:
            assert ch == '<'
            shortest[curr:curr + count + 1] = reversed(shortest[curr:curr + count + 1])
        curr += count
    return ' '.join(map(str, shortest)) + '\n' + ' '.join(map(str, longest))
if __name__ == '__main__':
    (T,) = map(int, input().split())
    for t in range(T):
        (N, S) = input().split()
        N = int(N)
        ans = solve(N, S)
        print(ans)

Solution (Python):
t = int(input())
for test_case in range(t):
    (n, pattern) = input().split()
    n = int(n)
    ans = [0] * n
    (num, last) = (n, 0)
    for i in range(n):
        if i == n - 1 or pattern[i] == '>':
            for j in range(i, last - 1, -1):
                ans[j] = num
                num -= 1
            last = i + 1
    print(' '.join((str(k) for k in ans)))
    (num, last) = (1, 0)
    for i in range(n):
        if i == n - 1 or pattern[i] == '<':
            for j in range(i, last - 1, -1):
                ans[j] = num
                num += 1
            last = i + 1
    print(' '.join((str(k) for k in ans)))