HRBUST - 2080 链条-点击打开链接
链条 |
Description |
给出n对数字,每个对数字由两个数a和b构成,其中a<b,现在用一些对拼成最长的链,要使得 b1 < a2, b2 < a3 ... 求最长链的长度。 |
Input |
有多组测试数据。 每组测试数据的第一行有一个数字N(1<=N<=10^5)。 接下来有n对数字ai和bi,数据保证保证ai<bi,0<=ai,bi<=10^9。 处理到文件结束。 |
Output |
对于每组测试数据输出一行,为最长长度。 |
Sample Input |
52 30 92 81 60 175 830 9366 6917 3247 7268 8023 49 |
Sample Output |
23 |
Time Limit: 1000 MS | Memory Limit: 32768 K |
/*
活动安排问题的模板题
*/
#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;
}