E. Third-Party Software

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