Maximal Intersection Codeforces Round #506 Div. 3 贪心+暴力

博客围绕在数轴上给定n个区间,要求删除一个区间使剩余(n - 1)个区间交集长度最大的问题展开。介绍了区间交集的概念和计算方法,输入输出格式,还提到最初理解题意有误,解决思路是暴力枚举删除的区间并求剩余区间交集。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You are given nn segments on a number line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.

The intersection of a sequence of segments is such a maximal set of points (not necesserily having integer coordinates) that each point lies within every segment from the sequence. If the resulting set isn't empty, then it always forms some continuous segment. The length of the intersection is the length of the resulting segment or 00 in case the intersection is an empty set.

For example, the intersection of segments [1;5] and [3;10] is [3;5] (length 2), the intersection of segments [1;5] and [5;7]is [5;5](length 0) and the intersection of segments [1;5] and [6;6] is an empty set (length 0).

Your task is to remove exactly one segment from the given sequence in such a way that the intersection of the remaining (n−1)(n−1)segments has the maximal possible length.

Input

The first line contains a single integer nn (2≤n≤3⋅10^5) — the number of segments in the sequence.

Each of the next nn lines contains two integers lili and riri (0≤li≤ri≤10^9) — the description of the ii-th segment.

Output

Print a single integer — the maximal possible length of the intersection of (n−1) remaining segments after you remove exactly one segment from the sequence

把题意弄错了,想了好久……

题意删除一段区间,使得剩下的区间都相交的部分最大;

假设现在不删区间,所有区间都相交的部分长度一定是 R_{min}-L_{max} ;  L,R分别表示区间左右端点;

现在要删去一个,直接暴力枚举删哪个区间即可,剩下的区间求R_{min}-L_{max}

#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long LL;
const int MOD=1e9+7;
const int maxn=1e5+7;
multiset<int> l,r;
vector<pair<int,int> > v;
int main()
{
    int n;
    scanf("%d",&n);
    int L,R;
    for(int i=0;i<n;++i)
    {
        scanf("%d%d",&L,&R);
        v.push_back(make_pair(L,R));
        l.insert(L);
        r.insert(R);
    }
    int ans=0;
    for(int i=0;i<n;++i)
    {
        l.erase(l.find(v[i].first));
        r.erase(r.find(v[i].second));
        ans=max(ans,*r.begin()-*l.rbegin());
        l.insert(v[i].first);
        r.insert(v[i].second);
    }
    cout<<ans<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值