memory_qianxiao的博客

心之所向,素履以往!

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;
}


阅读更多
版权声明:大家一起学习,欢迎转载,转载请注明出处。若有问题,欢迎纠正! https://blog.csdn.net/memory_qianxiao/article/details/79966535
个人分类: codeforce
想对作者说点什么? 我来说一句

Flash - Third-Party

2008年12月08日 1.77MB 下载

没有更多推荐了,返回首页

不良信息举报

C. Third-Party Software

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭