Program D--贪心-区间覆盖
Given several segments of line (int the X axis) with coordinates [Li,Ri]. You are to choose the minimal amount of them, such they would completely cover the segment [0,M].
Input
The first line is the number of test cases, followed by a blank line. Each test case in the input should contains an integer M (1 ≤ M ≤ 5000), followed by pairs
“Li Ri”( |Li|,|Ri|≤ 50000, i ≤ 100000), each on a separate line. Each test case of input is terminated by pair‘ 0 0’. Each test case will be separated by a single line.
Output
For each test case, in the first line of output your programm should print the minimal number of line segments which can cover segment [0,M]. In the
following lines, the coordinates of segments, sorted by their left end (Li), should be printed in the same format as in the input. Pair ‘0 0’ should not be
printed. If [0,M] can not be covered by given line segments, your programm should print ‘0’ (without quotes). Print a blank line between the outputs for two
consecutive test cases.
Sample Input
2
1 -1 0 -5 -3 2 5 0 0
1 -1 0 0 1 0 0
Sample Output
0
1 0 1
题意:给定一个M,和一些区间[Li,Ri],要选出几个区间能完全覆盖住[0,M]区间,要求数量最少,如果不能覆盖输出0.
思路:贪心的思想,把区间按Ri从大到小排序,然后遇到一个满足的[Li,Ri],就更新缩小区间,直到完全覆盖。
注意[Li,Ri]只有满足Li小于等于且Ri大于当前覆盖区间左端这个条件。才能选中。
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
struct node{
int x,y;
}a[100001],b[100001];
int cmp(node a,node b)
{
return a.x<b.x;
}
int main()
{
int t;
int m,n,p,max,sum,i,f,pmax,begin,end;
scanf("%d",&t);
while(t--)
{
scanf("%d",&m);
n=0;
while(scanf("%d%d",&begin,&end))
{
if(begin==0&&end==0)
break;
if(end>=0)
{
a[n].x=begin;
a[n].y=end;
++n;
} }
sort(a,a+n,cmp);
if(a[0].x>0)
printf("0\n");
else
{
p=0;
max=0;
sum=1;
while(p+1<n&&a[p+1].x<=0)
{
++p;
if(a[p].y>a[max].y)
max=p;
}
p=max;
b[1].x=a[p].x;
b[1].y=a[p].y;
while(p<n&&b[sum].y<m)
{
f=1;
pmax=max;
while(p+1<n&&a[p+1].x<=a[max].y)
{
f=0;
++p;
if(a[p].y>a[pmax].y)
pmax=p;
}
if(f)
break;
max=pmax;
++sum;
b[sum].x=a[max].x;
b[sum].y=a[max].y; }
if(b[sum].y>=m)
{
printf("%d\n",sum);
for(i=1;i<=sum;i++)
printf("%d %d\n",b[i].x,b[i].y);
}
else
printf("0\n");
}
if(t)
printf("\n");
}
return 0;
}
Program D--贪心-区间覆盖的更多相关文章
- 高效算法——E - 贪心-- 区间覆盖
E - 贪心-- 区间覆盖 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/E 解题思路: 贪心思想, ...
- 【题解】Cut the Sequence(贪心区间覆盖)
[题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...
- 南阳OJ-12-喷水装置(二)贪心+区间覆盖
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=12 题目大意: 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有 ...
- nyoj 12——喷水装置二——————【贪心-区间覆盖】
喷水装置(二) 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的 ...
- 51nod 1091 线段的重叠【贪心/区间覆盖类】
1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...
- UVA 10382 Watering Grass 贪心+区间覆盖问题
n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...
- B. Heaters 思维题 贪心 区间覆盖
B. Heaters 这个题目虽然只有1500的分数,但是我还是感觉挺思维的,我今天没有写出来,然后看了一下题解 很少做这种区间覆盖的题目,也不是很擅长,接下来讲讲我看完题解后的思路. 题目大意是:给 ...
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li, ...
随机推荐
- linux GO语言配置安装
1.下载地址 https://golang.org/dl/ 2.解压 解压到/usr/local/go目录下 cd /usr/local/go bin/go version 执行如上命令出现go的版本 ...
- c/c++小知识
1.printf计算参数时是从右到左 2.(int&)a 表示把a在内存中的值强行当作int数来处理 3.float四字节,1位符号位(正数为0),8位指数位(0采取01111111),23位 ...
- grep的用法
grep的用法首先创建我们练习grep命令时需要用到的demo文件demo_file. $ cat demo_file THIS LINE IS THE 1ST UPPER CASE LINE IN ...
- hiho_1078_线段树区间修改
题目 给定一组数,要求进行若干次操作,这些操作可以分为两种类型: (1) CMD 1 beg end value 将数组中下标在[beg, end] 区间内数字都变为value (2) CMD 2 b ...
- python语法笔记(二)
1. 循环对象 循环对象是一类特殊的对象,它包含一个next()方法(在python3中是 __next__()方法),该方法的目的是进行到下一个结果,而在结束一系列结果之后,举出 StopItera ...
- 利用ITextSharp导出PDF文件
最近项目中需要到处PDF文件,最后上网搜索了一下,发现ITextSharp比较好用,所以做了一个例子: public string ExportPDF() { //ITextSharp Usage / ...
- 关于json的理解
一. 获取数组长度 array.length 获取对象长度 Object.keys(objs).length 二. 如何抉择{}和[] 先说两者区别, {}:父类下面直接是子类名称 []:父类下会追 ...
- Django开发博客- 页面美化
css是一种用来描述某种标记语言写的web站点的样式语言.这里我们并不想展开讨论,关于CSS我在这里推荐一个很不错的资源: Codeacademy HTML & CSS course 不想从头 ...
- jmeter笔记4
软件测试中使用JMeter测试Web应用 JMeter作用领域 JMeter可以用于测试静态或者动态资源的性能(文件.Servlets.Perl脚本.java对象.数据库和查询.ftp服务器或者其他 ...
- 简单的Hibernate入门简介
其实Hibernate本身是个独立的框架,它不需要任何web server或application server的支持.然而,大多数的Hibernate入门介绍都加入了很多非Hibernate的东西, ...