区间重叠问题 Size Balanced Tree Gym - 101755C

Pavel is developing a game. To do that, he needs functions available in a third-party library too famous to be called. It is known that the function i first appeared in version ai and existed until version bi, and starting from the version bi + 1, it is absent in this library.

The library is not free and Pavel needs all the functions. Which minimal number of versions he need to purchase to be able to use all the functions?

Input

The first line contains a single integer n (1 ≤ n ≤ 200000) — the number of the functions.

Each of the next n lines contains two integers ai and bi (1 ≤ ai ≤ bi ≤ 109) — the interval of library versions where function i was available.

Output

In the first line output a single integer k — the minimal number of library versions need to be purchased to unlock all functions.

In the second line output k distinct integers — the numbers of versions need to be purchased.

If there are several possible answers, output any of them.

Example

Input

5
2 4
1 3
2 3
3 6
4 5

Output

2
3 4

题意  

给你n个区间 问最少选几个数 使这些数存在所以区间

思路

贪心 对区间排序后 选取第一个数存在最左边区间的最大值,如果之后区间不符合 则选取不符合区间最大值

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
struct node
{
    int l,r;
}a[200100];
bool cmp(node a,node b)
{
    if(a.r<b.r) return 1;
    else if(a.r==b.r&&a.l<b.l) return 1;
    else return 0;
}
int main()
{
    int n,i,pre=-1;
    queue<int> q;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d%d",&a[i].l,&a[i].r);
    }
    sort(a,a+n,cmp);
    for(i=0;i<n;i++)
    {
        if(pre<a[i].l)
        {
            pre = a[i].r;
            q.push(pre);
        }
    }
    printf("%d\n",q.size());
    while(!q.empty())
    {
        printf("%d",q.front());
        q.pop();
        if(!q.empty())
        {
            printf(" ");
        }
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值