51nod 1091 线段的重叠
来源:互联网 发布:网络金融产品都有哪些 编辑:程序博客网 时间:2024/06/11 18:50
1091 线段的重叠
X轴上有N条线段,每条线段包括1个起点和终点。线段的重叠是这样来算的,[10 20]和[12 25]的重叠部分为[12 20]。
给出N条线段的起点和终点,从中选出2条线段,这两条线段的重叠部分是最长的。输出这个最长的距离。如果没有重叠,输出0。
Input
第1行:线段的数量N(2 <= N <= 50000)。
第2 - N + 1行:每行2个数,线段的起点和终点。(0 <= s , e <= 10^9)
Output
输出最长重复区间的长度。
Input示例
5
1 5
2 4
2 8
3 7
7 9
Output示例
4
题解:
排序一下。o(n2)扫一下即可。
注意p[i].second
代码:
#include <bits/stdc++.h>using namespace std;const int maxn = 50000+5;typedef pair<int,int> temp;pair<int,int> p[maxn];bool cmp(temp a,temp b){ if(a.first!=b.first) { return a.first<b.first; } else { return a.second<b.second; }}int main(){ int n;cin>>n; for(int i=0;i<n;i++) { cin>>p[i].first>>p[i].second; } sort(p,p+n,cmp); int ans=0; for(int i=0;i<n-1;i++) { for(int j=i+1;j<n;j++) { int pi=max(p[i].first,p[j].first); int pj=min(p[i].second,p[j].second); ans=max(ans,pj-pi); if(p[i].second<p[j].second) break; } } cout<<ans<<endl; 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 线段的重叠
- 51 NOD 1091 线段的重叠
- 51Nod 1091 线段的重叠
- 51 nod 1091 线段的重叠
- 51Nod-1091 线段的重叠【排序】
- 51Nod 1091 线段的重叠
- 51nod 1091 线段的重叠
- 【SpringCloud】(十二):Fegion的Hystrix支持
- 《第一行代码》自学笔记-Android中异步消息处理机制&&AsyncTask的用法
- PagerAdapter 适配器
- 怎样教你牢记17个的Win7快捷键!
- Dungeon Master(BFS)
- 51nod 1091 线段的重叠
- Ubuntu 16.04 下部署Node.js+MySQL微信小程序商城
- C#的抽象类和接口的区别
- HDU 1075-What Are You Talking About(字符串)
- 在无重复数字的循环数组中查找给定数返回其下标
- 多态与强制类型转换
- 【JavaSE学习笔记】IO流04_编码、字符输出流Writer、字符输入流Reader、字符转换输入流、字符转换输出流
- IOC以及Spring注入
- ssm分布式框架电商项目(三)