A. Math Problem

Your math teacher gave you the following problem:

There are n segments on the x-axis, [l1;r1],[l2;r2],…,[ln;rn]. The segment [l;r] includes the bounds, i.e. it is a set of such x that l≤x≤r. The length of the segment [l;r] is equal to r−l.

Two segments [a;b] and [c;d] have a common point (intersect) if there exists x that a≤x≤b and c≤x≤d. For example, [2;5] and [3;10] have a common point, but [5;6] and [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 n segments.

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

Input

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

The first line of each test case contains one integer n (1≤n≤105) — the number of segments. The following n lines contain segment descriptions: the i-th of them contains two integers li,ri (1≤li≤ri≤109).

The sum of all values n over all the test cases in the input doesn't exceed 105.

Output

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.

Example

input

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

output

2

4

0

0

Note

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

题意

给你若干个区间,然后你需要找到一个长度最小的区间,覆盖住所有的区间,问你这个区间最小是多少

题解

我们考虑l最大能够是多少,r最小能够是多少,然后我们就发现 l = min(a[i].r,l),r = max(a[i].l,r),然后最后取个差值就好

代码

#include<bits/stdc++.h>
using namespace std; vector<pair<int,int> >A;
void solve(){
int n;cin>>n;
A.clear();
for(int i=0;i<n;i++){
int x,y;
cin>>x>>y;
A.push_back(make_pair(x,y));
}
sort(A.begin(),A.end());
int ll = A[0].first,lr=A[0].second;
int rl = A[0].first,rr=A[0].second;
for(int i=0;i<n;i++){
lr = min(lr,A[i].second);
rl = max(rl,A[i].first);
}
// cout<<lr<<" "<<rl<<endl;
if(lr>=rl){
cout<<"0"<<endl;
}else{
cout<<rl-lr<<endl;
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
solve();
}
}

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A. Math Problem 水题的更多相关文章

  1. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3

    A,有多个线段,求一条最短的线段长度,能过覆盖到所又线段,例如(2,4)和(5,6) 那么我们需要4 5连起来,长度为1,例如(2,10)(3,11),用(3,10) 思路:我们想一下如果题目说的是最 ...

  2. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) F2. Wrong Answer on test 233 (Hard Version) dp 数学

    F2. Wrong Answer on test 233 (Hard Version) Your program fails again. This time it gets "Wrong ...

  3. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) E. Arson In Berland Forest 二分 前缀和

    E. Arson In Berland Forest The Berland Forest can be represented as an infinite cell plane. Every ce ...

  4. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) D2. Optimal Subsequences (Hard Version) 数据结构 贪心

    D2. Optimal Subsequences (Hard Version) This is the harder version of the problem. In this version, ...

  5. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C. Messy 构造

    C. Messy You are fed up with your messy room, so you decided to clean it up. Your room is a bracket ...

  6. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B. Box 贪心

    B. Box Permutation p is a sequence of integers p=[p1,p2,-,pn], consisting of n distinct (unique) pos ...

  7. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C Messy

    //因为可以反转n次 所以可以得到任何可以构成的序列 #include<iostream> #include<string> #include<vector> us ...

  8. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B Box

    #include<bits/stdc++.h> using namespace std; ]; ]; int main() { int total; cin>>total; w ...

  9. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A Math Problem

    //只要从所有区间右端点的最小值覆盖到所有区间左端点的最大值即可 #include<iostream> using namespace std ; int x,y; int n; int ...

随机推荐

  1. vim 入门笔记

    前言 本文的初衷 从知道 vim 开始我就有心学习并尝试过几次,每次都是暂时的心血来潮,最终全部不了了之,就连最基本的 vimtutor 我都是学个两三节就半途而废,所以这次干脆写篇文章,利用几次学习 ...

  2. Android五大布局详解——TableLayout(表格布局)

    TableLayout 前面所学的LinearLayout和RelativeLayout两大布局已经完全适用于各种开发条件下,其他的布局仅供参考学习,毕竟知识就是力量,以后的开发过程中万一遇到也能游刃 ...

  3. LeetCode刷题191203 --回溯算法

    虽然不是每天都刷,但还是不想改标题,(手动狗头 题目及解法来自于力扣(LeetCode),传送门. 算法(78): 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明: ...

  4. C++ 进阶笔记之一

    优化相关 使用灵活的.动态分配的数据,不要使用固定大小多数组: 优先使用线性算法或者尽可能快的算法: push_back 散列表查询:O(1) set/map lower_bound/upper_bo ...

  5. vue 脚手架

    Vue 脚手架的基本用法 1. 基于 3.X 版本的脚手架 创建vue项目 命令行(CLI) 的方式创建 vue 项目 vue create my-project 图形化界面(GUI) 的方式创建 v ...

  6. IO 多路复用详解

    转自:https://blog.csdn.net/sehanlingfeng/article/details/78920423

  7. java之对象类型转换

    基本数据类型之间的转换: 自动类型转换:小的数据类型可以自动转换成大的数据类型: 强制类型转换:可以把大的数据类型转换成小的数据类型:float = (float)32.0; public class ...

  8. c++实现通讯录管理系统(控制台版)

    c++实现通讯录管理系统(控制台版) 此项目适合c++初学者,针对c++基础知识,涉及到变量.结构体定义使用.数组定义使用.指针定义使用等. 运行之后的结果如下: 代码: #include <i ...

  9. Java 添加Word脚注、尾注

    Word中的脚注和尾注都是对文本的补充说明.脚注一般是附在书页最左下端的注文,用以解释.说明特定内容:而尾注则是位于文档末尾,用于列出引文的出处.脚注和尾注都可以是针对某些文字或者段落来添加.本文中, ...

  10. PlayJava Day017

    今日所学: /* 2019.08.19开始学习,此为补档. */ 1.数组变量 a.数组变量是数组的管理者而非数组本身 b.数组必须创建出来然后交给数组变量来管理 c.数组变量之间的赋值是管理权限的赋 ...