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

解题思路:

1、把各区间按照x从小到大排序,如果区间1的起点大于0,则无解(因为其他区间的左起点更大);否则选择起点在s的最长区间;

2、选择区间[li, ri]后,新的起点应更新为ri,并且忽略所有区间在ri之前的部分;

程序代码:

#include <cstdio>
#include <algorithm>
using namespace std;
struct node
{
int x,y;
};
node a[];
int b[];
int cmp( node a, node b)
{
return a.x<b.x;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int m,i=;
scanf("%d",&m);
while(scanf("%d%d",&a[i].x,&a[i].y)==&&(a[i].x||a[i].y))
{
i++;
}
sort(a,a+i,cmp);
int k=,x=,y=;
int j;
while(x<m)
{
y=x;
for( j=;j<i;j++)
if(a[j].x<=x&&a[j].y>=y)
{y=a[j].y;b[k]=j;}
if(x==y)
{
k=;break;
}
x=y;
k++;
}
printf("%d\n",k);
for( j=;j<k;j++)
printf("%d %d\n",a[b[j]].x,a[b[j]].y);
}
return ;
}

高效算法——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. 51nod 1091 线段的重叠【贪心/区间覆盖类】

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

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

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

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

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

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

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

  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. 利用DIV,实现简单的网页布局

    <html lang="en"><head> <meta charset="UTF-8"> <title>GIS ...

  2. PreferenceFragment界面透明问题

    PreferenceFragment界面默认是透明的 而其布局代码框架为 <PreferenceScreen> ... </PreferenceScreen>,背景色及透明度属 ...

  3. IsPostBack and DropdownList.

    Encounted the issue accident when helping my classmate dealing with his homework assignment,it turns ...

  4. SQL Server Management Studio的对象资源管理器的使用

    1.查看 2.对象资源管理器 3.点到某个表的身上 4.出现以下图片,因为有时动态创建的触发器,刷新表下面的触发器可能不出来,所以来这里面找

  5. asp.net尽量不在js里写<%%>

    asp.net尽量不在js里写<%%> eg: <script type="text/javascript"> var rootsid="&quo ...

  6. 简单的html5 File base64 图片上传

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Magento 编译 php5.6.21 命令

    ./configure  '--prefix=/alidata/server/php' '--enable-opcache' '--with-config-file-path=/alidata/ser ...

  8. PHP对象类型在内存中的分配

    对象类型和整型.字符串等类型一样,也是PHP中的一种数据类型.都是在程序中用于存储不同类型数据使用的,在程序运行时它的每部分内容都要先加载到内存中再被使用.那么对象类型的数据在内存中是如何分配的呢?先 ...

  9. 百度编辑器ueditor如何配置

    完全版: 1.加载 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" ...

  10. Linux删除用户

    删除用户 # userdel abc 该删除操作将用户删除但保留用户的home文件夹和邮件文件夹.并且当用户abc正在登录的时候,删除操作将失败,如下: # userdel abc userdel: ...