Codeforces | CF1029C 【Maximal Intersection】

论Div3出这样巨水的送分题竟然还没多少人AC(虽说当时我也没A...其实我A了D...逃)
这个题其实一点都不麻烦,排序都可以免掉(如果用\(priority \_ queue\)的话)
先考虑不删除区间的情况,那么答案应该由所有区间中左端点坐标最大的区间和右端点坐标最小的区间决定(手动模拟一下加入新的区间造成的更严格的约束即得),所以想在删掉一个区间后使得剩余的区间的交尽可能大只需要考虑删去左端点最大的区间和右端点最小的区间即可
一定有人会问如果左端点最大且坐标相同的区间不是一个的时候删哪一个,其实完全不需要考虑(如果有不止一个那你删哪个都不能放宽左端点约束),右端点同理,所以我们可以直接把所有的左端点和右端点分别放入优先队列,最后讨论删除区间的情况取\(max\)即可
如果左端点最大的区间和右端点最小的区间是同一个区间,那就铁定删掉这个区间就对了(谁让它是最严格的约束呢\(qwq\))
下面按照惯例放AC代码\(\downarrow\downarrow\downarrow\)

#include<cstdio>//CF1029C
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>

using namespace std;

int n,ansl,ansr;

struct lborder{
	int x,id;
	bool operator<(const lborder&rhs)const{
		return x<rhs.x;
	}
};

struct rborder{
	int x,id;
	bool operator<(const rborder&rhs)const{
		return x>rhs.x;
	}
};

lborder lb;rborder rb;

priority_queue<lborder>ql;priority_queue<rborder>qr;

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&lb.x,&rb.x);
		lb.id=i;
		rb.id=i;
		ql.push(lb);
		qr.push(rb);
	}
	if(ql.top().id==qr.top().id){
		ql.pop();
		qr.pop();
		printf("%d\n",max(0,qr.top().x-ql.top().x));
		return 0;
	}
	ansl=ql.top().x;
	ansr=qr.top().x;
	ql.pop();
	qr.pop();
	int ans=max(ansr-ql.top().x,qr.top().x-ansl);
	printf("%d\n",max(ans,0));
	return 0;
} 
posted @ 2018-08-28 11:22  __BLUESKY007  阅读(293)  评论(0编辑  收藏  举报