CF1227A.Math Problem

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Your math teacher gave you the following problem:

There are nn segments on the xx-axis, [l1;r1],[l2;r2],…,[ln;rn][l_1; r_1], [l_2; r_2], \ldots, [l_n; r_n]. The segment [l;r][l; r] includes the bounds, i.e. it is a set of such xx that l≤x≤rl \le x \le r. The length of the segment [l;r][l; r] is equal to r−lr - l.

Two segments [a;b][a; b] and [c;d][c; d] have a common point (intersect) if there exists xx that a≤x≤ba \leq x \leq b and c≤x≤dc \leq x \leq d. For example, [2;5][2; 5] and [3;10][3; 10] have a common point, but [5;6][5; 6] and [1;4][1; 4] don't have.

You should add one segment, which has at least one common point with each of the given segments and as short as possible (i.e. has minimal length). The required segment can degenerate to be a point (i.e a segment with length zero). The added segment may or may not be among the given nn segments.

In other words, you need to find a segment [a;b][a; b], such that [a;b][a; b] and every [li;ri][l_i; r_i] have a common point for each ii, and b−ab-a is minimal.

你的数学老师给你布置了如下题目:

在 xx 轴上有 nn 条线段:[l1;r1],[l2;r2],…,[ln;rn][l_1; r_1], [l_2; r_2], \ldots, [l_n; r_n]。线段 [l;r][l; r] 包含端点,即它是满足 l≤x≤rl \le x \le r 的所有 xx 构成的集合。线段 [l;r][l; r] 的长度为 r−lr - l。

两条线段 [a;b][a; b] 和 [c;d][c; d] 有公共点(相交),当且仅当存在某个 xx,使得 a≤x≤ba \leq x \leq b 且 c≤x≤dc \leq x \leq d。例如,[2;5][2; 5] 与 [3;10][3; 10] 有公共点,但 [5;6][5; 6] 与 [1;4][1; 4] 没有公共点。

你需要添加一条新线段,使其与给定的每一条线段都至少有一个公共点,并且该线段尽可能短(即长度最小)。所添加的线段可以退化为一个点(即长度为零的线段)。添加的线段可以是给定的 nn 条线段之一,也可以不是。

换言之,你需要找出一条线段 [a;b][a; b],使得对每个 ii,[a;b][a; b] 与 [li;ri][l_i; r_i] 均有公共点,且 b−ab-a 最小。

输入格式

The first line contains integer number tt (1≤t≤1001 \le t \le 100) — the number of test cases in the input. Then tt test cases follow.

The first line of each test case contains one integer nn (1≤n≤1051 \le n \le 10^{5}) — the number of segments. The following nn lines contain segment descriptions: the ii-th of them contains two integers li,ril_i,r_i (1≤li≤ri≤1091 \le l_i \le r_i \le 10^{9}).

The sum of all values nn over all the test cases in the input doesn't exceed 10510^5.

第一行包含一个整数 tt(1≤t≤1001 \le t \le 100)—— 表示输入中测试用例的数量。随后是 tt 个测试用例。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^{5})—— 表示线段的数量。接下来的 nn 行描述这些线段:其中第 ii 行包含两个整数 li,ril_i, r_i(1≤li≤ri≤1091 \le l_i \le r_i \le 10^{9})。

输入中所有测试用例的 nn 值之和不超过 10510^5。

输出格式

For each test case, output one integer — the smallest possible length of the segment which has at least one common point with all given segments.

对于每个测试用例,输出一个整数——与所有给定线段均至少有一个公共点的线段的最小可能长度。

输入输出样例

  • 输入#1

    4
    3
    4 5
    5 9
    7 7
    5
    11 19
    4 17
    16 16
    3 12
    14 17
    1
    1 10
    1
    1 1

    输出#1

    2
    4
    0
    0

说明/提示

In the first test case of the example, we can choose the segment [5;7][5;7] as the answer. It is the shortest segment that has at least one common point with all given segments.

在示例的第一个测试用例中,我们可以选择区间 [5;7][5;7] 作为答案。它是与所有给定区间均至少有一个公共点的最短区间。

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

首页