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 ...
随机推荐
- Ubuntu系统配置Zabbix前端及中文乱码解决方案
Ubuntu系统配置Zabbix前端及中文乱码解决方案 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装zabbix 博主推荐阅读: https://www.cnblogs ...
- 读《Adaptive Thresholding Using the Integral Image》自适应图像阈值
图像的二值化问题总是一个问题.虽然使用深度学习的方法取得了不小的进展,但是传统的方法还是值得借鉴. 刚好随机游走到这篇文章 挖个07年的坟 地址:http://people.scs.carleton ...
- HiBench成长笔记——(11) 分析源码run.sh
#!/bin/bash # Licensed to the Apache Software Foundation (ASF) under one or more # contributor licen ...
- 037、Java中利用判断语句实现三目运算的功能
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- OpenCV HOGDescriptor 参数图解
防止以后再次掉入坑中,决定还是在写写吧 OpenCV中的HOG特征提取功能使用了HOGDescriptor这个类来进行封装,其中也有现成的行人检测的接口.然而,无论是OpenCV官方说明文档还是各个中 ...
- jenkins + gitlab 快速搭建(docker-compose) 时间,时区 同步
记录一下吧 算打一下 tag 最近在整得 swarm + jenkins 实现自动化部署 回滚 #构建jenkins 镜像 #dockerfile: docker build -t ...
- netty权威指南学习笔记二——netty入门应用
经过了前面的NIO基础知识准备,我们已经对NIO有了较大了解,现在就进入netty的实际应用中来看看吧.重点体会整个过程. 按照权威指南写程序的过程中,发现一些问题:当我们在定义handler继承Ch ...
- OI生涯回顾
OI回忆录只是一个预告,估计等2020高考结束才放出来吧. 先写一下自己简单的OI历程吧: 小升初刚起步 初一 第一次比赛,NOIP PJ组215分,踩线1=,全省rk86,全国rk677(毕竟AH ...
- crmv2项目
maven -----------------------------------------------------------------------------感谢打赏!
- 获得spring
这里 手动下载 各版本的发行包 这里是 官方项目地址 这里是在 GitHub上托管源代码 的地方 已知spring依赖的其他jar commons-logging-1[1].0.4.jar