CF1841D.Pairs of Segments

普及+/提高

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Two segments [l1,r1][l_1, r_1] and [l2,r2][l_2, r_2] intersect if there exists at least one xx such that l1≤x≤r1l_1 \le x \le r_1 and l2≤x≤r2l_2 \le x \le r_2.

An array of segments [[l1,r1],[l2,r2],…,[lk,rk]][[l_1, r_1], [l_2, r_2], \dots, [l_k, r_k]] is called beautiful if kk is even, and is possible to split the elements of this array into k2\frac{k}{2} pairs in such a way that:

  • every element of the array belongs to exactly one of the pairs;
  • segments in each pair intersect with each other;
  • segments in different pairs do not intersect.

For example, the array [[2,4],[9,12],[2,4],[7,7],[10,13],[6,8]][[2, 4], [9, 12], [2, 4], [7, 7], [10, 13], [6, 8]] is beautiful, since it is possible to form 33 pairs as follows:

  • the first element of the array (segment [2,4][2, 4]) and the third element of the array (segment [2,4][2, 4]);
  • the second element of the array (segment [9,12][9, 12]) and the fifth element of the array (segment [10,13][10, 13]);
  • the fourth element of the array (segment [7,7][7, 7]) and the sixth element of the array (segment [6,8][6, 8]).

As you can see, the segments in each pair intersect, and no segments from different pairs intersect.

You are given an array of nn segments [[l1,r1],[l2,r2],…,[ln,rn]][[l_1, r_1], [l_2, r_2], \dots, [l_n, r_n]]. You have to remove the minimum possible number of elements from this array so that the resulting array is beautiful.

两个线段 [l1,r1][l_1, r_1] 和 [l2,r2][l_2, r_2] 相交,当且仅当存在至少一个 xx,使得 l1≤x≤r1l_1 \le x \le r_1 且 l2≤x≤r2l_2 \le x \le r_2。

一个线段数组 [[l1,r1],[l2,r2],…,[lk,rk]][[l_1, r_1], [l_2, r_2], \dots, [l_k, r_k]] 被称为优美的(beautiful),如果满足以下条件:

  • kk 是偶数;
  • 可以将该数组的元素恰好划分为 k2\frac{k}{2} 对,使得:
    • 数组中的每个元素恰好属于其中一对;
    • 每对中的两个线段彼此相交;
    • 不同对中的线段互不相交。

例如,数组 [[2,4],[9,12],[2,4],[7,7],[10,13],[6,8]][[2, 4], [9, 12], [2, 4], [7, 7], [10, 13], [6, 8]] 是优美的,因为可以如下构造 33 对:

  • 数组的第一个元素(线段 [2,4][2, 4])与第三个元素(线段 [2,4][2, 4])配对;
  • 数组的第二个元素(线段 [9,12][9, 12])与第五个元素(线段 [10,13][10, 13])配对;
  • 数组的第四个元素(线段 [7,7][7, 7])与第六个元素(线段 [6,8][6, 8])配对。

如你所见,每对中的线段均相交,而不同对之间的线段互不相交。

现给你一个包含 nn 个线段的数组 [[l1,r1],[l2,r2],…,[ln,rn]][[l_1, r_1], [l_2, r_2], \dots, [l_n, r_n]]。你需要从中删除最少数量的元素,使得剩余数组是优美的。

输入格式

The first line contains one integer tt (1≤t≤10001 \le t \le 1000) — the number of test cases.

The first line of each test case contains one integer nn (2≤n≤20002 \le n \le 2000) — the number of segments in the array. Then, nn lines follow, the ii-th of them contains two integers lil_i and rir_i (0≤li≤ri≤1090 \le l_i \le r_i \le 10^9) denoting the ii-th segment.

Additional constraint on the input: the sum of nn over all test cases does not exceed 20002000.

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(2≤n≤20002 \le n \le 2000)—— 数组中线段的数量。随后是 nn 行,其中第 ii 行包含两个整数 lil_i 和 rir_i(0≤li≤ri≤1090 \le l_i \le r_i \le 10^9),表示第 ii 条线段。

输入的额外约束:所有测试用例的 nn 值之和不超过 20002000。

输出格式

For each test case, print one integer — the minimum number of elements you have to remove so that the resulting array is beautiful.

对于每个测试用例,输出一个整数——即需要删除的最少元素个数,使得剩余数组是优美的。

输入输出样例

  • 输入#1

    3
    7
    2 4
    9 12
    2 4
    7 7
    4 8
    10 13
    6 8
    5
    2 2
    2 8
    0 10
    1 2
    5 6
    4
    1 1
    2 2
    3 3
    4 4

    输出#1

    1
    3
    4

说明/提示

In the first test case of the example, it is enough to delete the 55-th element of the array of segments. Then you get the array [[2,4],[9,12],[2,4],[7,7],[10,13],[6,8]][[2, 4], [9, 12], [2, 4], [7, 7], [10, 13], [6, 8]], which is beautiful.

In the second test case of the example, you can delete the 11-st, 33-rd and 44-th element of the array. Then you get the array [[2,8],[5,6]][[2, 8], [5, 6]], which is beautiful.

In the third test case of the example, you have to delete the whole array.

在示例的第一个测试用例中,只需删除数组中第 55 个线段即可。此时得到的数组为 [[2,4],[9,12],[2,4],[7,7],[10,13],[6,8]][[2, 4], [9, 12], [2, 4], [7, 7], [10, 13], [6, 8]],该数组是优美的。

在示例的第二个测试用例中,可以删除数组中第 11、第 33 和第 44 个元素。此时得到的数组为 [[2,8],[5,6]][[2, 8], [5, 6]],该数组是优美的。

在示例的第三个测试用例中,必须删除整个数组。

输入解题思路,AI测评打分。不知道怎么写?

首页