cf 1029 C
3 seconds
256 megabytes
standard input
standard output
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].
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define ll long long
#define N 300009
int n,a[N],b[N],c[N],d[N];
map<int,int>mp;
int maxx=-,minn=;
bool check(int x,int y)
{
if(x!=y||x==y&&mp[y]>)
return true;
return false;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&a[i],&b[i]);
c[i]=a[i];d[i]=b[i];
mp[a[i]]++;mp[b[i]]++;
maxx=max(maxx,a[i]);
minn=min(minn,b[i]);
}
//cout<<maxx<<" "<<minn<<endl;
sort(a+,a++n);
sort(b+,b++n);
int l=;
for(int i=;i<=n;i++){
//cout<<maxx<<" "<<minn<<" "<<c[i]<<" "<<d[i]<<endl;
if(c[i]==maxx&&check(d[i],minn)){
l=max(l,minn-a[n-]);
}
if(d[i]==minn&&check(c[i],maxx)){
l=max(l,b[]-maxx);
}
if(c[i]==maxx&&d[i]==minn){//去掉某一个区间,那么区间的左右要同时去掉
//cout<<l<<endl;
l=max(l,b[]-a[n-]);
//cout<<b[2]<<" "<<a[n-1]<<endl;
}
}
/*
2
1 5
2 4
去掉2 4
2 4要同时去掉
*/
printf("%d\n",l);
return ;
}
最新文章
- AFNetworking报错";_UTTypeCopyPreferredTagWithClass";, referenced from: _AFContentTypeForPathExtens
- RPN-逆波兰计算器-中缀表达式转后缀表达式-javascript
- 超级强大的formValidator
- VB6.0对鼠标滚轮不支持的解决方法[转]已经测试work
- Codeforces 85D Sum of Medians
- 修改ptrace_scope
- spring exception--No unique bean of type
- CSS样式权值
- Linux - How To Set Up an NFS Mount on CentOS 6
- leetcode Roman to Integer python
- JSF教程(9)——生命周期之Process Validations Phase
- C语言实现进制转换——超图面试题
- MySQL分区表的局限和限制
- MySQL 索引管理与执行计划
- 使用zii.widgets.CDetailView显示内容
- EntityFrameWork实现部分字段获取和修改(含源码)
- 12 SharedPreferences
- 其他综合-fdisk一键分区操作-无需脚本
- Lua中__index和__newindex实践
- Red-Gate.NET.Reflector.v8.0.1.308(内含注册机Keygen与注册图解)