51nod 1091 線段的重疊

貪心,記錄前面末尾座標最長的點
#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

const int MAX = 50005;

struct Node{
    int a;
    int b;
}p[MAX];
int cmp(Node p1,Node p2)
{
    if(p1.a<p2.a)return 1;
    else if(p1.a==p2.a&&p1.b>p2.b)return 1;
        return 0;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i = 0;i < n ;i++){
            scanf("%d %d",&p[i].a,&p[i].b);
        }
       sort(p,p+n,cmp);
       int res = 0;
       Node s = p[0];
       for(int i = 1; i < n; i++)
       {
            if(p[i].b<=s.b){
                res=max(res,p[i].b-p[i].a);
            }
            else if(p[i].a<=s.b&&p[i].b>s.b){
                res=max(res,s.b-p[i].a);
                s=p[i];
            }
       }
       printf("%d\n",res);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章