链条 | ||||||
| ||||||
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 | ||||||
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<bits/stdc++.h>
using namespace std;
struct node
{
int x, y;
} a[100004];
bool cmp(node a, node b)
{
return a.y < b.y;
}
int main()
{
int n;
while(cin >> n)
{
for(int i = 0; i < n; i++)
cin >> a[i].x >> a[i].y;
sort(a, a + n, cmp);
int ans = 1;
int nex = a[0].y;
for(int i = 1; i < n; i++)
{
if(a[i].x > nex)
{
ans++;
nex = a[i].y;
}
}
cout << ans << endl;
}
return 0;
}