E. Third-Party Software - 2 贪心----最小区间覆盖
E. Third-Party Software - 2
2.0 s
256 MB
standard input
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?
The first line contains two integers n
and m (1≤n≤200000,1≤m≤109
) — the number of library versions and the number of functions.
Each of the next n
lines contains two integers ai and bi (1≤ai≤bi≤m) — the interval of function numbers available in the i
-th version.
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.
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 题意:翻译一段资料需要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[];
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=;
cin>>n>>m;
for(int i=;i<=n;i++)
{
cin>>p[i].l>>p[i].r;
p[i].id=i;
}
sort(p+,p++n,cmp);
if(p[].l!=)
cout<<"NO"<<endl; else
{
q.push_back(p[].id);
int now=p[].r;
for(int i=;i<=n;)
{
if(p[i].l>now+)
{
cout<<"NO"<<endl;
flag=;
break;
}
else
{
int temp=now,pos=p[i].id;
while(i<=n&&p[i].l<=now+)
{
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==)
cout<<"NO"<<endl;
else if(flag==)
{
cout<<"YES"<<endl;
cout<<q.size()<<endl;
for(int i=;i<q.size();i++)
cout<<q[i]<<' ';
cout<<endl;
} }
return ;
}
E. Third-Party Software - 2 贪心----最小区间覆盖的更多相关文章
- UVa 10020 (最小区间覆盖) Minimal coverage
题意: 数轴上有n个闭区间[ai, bi],选择尽量少的区间覆盖一条指定线段[0, m] 算法: [start, end]为已经覆盖到的区间 这是一道贪心 把各个区间先按照左端点从小到大排序,更新st ...
- 贪心算法----区间覆盖问题(POJ2376)
题目: 题目的大概意思是约翰这个农民有N条牛,这些牛可以在一天中的某个时间段可以进行工作,他想把这个时间段分成若干个片段让这些牛去进行打扫任务,你的任务是安排尽量少的牛然后可以完成分成这些片段的打扫任 ...
- POJ 2376 Cleaning Shifts (贪心,区间覆盖)
题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小 ...
- UVA10020:Minimal coverage(最小区间覆盖)
题目: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68990#problem/M 题目需求:数轴上有n个闭区间[ai,bi],选择尽量 ...
- UVA10020(最小区间覆盖)
题意: 给你一个区间[0,m]和一些小的区间[l,r]让你选择最少的小区间个数去把整个区间覆盖起来. 思路: 算是比较经典的贪心题目吧(经典于难度没什么对应关系),大体思路可以 ...
- NYOJ 12:喷水装置(二)(贪心,区间覆盖问题)
12-喷水装置(二) 内存限制:64MB 时间限制:3000ms 特判: No 通过数:28 提交数:109 难度:4 题目描述: 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n ...
- POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)
Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...
- 高效算法——D 贪心,区间覆盖问题
Given several segments of line (int the X axis) with coordinates [Li , Ri ]. You are to choose the m ...
- poj 2376 Cleaning Shifts 最小区间覆盖
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40751 Accepted: 9871 ...
随机推荐
- session存取时 getOutputStream()和getWriter()问题
情况1: 在使用httpResponse的getWriter()会写json是出现 getWriter() has already been called for this response,经我查看 ...
- python deepcopy的替代方案
from copy import deepcopy import marshal import timeit from multidict import CIMultiDict def test_de ...
- Windows驱动开发-IRP结构体
IRP的全名是I/O Request Package,即输入输出请求包,它是Windows内核中的一种非常重要的数据结构. 上层应用程序与底层驱动程序通信时,应用程序会发出I/O请求,操作系统将相应的 ...
- Python2 和 Python3 编码问题
基本存储单元 位(bit, b):二进制数中的一个数位,可以是0或者1,是计算机中数据的最小单位. 字节(Byte,B):计算机中数据的基本单位,每8位组成一个字节. 1B = 8b 各种信息在计算机 ...
- MQTT 协议学习: 总结 与 各种定义的速查表
背景 经过几天的学习与实操,对于MQTT(主要针对 v3.1.1版本)的学习告一段落,为了方便日后的查阅 本文链接:<MQTT 协议学习: 总结 与 各种定义的速查表> 章节整理 MQTT ...
- PE文件中找导出表
导出表: typedef struct _IMAGE_EXPORT_DIRECTORY { DWORD Characteristics; // 未使用,总为0 DWORD TimeDateStamp; ...
- 安卓:从assets目录下复制文件到指定目录
有些时候我们直接将某些资源文件内置到apk中,便于直接使用. 1.首先将文件放置在项目/app/src/main/assets目录中 2.功能代码: public void copyFile(Stri ...
- 2019 OI日记
// 我觉得记日记是个好习惯吧 毕竟指不定哪天就学不下去了 就AFO了 就没有梦了 // [置顶]活跃于你谷普及训练场.ybt(没底气说全部).loj(提高基础部分) //优先级从前往后 因为 ...
- 记一次Redis+Getshell经验分享
前言: 当我们接到一个授权渗透测试的时候,常规漏洞如注入.文件上传等尝试无果后,扫描端口可能会发现意外收获. 知己知彼乃百战不殆,Redis介绍: 简单来说 redis 就是一个Key-Value类型 ...
- loadBeanDefinitions方法源码跟踪(二)
因为字数超过了限制,所以分成了三篇,承接上篇: https://www.jianshu.com/p/a0cfaedf3fc5 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 3.parseDef ...