CF1399F-Yet Another Segments Subset

目录

CF1399F-Yet Another Segments Subset

You are given n n segments on a coordinate axis OX OX . The i i -th segment has borders [li;ri] [l_i; r_i] . All points x x , for which lixri l_i \le x \le r_i holds, belong to the i 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 [li;ri] [l_i; r_i] and [lj;rj] [l_j; r_j] are non-intersecting if they have no common points. For example, segments [1;2] [1; 2] and [3;4] [3; 4] , [1;3] [1; 3] and [5;5] [5; 5] are non-intersecting, while segments [1;2] [1; 2] and [2;3] [2; 3] , [1;2] [1; 2] and [2;2] [2; 2] are intersecting.

The segment [li;ri] [l_i; r_i] lies inside the segment [lj;rj] [l_j; r_j] if ljli l_j \le l_i and rirj r_i \le r_j . For example, segments [2;2] [2; 2] , [2,3] [2, 3] , [3;4] [3; 4] and [2;4] [2; 4] lie inside the segment [2;4] [2; 4] , while [2;5] [2; 5] and [1;4] [1; 4] are not.

You have to answer t t independent test cases.

The first line of the input contains one integer t t ( 1t1000 1 \le t \le 1000 ) — the number of test cases. Then t t test cases follow.

The first line of the test case contains one integer n n ( 1n3000 1 \le n \le 3000 ) — the number of segments. The next n n lines describe segments. The i i -th segment is given as two integers li l_i and ri r_i ( 1liri2105 1 \le l_i \le r_i \le 2 \cdot 10^5 ), where li l_i is the left border of the i i -th segment and ri r_i is the right border of the i 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 n does not exceed 3000 3000 ( n3000 \sum n \le 3000 ).

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
1
2
3
4
3
4
2
7
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "ybwhead/ios.h"
// map<pair<int, int>, int> x;
int f[6010][6010];
int n;
int a[6010];
vector<int> e[6010];
int l[3010], r[3010];
int main()
{
    int TTT;
    yin >> TTT;
    while (TTT--)
    {
        yin >> n;
        // x.clear();
        for (int i = 1; i <= n; i++)
        {
            yin >> l[i] >> r[i];
            a[2 * i - 1] = l[i];
            a[2 * i] = r[i];
        }
        sort(a + 1, a + n * 2 + 1);
        int tot = unique(a + 1, a + n * 2 + 1) - a - 1;
        for (int i = 1; i <= tot; i++)
            e[i].clear();
        for (int i = 1; i <= n; i++)
            e[lower_bound(a + 1, a + tot + 1, l[i]) - a].push_back(lower_bound(a + 1, a + tot + 1, r[i]) - a);
        for (int i = 1; i <= tot; i++)
            sort(e[i].begin(), e[i].end());
        for (int l = 1; l <= tot; l++)
        {
            for (int j = l; j <= tot; j++)
            {
                int i = j - l + 1;
                int xx = count(e[i].begin(), e[i].end(), j);
                f[i][j] = 0;
                f[i][j] = max(f[i][j], xx + f[i + 1][j]);
                for (int k = 0; k < e[i].size(); k++)
                {
                    int nr = e[i][k];
                    if (nr >= j)
                        break;
                    f[i][j] = max(f[i][j], xx + f[i][nr] + f[nr + 1][j]);
                }
            }
        }
        yout << f[1][tot] << endl;
    }
    return 0;
}
来发评论吧~
Powered By Valine
v1.4.14