code forces 1029C区间删除求最大区间长度值

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][1;5] and [3;10][3;10] is [3;5][3;5] (length 22), the intersection of segments [1;5][1;5] and [5;7][5;7] is [5;5][5;5](length 00) and the intersection of segments [1;5][1;5] and [6;6][6;6] is an empty set (length 00).

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⋅1052≤n≤3⋅105) — the number of segments in the sequence.

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

Output

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

Examples

input

 
  1. 4

  2. 1 3

  3. 2 6

  4. 0 4

  5. 3 3

output

1

input

 
  1. 5

  2. 2 6

  3. 1 3

  4. 0 4

  5. 1 20

  6. 0 4

output

2

input

 
  1. 3

  2. 4 5

  3. 1 2

  4. 9 20

output

0

input

 
  1. 2

  2. 3 10

  3. 1 5

output

7

Note

In the first example you should remove the segment [3;3][3;3], the intersection will become [2;3][2;3] (length 11). Removing any other segment will result in the intersection [3;3][3;3] (length 00).

In the second example you should remove the segment [1;3][1;3] or segment [2;6][2;6], the intersection will become [2;4][2;4] (length 22) or [1;3][1;3](length 22), respectively. Removing any other segment will result in the intersection [2;3][2;3] (length 11).

In the third example the intersection will become an empty set no matter the segment you remove.

In the fourth example you will get the intersection [3;10][3;10] (length 77) if you remove the segment [1;5][1;5] or the intersection [1;5][1;5] (length 44) if you remove the segment [3;10][3;10].

题意:就是给你n个区间,让你从这n个区间里面删去一个区间让其他区间的公共区间的区间长度最大

手动模拟一下,就是用所有区间的右端点的最小值减去所有区间左端点的最大值(这样是求没有移动的情况),因此你可以将所有左端点的最大值L1和右端点的最小值R1拿出,然后再用去掉之后的左端点的最大值L2与右端点的最小值R2,分别与L1,R1

最差进行比较,即max(R1-L2,R2-L1),但是需要判断一种特殊情况,就是如果当端点的最大值与最小值都是在一个区间里的,那就直接将其去掉,用次最小值与次最大值做差就是所需要的答案,其中所有的答案都要和0,进行对比,如果出现了负数就说明区间长度为0

#include<bits/stdc++.h>
using namespace std;
const int N=3e5+100;
const int INF=0x3f3f3f3f;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<=b;i++)

//bool cmpup(node a,node b)
//{
//    return a.l<b.l;
//}
//bool cmpdown(node a,node b)
//{
//    return a.r<b.r;
//}
struct noder
{
    int val;
    int id;
    bool operator <(const noder&a) const{
        return val > a.val;
    }
};
struct nodel
{
    int val,id;
     bool operator < (const nodel &b)const
    {
        return val<b.val;
    }
};
nodel l;
noder r;
priority_queue<nodel>pl;//注意两个结构体都需要进行重载
priority_queue<noder>pr;
int main()
{
   int n;
   cin>>n;
   rep(i,1,n)
   {
       cin>>l.val;
       cin>>r.val;
       l.id=i;
       r.id=i;
        pl.push(l);
        pr.push(r);
   }
   if(pl.top().id==pr.top().id)
   {
       pr.pop();pl.pop();
       cout<<max(0,pr.top().val-pl.top().val);return 0;
   }
   int ans1=pl.top().val;
   int ans2=pr.top().val;
   pl.pop();pr.pop();
   cout<<max(0,max(pr.top().val-ans1,ans2-pl.top().val));
}

还有一种方法就是用multiset来实现,它的思想就是先将所有的左端点与右端点全部放入multiset中排序,然后分别将所有的点的编号从1--n分别从中拿出,然后做差,再将其放入将第二个点拿出依次进行

#include <cstdio>
#include <algorithm>
#include <set>
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
const int N=300005;
const int INF=0x3f3f3f3f;
pii s[N];
int main(void){
    //freopen("data.txt","r",stdin);
    int n;
    multiset<int> l,r;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d%d",&s[i].fi,&s[i].se);
        l.insert(s[i].fi);
        r.insert(s[i].se);
    }
    int mx=0;
    for(int i=0;i<n;i++){
        l.erase(l.find(s[i].fi));
        r.erase(r.find(s[i].se));
        mx=max(mx,(*r.begin()-*l.rbegin()));
        l.insert(s[i].fi);
        r.insert(s[i].se);
    }
    printf("%d\n",mx);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/c___c18/article/details/82182095