hrbust 2080链表 【贪心】

链条
Time Limit: 1000 MSMemory Limit: 32768 K
Total Submit: 64(26 users)Total Accepted: 33(26 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<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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值