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();
}
}
Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A. Math Problem 水题的更多相关文章
- 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) 思路:我们想一下如果题目说的是最 ...
- 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 ...
- 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 ...
- 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, ...
- 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 ...
- 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 ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C Messy
//因为可以反转n次 所以可以得到任何可以构成的序列 #include<iostream> #include<string> #include<vector> us ...
- 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 ...
- 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 ...
随机推荐
- 「SAP技术」A项目关联公司间退货STO流程
[SAP技术]A项目关联公司间退货STO流程 1)创建公司间退货STO单据. 如下图示的公司间退货STO 4500000572, 2),VL10B, 创建交货单. 如下图交货单号:80044918, ...
- 如何用web3部署智能合约
合约示例 pragma solidity ^0.4.18; contract CallMeChallenge { bool public isComplete = false; function ca ...
- 一文解读Redis (转)
本文由葡萄城技术团队编撰并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 引言 在Web应用发展的初期,那时关系型数据库受到了较为广泛的关注和应用,原 ...
- 配置 yum 源的两种方法
配置 yum 源的两种方法 由于 redhat的yum在线更新是收费的,如果没有注册的话不能使用,如果要使用,需将redhat的yum卸载后,重启安装,再配置其他源,以下为详细过程: 1.删除red ...
- RMAN RECOVER TABLE 功能是 Oracle Database 12c 的新增功能 (Doc ID 1521524.1)
RMAN RECOVER TABLE Feature New to Oracle Database 12c (Doc ID 1521524.1) APPLIES TO: Oracle Database ...
- 自建KMS服务器激活Windows office
一.KMS服务器环境介绍 操作系统Centos 7.x Windows Server 也是可以的,此处以Centos7为例安装,使用默认1688端口号 二.服务端安装过程: 1.下载安装包(这 ...
- MongoDB学习笔记(六、MongoDB复制集与分片)
目录: MongoDB部署模型 MongoDB可复制集 MongoDB读写分离 分片架构部署 最佳实践 MongoDB部署模型: 单机 -> 可复制集 -> 分片集群 MongoDB可复制 ...
- js全局属性/函数
全局函数 eval () isFinite () 检查某个值是否为有穷大的数 isNaN () 检查某个值是否是数字 返回true或fa ...
- mysql windows 安装5.7
E:\MySQL\MySQLServer57\bin>net start mysqlMySQL 服务无法启动. 解决办法: 手动在安装目录创建文件 my.ini [mysql] # 设置mysq ...
- YAPI windows 二次开发 树形结构 多层级结构 拖拽 数据导入 接口自动化测试
什么是YAPI: 高效.易用.功能强大的API管理平台 http://yapi.demo.qunar.com/ github: https://github.com/YMFE/yapi 可以去那里下载 ...