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 | ||||||
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;
}
本文解析了一道经典的活动安排问题模板题,通过给出的样例输入和输出,介绍了如何利用C++编程语言实现该问题的解决方案。通过对一系列数字对进行特定排序并应用贪心策略来寻找最长的活动链。

1155

被折叠的 条评论
为什么被折叠?



