51 Nod 线段最长重叠部分
基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
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
输出最长重复区间的长度
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
struct line
{
int l;
int r;
} L[50005];
bool operator<(line l1,line l2)
{
if(l1.r==l2.r)return l1.l<l2.l;
else return l1.r<l2.r;
}
int id[50005];//按照起点排序的线段序号
bool used[50005];
struct cmp1
{
bool operator()(int i1,int i2)
{
if(L[i1].l!=L[i2].l)
return L[i1].l<L[i2].l;
else return L[i1].r<L[i2].r;
}
};
int main()
{
//freopen("in.txt","r",stdin);
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>L[i].l>>L[i].r;
}
sort(L,L+n);
for(int i=0;i<n;i++)id[i]=i;
sort(id,id+n,cmp1());
int ans=0;
int pos=0;
ans=0;
for(int i=0;i<n;i++)
{
while((used[id[pos]]&&pos<n)||id[pos]==i)pos++;
if(pos>=n)break;
if(id[pos]!=i)
{
int beg=max(L[id[pos]].l,L[i].l);
int END=min(L[id[pos]].r,L[i].r);
ans=max(ans,END-beg);
}
used[i]=1;
}
cout<<ans<<endl;
return 0;
}
51 Nod 线段最长重叠部分的更多相关文章
- 51 nod 1055 最长等差数列(dp)
1055 最长等差数列 基准时间限制:2 秒 空间限制:262144 KB 分值: 80 难度:5级算法题 N个不同的正整数,找出由这些数组成的最长的等差数列. 例如:1 3 5 6 8 9 ...
- 51 Nod 1035 最长的循环节 (此题还不是很懂,日后再看)
转自: https://blog.csdn.net/define_danmu_primer/article/details/52456763 51nod 1035 最长的循环节(无限小数的循环节) 2 ...
- 51 Nod 1089 最长回文子串(Manacher算法)
1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 回文串是指aba.abba.cccbccc.aaa ...
- 51 Nod 1134 最长递增子序列(经典问题回顾)
1134 最长递增子序列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元 ...
- 51 Nod 1134 最长递增子序列 (动态规划基础)
原题链接:1134 最长递增子序列 题目分析:长度为 的数列 有多达 个子序列,但我们应用动态规划法仍可以很高效地求出最长递增子序列().这里介绍两种方法. 先考虑用下列变量设计动态规划的算法. ...
- 51 Nod 1006 最长公共子序列(LCS & DP)
原题链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1006 题目分析: 首先先知道LCS问题,这有两种: Long ...
- 51 nod 1006 最长公共子序列Lcs
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1006 参考博客 :http://blog.csdn.net/yysdsy ...
- 51 nod 1766 树上的最远点对(线段树+lca)
1766 树上的最远点对 基准时间限制:3 秒 空间限制:524288 KB 分值: 80 难度:5级算法题 n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个 ...
- 51 nod 1394 1394 差和问题(线段树)
1394 差和问题基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 有一个多重集合S(即里面元素可以有重复),初始状态下有n个元素,对他进行如下操作: 1.向S里面添 ...
随机推荐
- Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- 关于前端JS判断字符串是否包含另外一个字符串的方法总结
RegExp 对象方法 test() var str = "abcd"; var reg = RegExp(/d/); console.log(reg.test(str)); // ...
- STL vector常见用法详解
<算法笔记>中摘取 vector常见用法详解 1. vector的定义 vector<typename> name; //typename可以是任何基本类型,例如int, do ...
- C++练习 | 最长公共字符串(DP)
HDU 1159.Common Subsequence #include<iostream> #include<stdio.h> #include<string> ...
- Zabbix 系统概述与部署
Zabbix是一个非常强大的监控系统,是企业级的软件,来监控IT基础设施的可用性和性能.它是一个能够快速搭建起来的开源的监控系统,Zabbix能监视各种网络参数,保证服务器系统的安全运营,并提供灵活的 ...
- python基本数据类型零碎知识点
...
- Spring IOC的简单实现
简单的说,Spring就是通过工厂+反射将我们的bean放到它的容器中的,当我们想用某个bean的时候,只需要调用getBean("beanID")方法即可. 原理简单说明: Sp ...
- java接口自动化测试小dome
GitHub地址:https://github.com/leonInShanghai/InterfaceAutomation 这个dome 请求 https://www.v2ex.com/api/no ...
- openstack云主机冷迁移
1:开启nova计算节点之间互信 冷迁移需要nova计算节点之间使用nova用户互相免密码访问 默认nova用户禁止登陆,开启所有计算节点的nova用户登录shell. usermod -s /bin ...
- Mongodb操作-更新操作符
1.$inc 用法:{$inc:{field:value}} 作用:对一个数字字段的某个field增加value 示例:将name为chenzhou的学生的age增加5 > db.student ...