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
原创粉丝点击