51nod 1091 线段重叠的问题

重点是维护当前扫过的最远的(最右)的点,排序之后贪心扫一遍,ans=max(ans,min(p[i].r,last)-p[i].l),last = max(last,p[i].r);

重叠部分就是min(p[i].r,last)-p[i].l

#include<cstdio>
#include<algorithm>
#define maxn 50050
using namespace std;
struct point
{
	int l,r;
	bool operator < (const  point b){
		if(l!=b.l) return l<b.l;
		else return r>b.r;
	}
}p[maxn];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i = 1; i<=n; i++) scanf("%d%d",&p[i].l,&p[i].r);
	sort(p+1,p+n+1);
	int last = p[1].r,ans = 0;
	for(int i = 2; i<=n; i++)  ans=max(ans,min(p[i].r,last)-p[i].l),last = max(last,p[i].r);
	printf("%d\n",ans);
	return 0;
 } 

猜你喜欢

转载自blog.csdn.net/qq_41156122/article/details/81416776