E. Third-Party Software - 2 贪心

E. Third-Party Software - 2

time limit per test

2.0 s

memory limit per test

256 MB

input

standard input

output

standard output

Pavel is developing another game. To do that, he again needs functions available in a third-party library too famous to be called. There are m

functions numbered from 1 to m, and it is known that the i-th version of the library contains functions from ai to bi

inclusively.

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

Input

The first line contains two integers n

and m (1n200000,1m109

) — the number of library versions and the number of functions.

Each of the next n

lines contains two integers ai and bi (1aibim) — the interval of function numbers available in the i

-th version.

Output

In the first line output «YES» or «NO», depending on if it's possible or not to purchase library versions to use all the functions.

In case of the positive answer output two more lines. In the second line output a single integer k

 — the minimal number of library versions needed to be purchased. In the third line output k

distinct integers — the numbers of versions needed to be purchased.

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

Examples
Input
Copy
4 8
1 2
3 4
5 6
7 8
Output
Copy
YES
4
1 2 3 4 
Input
Copy
4 8
1 5
2 7
3 4
6 8
Output
Copy
YES
2
1 4 
Input
Copy
3 8
1 3
4 5
6 7
Output
Copy
NO

题意:翻译一段资料需要m个信息,信息分布在n本字典上,问这n本字典能否提供m个信息去翻译资料,若能得话,最少需要基本字典,并输出字典的序号

题解:
题解:首先按照左端点从小到大排序,左端点相同按照右端点从大到小排序。然后贪心选取,将设当前能到达的最远点为now,我们需要在左端点<=now+1的线段中选取右端点最大的放进去,之后更新now。
其他不满足的情况特判即可。

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>q;
struct node
{
    int l;
    int r;
    int id;
}p[200005];
bool cmp(node a,node b)
{
    if(a.l!=b.l)
        return a.l<b.l;
    else
        return a.r>b.r;
}
int main()
{
    int n,m,flag=0;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>p[i].l>>p[i].r;
        p[i].id=i;
    }
    sort(p+1,p+1+n,cmp);
    if(p[1].l!=1)
        cout<<"NO"<<endl;
    
    else
    {
        q.push_back(p[1].id);
        int now=p[1].r;
        for(int i=2;i<=n;)
        {
            if(p[i].l>now+1)
            {
                cout<<"NO"<<endl;
                flag=1;
                break;
            }
            else
            {
                int temp=now,pos=p[i].id;
                while(i<=n&&p[i].l<=now+1)
                {
                    if(p[i].r>temp)
                    {
                        temp=p[i].r;
                        pos=p[i].id;
                    }
                    i++;
                }
                if(now==temp)
                    continue;
                now=temp;
                q.push_back(pos);  
            } 
        }
        if(now<m&&flag==0)
            cout<<"NO"<<endl;
        else if(flag==0)
        {
            cout<<"YES"<<endl;
            cout<<q.size()<<endl;
            for(int i=0;i<q.size();i++)
                cout<<q[i]<<' ';
            cout<<endl;
        }
        
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/-citywall123/p/11182269.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值