cf 1029 C
You are given nn segments on a number line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.
The intersection of a sequence of segments is such a maximal set of points (not necesserily having integer coordinates) that each point lies within every segment from the sequence. If the resulting set isn't empty, then it always forms some continuous segment. The length of the intersection is the length of the resulting segment or 00 in case the intersection is an empty set.
For example, the intersection of segments [1;5][1;5] and [3;10][3;10] is [3;5][3;5] (length 22), the intersection of segments [1;5][1;5] and [5;7][5;7] is [5;5][5;5] (length 00) and the intersection of segments [1;5][1;5] and [6;6][6;6] is an empty set (length 00).
Your task is to remove exactly one segment from the given sequence in such a way that the intersection of the remaining (n−1)(n−1) segments has the maximal possible length.
The first line contains a single integer nn (2≤n≤3⋅1052≤n≤3⋅105) — the number of segments in the sequence.
Each of the next nn lines contains two integers lili and riri (0≤li≤ri≤1090≤li≤ri≤109) — the description of the ii-th segment.
Print a single integer — the maximal possible length of the intersection of (n−1)(n−1) remaining segments after you remove exactly one segment from the sequence.
4
1 3
2 6
0 4
3 3
1
5
2 6
1 3
0 4
1 20
0 4
2
3
4 5
1 2
9 20
0
2
3 10
1 5
7
In the first example you should remove the segment [3;3][3;3], the intersection will become [2;3][2;3] (length 11). Removing any other segment will result in the intersection [3;3][3;3] (length 00).
In the second example you should remove the segment [1;3][1;3] or segment [2;6][2;6], the intersection will become [2;4][2;4] (length 22) or [1;3][1;3] (length 22), respectively. Removing any other segment will result in the intersection [2;3][2;3] (length 11).
In the third example the intersection will become an empty set no matter the segment you remove.
In the fourth example you will get the intersection [3;10][3;10] (length 77) if you remove the segment [1;5][1;5] or the intersection [1;5][1;5] (length 44) if you remove the segment [3;10][3;10].
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstdlib> 5 #include <cstring> 6 #include <string> 7 #include <deque> 8 #include <vector> 9 #include <set> 10 #include <map> 11 using namespace std; 12 #define ll long long 13 #define N 300009 14 int n,a[N],b[N],c[N],d[N]; 15 map<int,int>mp; 16 int maxx=-1,minn=1000000009; 17 bool check(int x,int y) 18 { 19 if(x!=y||x==y&&mp[y]>1) 20 return true; 21 return false; 22 } 23 int main() 24 { 25 scanf("%d",&n); 26 for(int i=1;i<=n;i++){ 27 scanf("%d%d",&a[i],&b[i]); 28 c[i]=a[i];d[i]=b[i]; 29 mp[a[i]]++;mp[b[i]]++; 30 maxx=max(maxx,a[i]); 31 minn=min(minn,b[i]); 32 } 33 //cout<<maxx<<" "<<minn<<endl; 34 sort(a+1,a+1+n); 35 sort(b+1,b+1+n); 36 int l=0; 37 for(int i=1;i<=n;i++){ 38 //cout<<maxx<<" "<<minn<<" "<<c[i]<<" "<<d[i]<<endl; 39 if(c[i]==maxx&&check(d[i],minn)){ 40 l=max(l,minn-a[n-1]); 41 } 42 if(d[i]==minn&&check(c[i],maxx)){ 43 l=max(l,b[2]-maxx); 44 } 45 if(c[i]==maxx&&d[i]==minn){//去掉某一个区间,那么区间的左右要同时去掉 46 //cout<<l<<endl; 47 l=max(l,b[2]-a[n-1]); 48 //cout<<b[2]<<" "<<a[n-1]<<endl; 49 } 50 } 51 /* 52 2 53 1 5 54 2 4 55 去掉2 4 56 2 4要同时去掉 57 */ 58 printf("%d\n",l); 59 return 0; 60 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现