E - Third-Party Software - 2 Gym - 102215E (贪心)
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 mm functions numbered from 11to mm, and it is known that the ii-th version of the library contains functions from aiai to bibi 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 nn and mm (1≤n≤200000,1≤m≤1091≤n≤200000,1≤m≤109) — the number of library versions and the number of functions.
Each of the next nn lines contains two integers aiai and bibi (1≤ai≤bi≤m1≤ai≤bi≤m) — the interval of function numbers available in the ii-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 kk — the minimal number of library versions needed to be purchased. In the third line output kk distinct integers — the numbers of versions needed to be purchased.
If there are several possible answers, output any of them.
Examples
4 8
1 2
3 4
5 6
7 8
YES
4
1 2 3 4
4 8
1 5
2 7
3 4
6 8
YES
2
1 4
3 8
1 3
4 5
6 7
NO
题解:首先按照左端点从小到大排序,左端点相同按照右端点从大到小排序。然后贪心选取,将设当前能到达的最远点为now,我们需要在左端点<=now+1的线段中选取右端点最大的放进去,之后更新now。
其他不满足的情况特判即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=;
struct node
{
int l,r,id;
}q[maxn];
int cmp(node a,node b)//按左端点从小到大,右端点从大到小排序
{
if(a.l==b.l)return a.r>b.r;
return a.l<b.l;
}
vector<int>ans;
int main()
{
int n,m;
cin>>n>>m;
for(int i=;i<=n;i++){
cin>>q[i].l>>q[i].r;
q[i].id=i;
}
sort(q+,q++n,cmp);
if(q[].l!=)return cout<<"NO"<<endl,;
int now=q[].r;
ans.push_back(q[].id);//首先将第一条放进去
for(int i=;i<=n;){
if(q[i].l>now+)return cout<<"NO"<<endl,;
else{
int temp=now,pos=q[i].id;
while(i<=n&&q[i].l<=now+){//在当前now可连接的取右端点最大值
if(q[i].r>temp){
temp=q[i].r;
pos=q[i].id;
}
i++;
}
if(now==temp)continue;//防止不必要的放入
now=temp;
ans.push_back(pos);
}
}
if(now<m)return cout<<"NO"<<endl,;
cout<<"YES"<<endl;
cout<<ans.size()<<endl;
for(int i=;i<ans.size();i++){
cout<<ans[i]<<" ";
}
cout<<endl;
return ;
}
E - Third-Party Software - 2 Gym - 102215E (贪心)的更多相关文章
- hdu-5695 Gym Class(贪心+拓扑排序)
题目链接: Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5965 Gym Class 贪心+toposort
分析:就是给一些拓补关系,然后求最大分数,所以贪心,大的越靠前越好,小的越靠后越好 剩下的就是toposort,当然由于贪心,所以使用优先队列 #include <iostream> #i ...
- Ice Cream Tower Gym - 101194D (贪心 + 二分 )
题目链接 : https://cn.vjudge.net/problem/Gym-101194D 题目大意 : 给你n个冰激凌球,让你用这些冰激凌球去垒冰激凌,要求是下面的这一个必须是他上面一个的两倍 ...
- Pond Cascade Gym - 101670B 贪心+数学
题目:题目链接 思路:题目让求最下面池子满的时间和所有池子满的时间,首先我们考虑所有池子满的时间,我们从上到下考虑,因为某些池子满了之后溢出只能往下溢水,考虑当前池子如果注满时间最长,那么从第一个池子 ...
- UVaLive 6588 && Gym 100299I (贪心+构造)
题意:给定一个序列,让你经过不超过9的6次方次操作,变成一个有序的,操作只有在一个连续区间,交换前一半和后一半. 析:这是一个构造题,我们可以对第 i 个位置找 i 在哪,假设 i 在pos 位置, ...
- 贪心 Gym 100502E Opening Ceremony
题目传送门 /* 题意:有一堆砖块,每一次操作可以选择消去任意一行,也可以选择消去任意一列.求要消去所有的砖块需要最小的操作数 贪心:首先清楚的是消去最高列的最佳,消去第一行最佳,行列的顺序只对中间过 ...
- Codeforces Gym 100803C Shopping 贪心
Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- codeforces Gym 100187F F - Doomsday 区间覆盖贪心
F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F ...
随机推荐
- uni-app小程序组建
(1)新建组建:编辑器右击 新建组建 (2)传值 <template> <view class="myRankingList"> <block v-f ...
- 8051单片机中访问int中字节的方法
在使用单片机中,unsigned int 占2个字节,unsigned char 占一个字节.而单片机是实行的字节寻址.16字节的bit寻址实在是不好用, 不好用在不能建数组. 在实际的开发过程中,要 ...
- day02-Python运维开发基础
1. Number 数据类型 2. 容器数据类型-字符串 """ 语法: "字符串" % (值1,值2 ... ) 占位符: %d 整型占位符 %f ...
- vue - 子组件向父组件 传递方法和参数
1,子组件 TodoItem.vue : <template> <div class="todo-item" :class="{'is-compl ...
- GoJS API学习
var node = {}; node["key"] = "节点Key"; node["loc"] = "0 0";// ...
- other#nginx配置
#user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log notice; #error ...
- 021、MySQL变量的使用,在MySQL中创建存储过程,并添加变量
#编写一个存储过程 CREATE PROCEDURE ShowDate ( ) BEGIN #变量定义 ); #变量赋值 set m_str1 = '曾经沧海难为水'; #输出当前时间 SELECT ...
- tornado结合前端进行文件上传
在表单中我们获取用户提交的数据,使用的是get_argument,复选框使用的是get_arguments,但是文件的不一样,文件的使用request.files. form文件上传 html代码: ...
- 04 MySQL数据类型
MySQL支持的数据类型 1.数值类型: a.整数类型 TINYINT 1字节 有符号(-128~127); 无符号(0~255) SMALLINT 2字节 有符号(-3276 ...
- M3U8地址在谷歌浏览器中播放
该案例git码云地址:https://gitee.com/kawhileonardfans/hls-player-example 1.下载插件 插件地址:https://files.cnblogs.c ...