D. The Union of k-Segments
time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given n segments on the coordinate axis Ox and the number k. The point is satisfied if it belongs to at least k segments. Find the smallest (by the number of segments) set of segments on the coordinate axis Ox which contains all satisfied points and no others.

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 106) — the number of segments and the value of k.

The next n lines contain two integers li, ri ( - 109 ≤ li ≤ ri ≤ 109) each — the endpoints of the i-th segment. The segments can degenerate and intersect each other. The segments are given in arbitrary order.

Output

First line contains integer m — the smallest number of segments.

Next m lines contain two integers aj, bj (aj ≤ bj) — the ends of j-th segment in the answer. The segments should be listed in the order from left to right.

Sample test(s)
input
  1. 3 2
    0 5
    -3 2
    3 8
output
  1. 2
    0 2
    3 5
input
  1. 3 2
    0 5
    -3 3
    3 8
output
  1. 1
    0 5

题意:问这些线段覆盖的不小于k次的线段有哪些,注意有可能是一个点;

思路:把起点和终点标记后排序,用一个计数器当等于k事说明符合条件线段的起点或终点,加入容器中,注意排序的时候如果坐标相同一定是起点在终点前,不然点的情况会漏掉,最后还要把可以合并的区间合并;

AC代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=1e6+5;
  4. int a,b;
  5. struct node
  6. {
  7. int fi,se;
  8. };
  9. node po[2*N];
  10. int cmp(node x,node y)
  11. {
  12. if(x.fi==y.fi)return x.se<y.se;
  13. return x.fi<y.fi;
  14. }
  15. vector < int >ve;
  16. int ans[2*N];
  17. int main()
  18. {
  19. int n,k;
  20. scanf("%d%d",&n,&k);
  21. for(int i =0;i<2*n;i+=2)
  22. {
  23. scanf("%d%d",&po[i].fi,&po[i+1].fi);
  24. po[i].se=-1;
  25. po[i+1].se=1;
  26. }
  27. sort(po,po+2*n,cmp);
  28. int num=0;
  29. for(int i=0;i<2*n;i++)
  30. {
  31. if(po[i].se==-1)
  32. {
  33. num++;
  34. if(num==k)
  35. {
  36. ve.push_back(po[i].fi);
  37. }
  38. }
  39. else
  40. {
  41. if(num==k)
  42. {
  43. ve.push_back(po[i].fi);
  44. }
  45. num--;
  46. }
  47. }
  48. int cnt=0;
  49. int len=ve.size();
  50. if(len>0){
  51. ans[cnt++]=ve[0];
  52. for(int i=1;i<len-1;i+=2)
  53. {
  54. if(ve[i]==ve[i+1])
  55. {
  56. continue;
  57. }
  58. else
  59. {
  60. ans[cnt++]=ve[i];
  61. ans[cnt++]=ve[i+1];
  62. }
  63. }
  64. ans[cnt++]=ve[len-1];
  65. }
  66. printf("%d\n",cnt/2);
  67. for(int i=0;i<cnt;i+=2)
  68. {
  69. printf("%d %d\n",ans[i],ans[i+1]);
  70. }
  71. return 0;
  72. }

codeforces 612D The Union of k-Segments (线段排序)的更多相关文章

  1. CodeForces - 612D 思维

    题意: 给你n个线段和一个整数k,你需要找出来所有能被任意k条线段同时覆盖的区间个数的最小值,并按从左到右的顺序输出每个区间. 题解: 对于题目输入的n个线段的左端点L,右端点R,把它们分开放在结构体 ...

  2. 合并k个已排序的链表 分类: leetcode 算法 2015-07-09 17:43 3人阅读 评论(0) 收藏

    最先想到的是把两个linked lists 合并成一个. 这样从第一个开始一个一个吞并,直到所有list都被合并. class ListNode:# Definition for singly-lin ...

  3. SqlServer中的UNION操作符在合并数据时去重的原理以及UNION运算符查询结果默认排序的问题

    本文出处:http://www.cnblogs.com/wy123/p/7884986.html 周围又有人在讨论UNION和UNION ALL,对于UNION和UNION ALL,网上说的最多的就是 ...

  4. B - Toy Storage(POJ - 2398) 计算几何基础题,比TOYS多了个线段排序

    Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing ...

  5. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线

    D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...

  6. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并

    D. Vika and Segments     Vika has an infinite sheet of squared paper. Initially all squares are whit ...

  7. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

  8. Codeforces 610D Vika and Segments 线段树+离散化+扫描线

    可以转变成上一题(hdu1542)的形式,把每条线段变成宽为1的矩形,求矩形面积并 要注意的就是转化为右下角的点需要x+1,y-1,画一条线就能看出来了 #include<bits/stdc++ ...

  9. CodeForces 754D Fedor and coupons ——(k段线段最大交集)

    还记得lyf说过k=2的方法,但是推广到k是其他的话有点麻烦.现在这里采取另外一种方法. 先将所有线段按照L进行排序,然后优先队列保存R的值,然后每次用最小的R值,和当前的L来维护答案即可.同时,如果 ...

随机推荐

  1. linux使用rsync+inotify-tools+ssh实现文件实时同步

    假设某服务器架构中有两台web服务器(IP为192.168.1.252和192.168.1.254),一台代码更新发布服务器(IP为192.168.1.251),需要同步的目录是/data/www/, ...

  2. Composer的Autoload源码实现2——注册与运行

    前言 上一篇 文章我们讲到了 Composer 自动加载功能的启动与初始化,经过启动与初始化,自动加载核心类对象已经获得了顶级命名空间与相应目录的映射,换句话说,如果有命名空间 'App\Consol ...

  3. linux系统之间基于密钥对免输入密码登陆

    通常,我们登陆到一台linux主机是需要输入密码,这样可以验证登陆者的身份. 这篇随笔里面我打算记录一下配置基于ssh密钥对登陆系统.在配置之前先和大家说明一下我遇到过的问题:刚接触到linux时候首 ...

  4. 【BZOJ3791】作业 DP

    [BZOJ3791]作业 Description 众所周知,白神是具有神奇的能力的.比如说,他对数学作业说一声“数”,数学作业就会出于畏惧而自己完成:对语文作业说一声“语”,语文作业就会出于畏惧而自己 ...

  5. 基于SpringMVC国际化资源配置方式

    1.首先需要在spring-mvc-servlet.xml 中配置拦截器: <bean id="localeChangeInterceptor" class="or ...

  6. 信息搜集之google语法

    总结的比较全,无耻的转了.D: http://blog.csdn.net/chaosa/article/details/1828301 说起Google,可谓无人不知无人不晓.作为世界第一的搜索引擎, ...

  7. Android系统移植与调试之------->如何修改Android设备添加重启、飞行模式、静音模式等功能(二)

    今天要说的是为Android设备添加重启.飞行模式.静音模式按钮,客户需求中需要添加这项功能,在长按电源键弹出的菜单中没有这些选项,谨以此文记录自己添加这个功能的过程. 首先找到长按电源键弹出的对话框 ...

  8. linux里的drwxr-xr-x代表的意思

    权限的计算是除去第一位字母开始,权限都是三个符号为一组合,其中-表示没有这个权限 d:第一位表示文件类型.d是目录文件,l是链接文件,-是普通文件,p是管道 rwx:第2-4位表示这个文件的属主拥有的 ...

  9. Python map,reduce,filter,apply

    map(function, iterable, ...) map()函数接收两个参数,一个是函数,一个是可迭代的对象,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回. 基本等 ...

  10. DevOps means no Ops!

    DevOps means no Ops! 只单纯地搞网络的话或许你可以搞得非常好,并且获得不错的薪资,不过,5年后~10年后~,那时候随便一个人经过简单的学习就能通过Web界面或者专用的工具就能搞定一 ...