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, ...
随机推荐
- spring相关资源
1. 文档中英文 Spring Framework Reference Documentation http://docs.spring.io/spring/docs/4.1.7.RELEASE/sp ...
- 深入理解JVM虚拟机-2垃圾收集器
这里讨论的收集器基于JDK 1.7 Update 14之后的HotSpot虚拟机. 如果两个收集器之间存在连线,说明可以搭配使用.虚拟机所处的区域,则表示它是属于新生代收集器还是年老代收集器.在这里我 ...
- 优之星S900平板电脑root方法
优之星S900root方法: 尝试百度一键root安卓的apk版本root失败. 电脑上下载百度一键root的Windows版本,平板设置→开发者选项→允许USB调试√,保持唤醒状态√,安全设置→允许 ...
- java 内部类3(匿名内部类)
匿名内部类: 1.没有类名的类就叫匿名内部类 2.好处:简化书写. 3.使用前提:必须有继承或实现关系......不要想着你自己没有钱你没可是你爸有 4.一般用于于实参.(重点) class Oute ...
- program testy data
做项目得用数据吧,拿去.... 1.Data.gov搜索 美国政府去年承诺使所有政府数据都能在网上免费获得.这个网站是第一阶段,作为一个门户网站,囊括了从气候到犯罪的一切惊人的信息. 2. 美 ...
- text-overflow:ellipsis实现超出隐藏时省略号显示
text-overflow:ellipsis;要达到的效果是:文字超出容器宽度时,文字被隐藏的文字用省略号代替.所以该属性只能用于块状元素或行内块元素中,对行内元素是不起作用的. 一般和white-s ...
- 第二周 WBS、NABCD查阅
WBS WBS:工作分解结构(Work Breakdown Structure) 创建WBS:创建WBS是把项目可交付成果和项目工作分解成较小的,更易于管理的组成部分的过程. WBS是项目管理重要的专 ...
- [转]ebkit内核浏览器的Linear Gradients (线性渐变)
转自:http://www.css88.com/archives/tag/webkit-gradient webkit内核的safari. Chrome的Linear Gradients (线性渐变) ...
- hdu----(5053)the Sum of Cube(签到题,水体)
the Sum of Cube Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- JQ添加移除css样式
1. addClass() - 添加CSS类 $("#target").addClass("newClass"); //#target 指的是需要添加样式的元素 ...