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

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();
	}
}
posted @   qscqesze  阅读(352)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2016-11-24 Codeforces Round #381 (Div. 1) A. Alyona and mex 构造
2016-11-24 BZOJ 2648: SJY摆棋子 kdtree
2015-11-24 线段树 模板
2015-11-24 Codeforces Round #115 B. Plane of Tanks: Pro 水题
2015-11-24 Codeforces Round #115 A. Robot Bicorn Attack 暴力
点击右上角即可分享
微信分享提示