51 nod 線段的重疊

1091
#include<bits/stdc++.h>
using namespace std;
#define M 55000
struct line{
    int hend,tail;
}num[M];

int cmp(line a,line b)
{
   return a.hend==b.hend?a.tail>b.tail:a.hend<b.hend;
}

int main()
{
    int ans=0;
    int maxn;
    int t;
    cin>>t;
    for(int i=0; i<t; i++){
        cin>>num[i].hend>>num[i].tail;
    }
    sort(num,num+t,cmp);
    maxn=num[0].tail;
    for(int i=1; i<t; i++){
        ans = max(ans,min(maxn,num[i].tail)-num[i].hend);
        maxn=max(maxn,num[i].tail);
    }
    cout<<ans;
    return 0;
}

基準時間限制: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
輸出最長重複區間的長度。
Input示例
5
1 5
2 4
2 8
3 7
7 9
Output示例
4
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章