XYNUOJ暑期集训第二次测试 F - 不重叠的线段

F - 不重叠的线段

X轴上有N条线段,每条线段有1个起点S和终点E。最多能够选出多少条互不重叠的线段。(注:起点或终点重叠,不算重叠)。

例如:151523233636,可以选23233636,这2条线段互不重叠。

Input

第1行:1个数N,线段的数量(2 <= N <= 10000) 
第2 - N + 1行:每行2个数,线段的起点和终点(-10^9 <= S,E <= 10^9)

Output

输出最多可以选择的线段数量。

Sample Input

3
1 5
2 3
3 6

Sample Output

2
/*
这题是找最多不重叠线段,最大区间不相交问题, 尽量保证线段不相交就要按照右端点排序,
左端点怎么排随意,只要保证遍历的时候当前线段的左端点比前面标记的右端点大就满足不重叠的要求。
*/
#include<cstdio>
#include<algorithm>
#define M 100000
using namespace std;

struct time
{
	long long s;
	long long e;
} a[M];


int cmp(time a,time b)
{
	return a.e<b.e;
}

int main()
{
	int n;
	int count=1;
	scanf("%d",&n);
	for(int i=0; i<n; i++)
		scanf("%lld %lld",&a[i].s,&a[i].e);
	sort(a,a+n,cmp);
	long long j=0;
	for(int i=j+1; i<n; i++)
	{
		if(a[i].s >=a[j].e )//尽可能多的不重叠线段
		{
			count++;
			j=i;
		}
	}
	printf("%d\n",count);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/yxy602843889/article/details/81292030