C - Third-Party Software

添加链接描述

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

先给区间按左区间从小到大排序,然后用mi和ma维护当前区间的左区间最大值和右区间最小值,用len记录更换的设备

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define N 200010
struct date
{
   ll x,y;
}a[N];
bool cmp(date l,date r)
{
    if(l.x==r.x)
        return l.y<r.y;
    else
        return l.x<r.x;
}
int main()
{
    int i,n,len;
    ll s[N];
    ll mi,ma;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i].x>>a[i].y;
    }
    sort(a+1,a+1+n,cmp);
    mi=a[1].x;
    ma=a[1].y;
    len=0;
    for(i=2;i<=n;i++)
    {
        if(a[i].x>ma)
        {
            s[++len]=mi;//a[len]=mi或ma都可以,反正都是已输入区间的公共点
            mi=a[i].x;
            ma=a[i].y;
        }
        mi=max(mi,a[i].x);//s[len]必须保证每个区间都存在,所以不断缩小区间范围,找区间重叠点
        ma=min(ma,a[i].y);
    }
    s[++len]=mi;//不要忘记最后一个区间
    cout<<len<<endl;
    for(i=1;i<=len;i++)
    {
        if(i==1)
            cout<<s[i];
        else
            cout<<' '<<s[i];
    }
    cout<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值