51nod 1091 线段的重叠

左端点排序,扫一遍,维护右端点的最大值;因为左端点有序,故只需要当前左端点小于最大值即可保证相交。答案即每个相交的长度最大值。

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<cstring>
#include<stack>
#include <cmath>

#define mem(ss) memset(ss,0,sizeof(ss))
#define rep(d, s, t) for(int d=s;d<=t;d++)
#define rev(d, s, t) for(int d=s;d>=t;d--)
#define _max(a, b) ((a) > (b) ? (a) : (b))
#define _min(a, b) ((a) < (b) ? (a) : (b))
#define inf 0x3f3f3f3f
#define eps 1e-6
typedef std::pair<int, int> pii;
typedef std::pair<long long, long long> pll;
typedef long long ll;
typedef long double ld;
const double pi = acos(-1);
const int mod = 1e9 + 7;
const int N = 5e4 + 10;

ll gcd(ll p, ll q) { return q == 0 ? p : gcd(q, p % q); }

using namespace std;
pii a[N << 1];
int n;

int main() {
    cin >> n;
    rep(i, 0, n - 1)cin >> a[i].first >> a[i].second;
    sort(a, a + n);
    int ans = 0, maxr = a[0].second;
    rep(i, 1, n - 1) {
        if (a[i].first < maxr)
            ans = max(ans, min(a[i].second, maxr) - a[i].first);
        maxr = max(maxr, a[i].second);
    }
    cout << ans;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值