51nod 1091 线段重叠
来源:互联网 发布:推背图的骗局 知乎 编辑:程序博客网 时间:2024/06/07 01:39
排序贪心,按左端点从小到大,相同时右端点从小到大。
选择一个参考点,初始化为最左端的线段的右端点
对于之后可能出现的情况有3种,
1 被包含在参考点所在线段中,更新最大值,切参考点保持不变。
2 参考点在线段的中间,更新最大值,切参考点改变,为了尽可能包含长的线段。
3 线段和参考点没有相交的部分,在右边,可以和第2种写在一起。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <vector>#include <map>#include <cmath>#include <set>#include <queue>using namespace std;const int INF=1e9+10;const double EPS = 1e-10; typedef long long ll;struct node { int x,y; }p[50001]; int cmp(node a,node b) { if(a.x!=b.x) return a.x<b.x; else return a.y<b.y; } int main(){//freopen("out.txt","w",stdout);int n; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d %d",&p[i].x,&p[i].y); sort(p,p+n,cmp); int k=p[0].y; int ans=0; for(int i=1;i<n;i++){ if(p[i].y<=k){ ans=max(ans,p[i].y-p[i].x); } else{ ans=max(ans,k-p[i].x); k=p[i].y; } } printf("%d\n",ans ); return 0; }
0 0
- 51nod 1091 线段重叠
- 51nod-1091 . 线段的重叠
- 51nod 1091 线段的重叠(贪心)
- 51nod 1091 线段的重叠
- 51nod 1091 线段的重叠
- 51nod 1091 线段的重叠 贪心
- 51nod 1091 线段的重叠
- 51nod 1091 线段的重叠 贪心
- 贪心-51nod 1091 线段的重叠
- 51nod-【1091 线段的重叠】
- 51nod 1091 线段的重叠
- 51nod 1091 线段的重叠
- 51nod 1091 线段的重叠
- 51 NOD 1091 线段的重叠
- 51Nod 1091 线段的重叠
- 51 nod 1091 线段的重叠
- 51Nod-1091 线段的重叠【排序】
- 51Nod 1091 线段的重叠
- php之mysql_connet()
- 跨域详解
- Java正则表达式
- Windows终端颜色设置
- CSU 1903: Tricky数
- 51nod 1091 线段重叠
- 6.1.4.Drv/PWM(Character Device Drv)
- HDU---1829 A Bug's Life【并查集】
- 面试14之请实现一种数据结构SetOfStacks
- Js选择框脚本 移动操作select 标签中的 option 项的操作事项
- Linux——分区时出现This partition should be marked for use as an "EFI boot partition"
- 多频段融合方法——图像拼接
- Android与h5交互
- 【Eclipse】安装Eclipse Color Theme 插件