C. Third-Party Software

C. Third-Party Software
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output

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 functionifirst appeared in versionaiand existed until versionbi, and starting from the versionbi+ 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 integern(1 ≤n≤ 200000) — the number of the functions.

Each of the nextnlines contains two integersaiandbi(1 ≤aibi≤ 109) — the interval of library versions where functioniwas available.

Output

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

In the second line outputkdistinct integers — the numbers of versions need to be purchased.

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

Example
input
Copy
5
2 4
1 3
2 3
3 6
4 5
output
Copy
2
3 4


题意:一个人发明了一个游戏,他想使用所有的功能。我们可以知道的是,每个功能对应一个游戏版本范围,只要在这些版本范围内,就可以使用这个功能。如果不好理解,我们可以把它比作LOL里面的游戏或者王者荣耀,在每个版本范围区间,就可以使用这些英雄。比如样例,5组数据,需要玩5个英雄,下面依次是第一个英雄能够在2 -4版本可以使用,第二个英雄能在1-3版本使用,第三个能在2-6版本使用......现在问想使用所有功能(英雄),问最少需要买几个版本。

题解: 结构体排序     我们可以画一条水平数轴,把版本范围区间画出来,版本有(重叠)交叉范围的,为了少买游戏版本,就缩小区间。最后判断有多少个不想交的区间,表示必买的游戏版本,存入数组里面。

#include<bits/stdc++.h>
using namespace std;
const int N=200000;
int s[N];
struct node
{
    int s,y;
} q[N];
bool cmp(node a,node b)
{
    if(a.s==b.s)
        return a.y<b.y;
    return a.s<b.s;
}
int main()
{
    int a,b,n,minn,maxn,cnt=0;
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>q[i].s>>q[i].y;
    stable_sort(q,q+n,cmp);
    minn=q[0].s,maxn=q[0].y;
    for(int i=1; i<n; i++)
    {
        if(q[i].s>maxn)
        {
            s[cnt++]=minn;
            minn=q[i].s;
            maxn=q[i].y;
        }
       minn=max(minn,q[i].s),maxn=min(maxn,q[i].y);///压缩小集合
    }
    s[cnt++]=minn;
    cout<<cnt<<endl;
    for(int i=0; i<cnt; i++)
        cout<<s[i]<<" ";
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落凡尘.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值