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

贪心,排序,对左端点贪,找最大右端点。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #define maxn 6000000
  5. using namespace std;
  6.  
  7. int t,m;
  8. int t1;
  9. int ll[maxn],rr[maxn];
  10. struct node
  11. {
  12. int l,r;
  13. int id;
  14. bool operator <(const node &a)const
  15. {
  16. return (l<a.l)||(l==a.l&&r>a.r);
  17. }
  18. } p[maxn],ans[maxn];
  19.  
  20. int main()
  21. {
  22. scanf("%d",&t);
  23. while(t--)
  24. {
  25. t1=;
  26. scanf("%d",&m);
  27. int cnt=;
  28. int l,r;
  29. int num=;
  30. while(scanf("%d%d",&l,&r)!=EOF)
  31. {
  32. if(l==&&r==) break;
  33. if(l>m||r<) continue;
  34. p[cnt].id=num;
  35. p[cnt].l=l;
  36. p[cnt++].r=r;
  37. }
  38. sort(p,p+cnt);
  39. bool flag1=false;
  40. int ll=,rr=;
  41. while()
  42. {
  43. if(ll>=m)
  44. {
  45. break;
  46. }
  47. flag1=false;
  48. rr=;
  49. int pos;
  50. for(int i=; i<cnt; i++)
  51. {
  52. if(p[i].l<=ll&&p[i].r>rr)
  53. {
  54. pos=i;
  55. rr=p[i].r;
  56. flag1=true;
  57. }
  58. }
  59. if(flag1)
  60. {
  61. t1++;
  62. ans[t1]=p[pos];
  63. ll=rr;
  64. }
  65. else break;
  66. }
  67. if(flag1)
  68. {
  69. printf("%d\n",t1);
  70. for(int i=; i<=t1; i++)
  71. {
  72. printf("%d %d\n",ans[i].l,ans[i].r);
  73. }
  74. }
  75. else printf("0\n");
  76. }
  77. return ;
  78. }

uva 10020 Minimal coverage的更多相关文章

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

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

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

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

  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(贪心)

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

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

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

  6. 贪心 URAL 1303 Minimal Coverage

    题目传送门 /* 题意:最少需要多少条线段能覆盖[0, m]的长度 贪心:首先忽略被其他线段完全覆盖的线段,因为选取更长的更优 接着就是从p=0开始,以p点为标志,选取 (node[i].l < ...

  7. UVA-10020 Minimal coverage(贪心)

    题目大意:在x轴上,给一些区间,求出能把[0,m]完全覆盖的最少区间个数及该情形下的各个区间. 题目分析:简单的区间覆盖问题.可以按这样一种策略进行下去:在所有区间起点.长度有序的前提下,对于当前起点 ...

  8. ural 1303 Minimal Coverage【贪心】

    链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 http://acm.hust.edu.cn/vjudge/contest/view ...

  9. UVa 10020 (最小区间覆盖) Minimal coverage

    题意: 数轴上有n个闭区间[ai, bi],选择尽量少的区间覆盖一条指定线段[0, m] 算法: [start, end]为已经覆盖到的区间 这是一道贪心 把各个区间先按照左端点从小到大排序,更新st ...

随机推荐

  1. libvirt API管理hypervisors

    发布一段C代码,用于连接指定的KVM宿主机器,获得该宿主机器的配置信息,以及该主机上所有的虚拟主机列表.状态及配置信息: #include <stdio.h>#include <st ...

  2. D - Mayor's posters - 2528(区间覆盖)

    题意:贴海报 有一面很长的墙,大概有10000000 这么长,现有有一些海报会贴在墙上,当然贴海报的顺序是有先后的,问你当最后一张海报也贴上的时候能不能求出来在这面墙上能看到多少张不同的海报? 分析: ...

  3. Promise in AngularJS

    What's promise Angular’s event system provides a lot of power to our Angular apps. One of the most p ...

  4. Linq to Sql语法及实例大全

    LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的 ,而判断条件就是它后面所接的 ...

  5. [Typescript] Introduction to Generics in Typescript

    If Typescript is the first language in which you've encountered generics, the concept can be quite d ...

  6. rk3288 ov8858 camera移植

    平台:瑞芯的rk3288 SDK:4.4/5.0/5.1 作者:fulinux *****本文同意转载.只是请注明出处:http://blog.csdn.net/fulinus**** rk3288的 ...

  7. IOS后台执行机制 与 动作

    当用户按下"Home"键或者系统启动另外一个应用时,前台foreground应用首先切换到Inactive状态,然后切换到Background状态.此转换将会导致先后调用应用代理的 ...

  8. 纯CSS3实现超立体的3D图片侧翻倾斜效果

    看到网友分享的一款CSS3 3D图片侧翻倾斜特效,觉得效果非常棒,其实话说回来,这玩意儿的实现真的非常简单,主要是创意不错.先来看看效果图.那么接下来我们分析一下源码吧,显示html代码,非常简单: ...

  9. 无限循环的ViewPager

    目前情况 在不修改源码的情况下,当ViewPager滑动到最后一个item的时候,他就无法再往右滑动:当ViewPager滑动到第一个item的时候,他也无法再往前滑动.(以上全是废话) 设想 我们可 ...

  10. Citrix Presentation server can not contact the license server

    If you come across the above error,  you may also come across one or more of the errors below within ...