Minimal coverage 

The Problem

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].

The 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.

The 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

  1. 2
  2.  
  3. 1
  4. -1 0
  5. -5 -3
  6. 2 5
  7. 0 0
  8.  
  9. 1
  10. -1 0
  11. 0 1
  12. 0 0

Sample Output

  1. 0
  2.  
  3. 1
  4. 0 1

题意:给定一个M,和一些区间[Li,Ri]。。要选出几个区间能完全覆盖住[0,M]区间。要求数量最少。。如果不能覆盖输出0.

思路:贪心的思想。。把区间按Ri从大到小排序。 然后遇到一个满足的[Li,Ri],就更新缩小区间。。直到完全覆盖。

注意[Li,Ri]只有满足Li小于等于且Ri大于当前覆盖区间左端这个条件。才能选中。

代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. int t;
  7. int start, end, qn, outn;
  8. struct M {
  9. int start;
  10. int end;
  11. } q[100005], out[100005];
  12.  
  13. int cmp (M a, M b) {//按最大能覆盖到排序
  14. return a.end > b.end;
  15. }
  16. int main() {
  17. scanf("%d", &t);
  18. while (t --) {
  19. qn = 0; outn = 0; start = 0;
  20. scanf("%d", &end);
  21. while (~scanf("%d%d", &q[qn].start, &q[qn].end) && q[qn].start + q[qn].end) {
  22. qn ++;
  23. }
  24. sort(q, q + qn, cmp);
  25. while (start < end) {
  26. int i;
  27. for (i = 0; i < qn; i ++) {
  28. if (q[i].start <= start && q[i].end > start) {
  29. start = q[i].end;//更新区间
  30. out[outn ++] = q[i];
  31. break;
  32. }
  33. }
  34. if (i == qn) break;//如果没有一个满足条件的区间,直接结束。
  35. }
  36. if (start < end) printf("0\n");
  37. else {
  38. printf("%d\n", outn);
  39. for (int i = 0; i < outn; i ++)
  40. printf("%d %d\n", out[i].start, out[i].end);
  41. }
  42. if (t) printf("\n");
  43. }
  44. return 0;
  45. }

UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)的更多相关文章

  1. UVa 10020 - Minimal coverage(区间覆盖并贪心)

    Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the min ...

  2. uva.10020 Minimal coverage(贪心)

    10020 Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose t ...

  3. uva 10020 Minimal coverage 【贪心】+【区间全然覆盖】

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

  4. 【区间覆盖问题】uva 10020 - Minimal coverage

    可以说是区间覆盖问题的例题... Note: 区间包含+排序扫描: 要求覆盖区间[s, t]; 1.把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大):否则选 ...

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

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

  6. uva 10020 Minimal coverage

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

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

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

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

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

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

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

随机推荐

  1. (C#)Windows Shell 外壳编程系列8 - 同后缀名不同图标?

    原文 (C#)Windows Shell 外壳编程系列8 - 同后缀名不同图标? (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:(C#)Windows Shell 外壳 ...

  2. 数据结构之后缀数组suffix array

    在字符串处理当中,后缀树和后缀数组都是非常有力的工具,其中后缀树大家了解得比较多,关于后缀数组则很少见于国内的资料.其实后缀是后缀树的一个非常精巧的替代品,它比后缀树容易编程实现,能够实现后缀树的很多 ...

  3. SharePoint将网站另存为模板

    1.将网站另存为模板 参考文章 http://blog.csdn.net/dyp330/article/details/23180843 http://blog.163.com/berlin1989@ ...

  4. iOS推送证书p12转成pem

    首先你需要导出p12格式的证书,具体操作请参考如下: 其次你就可以通过在控制台输入如下命令即可转换: openssl pkcs12 -in 你导出的p12证书 -out 你要转换的pem证书 -nod ...

  5. javascripts小结

    1 NAN-isNaN():判断是否数值 2 数值转换 Number()-任何数据类型,parseInt(),parseFloat()-字符串 3数组转字符串 var a=["red&quo ...

  6. C++对C语言的非面向对象特性扩充(2)

    上一篇随笔写了关于C++在注释,输入输出,局部变量说明的扩充,以及const修饰符与C中的#define的比较,也得到了几位学习C++朋友们的帮助讲解,十分感谢,我也希望欢迎有更多学习C++的朋友一起 ...

  7. 「OC」block 和 protocol

    一.block   (一)简介 block 是什么?苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,block 可以在任何时候执行.在多线程.异步任务.集合遍历.集合排序.动 ...

  8. 初探响应式Web设计

    公司书柜有本<响应式Web设计:HTML5和CSS3实战>,大概就认真看了前面几章,后面大部分介绍css3(随便找本手册都可以了要你可用!) <响应式Web设计:HTML5和CSS3 ...

  9. (zz)Linux下Gcc生成和使用静态库和动态库详解

    http://blog.chinaunix.net/uid-23592843-id-223539.html

  10. 图的BFS代码

    图是严蔚敏书上P168的图, 图的邻接表存储,DFS可以看以前写的文章:http://www.cnblogs.com/youxin/archive/2012/07/28/2613362.html ]; ...