51nod 1091 线段的重叠
来源:互联网 发布:用什么相机拍淘宝衣服 编辑:程序博客网 时间:2024/06/06 13:35
Input
第1行:线段的数量N(2 <= N <= 50000)。第2 - N + 1行:每行2个数,线段的起点和终点。(0 <= s , e <= 10^9)
Output
输出最长重复区间的长度。
Input示例
51 52 42 83 77 9
Output示例
4
这题先按起点从小到大排序,然后在遍历,遍历的过程中更新最大的覆盖值。时间复杂度为O(n^2),但有n*longn的做法。。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
int x,y;
};
bool cmp(node a,node b)
{
if(a.x!=b.x)
return a.x<b.x;
return a.y<b.y;
}
int main()
{
int n;
node point[50005];
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%d%d",&point[i].x,&point[i].y);
}
sort(point,point+n,cmp);
int max=-100000;
int cnt=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(point[i].y-point[i].x<max)
break;//当线段长度小于已知的最大覆盖值直接跳出来,没有这句则程序会超时= =
if(point[i].y<point[j].x)
continue;
else if(point[i].x<point[j].y&&point[i].y>point[j].y)
{
cnt=point[j].y-point[j].x;
}
else
cnt=point[i].y-point[j].x;
if(cnt>max)
max=cnt;
}
}
printf("%d\n",max);
}
return 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 线段的重叠
- Windows注册表详解
- swift2 类和结构体
- ios开发遇到的问题
- Java基础
- 1014. Waiting in Line (30)
- 51nod 1091 线段的重叠
- Android开发相关的Blog推荐——跟随大神的脚步才能成长为大神
- POJ 3460Booksort
- SQL连接
- ubuntu_maven
- 【CSS】——基础认识
- JAVA_SE基础——15.循环嵌套
- java中初始化对象变量的方法
- java中String s="abc"及String s=new String("abc")详解