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--贪心-区间覆盖的更多相关文章

  1. 高效算法——E - 贪心-- 区间覆盖

    E - 贪心-- 区间覆盖 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/E 解题思路: 贪心思想, ...

  2. 【题解】Cut the Sequence(贪心区间覆盖)

    [题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...

  3. UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】

    UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...

  4. 南阳OJ-12-喷水装置(二)贪心+区间覆盖

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=12 题目大意: 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有 ...

  5. nyoj 12——喷水装置二——————【贪心-区间覆盖】

    喷水装置(二) 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的 ...

  6. 51nod 1091 线段的重叠【贪心/区间覆盖类】

    1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...

  7. UVA 10382 Watering Grass 贪心+区间覆盖问题

    n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...

  8. B. Heaters 思维题 贪心 区间覆盖

    B. Heaters 这个题目虽然只有1500的分数,但是我还是感觉挺思维的,我今天没有写出来,然后看了一下题解 很少做这种区间覆盖的题目,也不是很擅长,接下来讲讲我看完题解后的思路. 题目大意是:给 ...

  9. UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)

     Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinates [Li, ...

随机推荐

  1. [css] px em rem

    一.区别 px是相对于显示器屏幕分辨率而言的. em相对于浏览器的默认字体尺寸. rem相对于HTML根元素. 二.使用  1.em 任意浏览器的默认字体高都是16px.所有未经调整的浏览器都符合: ...

  2. control file sequential read 等待事件

    可能的原因 control file sequential read Reading from the control file. This happens in many cases. For ex ...

  3. Python核心编程-描述符

    python中,什么描述符.描述符就是实现了"__get__"."__set__"或"__delete__" 方法中至少一个的对象.什么是非 ...

  4. git tag推送小分析

    一个推送可以用三条命令 -[deleted]-git push origin --tags git push origin master --follow-tags git push --follow ...

  5. Lua a and b or c

    lua中nil和false为条件不成立,其余都为条件成立. a and b : a条件不成立,则返回a,否则,返回b a or c   : a条件成立,则返回a,否则,返回b 常用x = x or v ...

  6. this 指向

    this-->当前行为发生的主体就是this,但是this是谁和这个方法在哪执行的或者在哪定义的都没有半毛钱的关系 如何的区分JS中的this? 1)函数执行,看函数名前面是否有".& ...

  7. Hibernate-细细道来-01

    Dao代码,如何编写? 使用Jdbc技术,原始的jdbc操作, Connection/Statement/ResultSet DbUtils组件, 轻量级的dao的组件: Hibernate技术  [ ...

  8. JavaWeb基础:Servlet

    Servlet 基本概念 Servlet是Sun公司提出的一种用于开发动态Web资源的技术规范: Servlet是一个Java接口, 用户想要开发自定义的Servlet可以通过以下步骤: 编写实现Se ...

  9. hdu 1829 A Bug's Life(分组并查集(偏移量))

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  10. hdu 1695 GCD(莫比乌斯反演)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...