Yet Another Segments Subset (Codeforces 1399F)
Tags: DP, graphs

You are given $$$n$$$ segments on a coordinate axis $$$OX$$$. The $$$i$$$-th segment has borders $$$[l_i; r_i]$$$. All points $$$x$$$, for which $$$l_i \le x \le r_i$$$ holds, belong to the $$$i$$$-th segment.Your task is to choose the maximum by size (the number of segments) subset of the given set of segments such that each pair of segments in this subset either non-intersecting or one of them lies inside the other one.Two segments $$$[l_i; r_i]$$$ and $$$[l_j; r_j]$$$ are non-intersecting if they have no common points. For example, segments $$$[1; 2]$$$ and $$$[3; 4]$$$, $$$[1; 3]$$$ and $$$[5; 5]$$$ are non-intersecting, while segments $$$[1; 2]$$$ and $$$[2; 3]$$$, $$$[1; 2]$$$ and $$$[2; 2]$$$ are intersecting.The segment $$$[l_i; r_i]$$$ lies inside the segment $$$[l_j; r_j]$$$ if $$$l_j \le l_i$$$ and $$$r_i \le r_j$$$. For example, segments $$$[2; 2]$$$, $$$[2, 3]$$$, $$$[3; 4]$$$ and $$$[2; 4]$$$ lie inside the segment $$$[2; 4]$$$, while $$$[2; 5]$$$ and $$$[1; 4]$$$ are not.You have to answer $$$t$$$ independent test cases.

Input: The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow.The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 3000$$$) — the number of segments. The next $$$n$$$ lines describe segments. The $$$i$$$-th segment is given as two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le 2 \cdot 10^5$$$), where $$$l_i$$$ is the left border of the $$$i$$$-th segment and $$$r_i$$$ is the right border of the $$$i$$$-th segment.Additional constraint on the input: there are no duplicates in the list of segments.It is guaranteed that the sum of $$$n$$$ does not exceed $$$3000$$$ ($$$\sum n \le 3000$$$).

Output: For each test case, print the answer: the maximum possible size of the subset of the given set of segments such that each pair of segments in this subset either non-intersecting or one of them lies inside the other one.


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

Sample Outputs:
3
4
2
7

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
ans = []
x = [[] for _ in range(6006)]
for _ in range(t):
    n = int(input())
    s = set()
    lr = []
    for _ in range(n):
        (l, r) = map(int, input().split())
        s.add(l)
        s.add(r)
        lr.append((l, r))
    s = list(s)
    s.sort()
    d = dict()
    for i in range(len(s)):
        d[s[i]] = i + 1
    m = 0
    for (l, r) in lr:
        (l0, r0) = (d[l], d[r])
        x[r0 - l0].append((l0, r0))
        m = max(m, r0 - l0)
    y = [[] for _ in range(len(s) + 1)]
    for i in range(m + 1):
        x[i].sort(reverse=True)
        while x[i]:
            (l, r) = x[i].pop()
            dp = [0] * (r - l + 2)
            dp[0] = 1
            for r0 in range(l, r + 1):
                j = r0 - l + 1
                for (l0, c) in y[r0]:
                    if l0 < l:
                        continue
                    dp[j] = max(dp[j], dp[l0 - l] + c)
                dp[j] = max(dp[j], dp[j - 1])
            y[r].append((l, dp[-1]))
    m = len(s)
    dp = [0] * (m + 1)
    for r in range(1, m + 1):
        dpr = 0
        for (l, c) in y[r]:
            dpr = max(dpr, dp[l - 1] + c)
        dp[r] = max(dpr, dp[r - 1])
    ans0 = dp[m]
    ans.append(ans0)
sys.stdout.write('\n'.join(map(str, ans)))

Solution (Python):
import sys
input = sys.stdin.buffer.readline

def solve():
    n = int(input())
    LR = [tuple(map(int, input().split())) for i in range(n)]
    s = set()
    for (l, r) in LR:
        s.add(l)
        s.add(r)
    dic = {x: i for (i, x) in enumerate(sorted(list(s)))}
    le = len(s)
    seg = [[] for i in range(le)]
    for (l, r) in LR:
        seg[dic[r]].append(dic[l])
    dp = [[len(seg[0])]]
    for i in range(1, le):
        dp.append(dp[-1] + [0])
        for l in sorted(seg[i], reverse=True):
            dp[i][l] += 1
            for j in range(l):
                dp[i][j] = max(dp[i][j], dp[i][l] + dp[l - 1][j])
    return dp[-1][0]
t = int(input())
for _ in range(t):
    ans = solve()
    print(ans)

Solution (Python):
def f(sl):
    sl.sort(key=lambda s: (s[1], -s[0]))
    tl = sorted(list(set([s[0] for s in sl] + [s[1] for s in sl])))
    cd = {tl[i]: i for i in range(len(tl))}
    csl = [[] for i in range(len(tl))]
    for (s0, s1) in sl:
        csl[cd[s1]].append(cd[s0])
    dp = [[len(csl[0])]]
    for r in range(1, len(tl)):
        dp.append(dp[-1] + [0])
        for li in csl[r]:
            dp[r][li] += 1
            for l in range(li):
                dp[r][l] = max(dp[r][l], dp[r][li] + dp[li - 1][l])
    return dp[-1][0]
T = int(input())
for i in range(T):
    n = int(input())
    ll = [list(map(int, input().split())) for _ in range(n)]
    print(f(ll))