HRBUST - 2080 链条-------HPU组队选拔赛(三)

HRBUST - 2080 链条-点击打开链接

链条
Time Limit: 1000 MS Memory Limit: 32768 K
Total Submit: 163(49 users) Total Accepted: 64(44 users) Rating: Special Judge: No
Description

给出n对数字,每个对数字由两个数ab构成,其中a<b,现在用一些对拼成最长的链,要使得 b1 < a2, b2 < a3 ... 求最长链的长度。

Input

有多组测试数据。

每组测试数据的第一行有一个数字N(1<=N<=10^5)

接下来有n对数字aibi,数据保证保证ai<bi0<=ai,bi<=10^9

处理到文件结束。

Output

对于每组测试数据输出一行,为最长长度。

Sample Input
5
2 3
0 9
2 8
1 6
0 1
7
5 8
30 93
66 69
17 32
47 72
68 80
23 49
Sample Output
2
3


/*
	活动安排问题的模板题 
	
*/

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;

const int maxn=1e5+5;

struct node{
	int x,y;
}a[maxn]; 

bool cmp(node a,node b)
{
	return a.y==b.y?a.x<b.x:a.y<b.y;
}


int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		for(int i=0;i<n;i++)	scanf("%d %d",&a[i].x,&a[i].y);
		sort(a,a+n,cmp);
		int ans=1;
		int z=a[0].y;
		for(int i=1;i<n;i++)
		{
			if(a[i].x>z)
			{
				ans++;
				z=a[i].y;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}







猜你喜欢

转载自blog.csdn.net/hpuer_random/article/details/80153175
今日推荐