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
3 2
0 5
-3 2
3 8
output
2
0 2
3 5
input
3 2
0 5
-3 3
3 8
output
1
0 5

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

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

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int a,b;
struct node
{
int fi,se;
};
node po[2*N];
int cmp(node x,node y)
{
if(x.fi==y.fi)return x.se<y.se;
return x.fi<y.fi;
}
vector < int >ve;
int ans[2*N];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i =0;i<2*n;i+=2)
{
scanf("%d%d",&po[i].fi,&po[i+1].fi);
po[i].se=-1;
po[i+1].se=1;
}
sort(po,po+2*n,cmp);
int num=0;
for(int i=0;i<2*n;i++)
{
if(po[i].se==-1)
{
num++;
if(num==k)
{
ve.push_back(po[i].fi);
}
}
else
{
if(num==k)
{
ve.push_back(po[i].fi);
}
num--;
}
}
int cnt=0;
int len=ve.size();
if(len>0){
ans[cnt++]=ve[0];
for(int i=1;i<len-1;i+=2)
{
if(ve[i]==ve[i+1])
{
continue;
}
else
{
ans[cnt++]=ve[i];
ans[cnt++]=ve[i+1];
}
}
ans[cnt++]=ve[len-1];
}
printf("%d\n",cnt/2);
for(int i=0;i<cnt;i+=2)
{
printf("%d %d\n",ans[i],ans[i+1]);
}
return 0;
}

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. Oracle Data Provider for .NET的使用(托管与非托管(一))

    目录 简单的概述 简单的使用 非托管系统要求 托管驱动系统要求 其它的注意事项 ODP.NET版本说明 安装ODP.NET 安装非托管驱动 非托管驱动绿色配置 简单的概述 ODP.NET的含义是 Or ...

  2. WPF 员工卡条形码

    大家都知道条形码(Barcode)是一种可以由机器识别的特殊编码,在生产.生活中也常常会见到并使用它.条形码的类型和种类很多感兴趣的朋友可以详细了解一下.其中Code 39 可以说是一种最为常见并广泛 ...

  3. 查看Linux服务器的物理状态

    1.当前内存使用情况 [user@host ~]$ free -m 2.当前CPU使用情况 [user@host ~]$ top 3.当前硬盘使用状态 [user@host ~]$ df -lh 4. ...

  4. 详解Linux Top 命令

    Linux top命令简介 top 命令是最流行的性能监视工具之一,我们必需了解.它是一个优秀的交互式工具,用于监视性能.它提供系统整体性能,但报告进程信息才是 top 命令的长处.top 命令交互界 ...

  5. Linux下,部署多个Tomcat

    1.复制一个已有的tomcat,并重命名: 2.打开tomcat--conf--server.xml:[共修改3处配置] <Server port="8005" shutdo ...

  6. Office 365系列(-)

    昨天参加上海微软TechED技术大会,看见很多传说中的大牛,听了涂曙光老师等人的讲座,激情澎湃啊,看见他们对技术以及程序员社区的投入及激情,十分敬佩.自己搞IT行业也已经10多年了,平常都很少写博客和 ...

  7. PLSQL使用技巧----加快你的编程效率

    使用PLSQL 编程效率明显有所提高了 1.登录后默认自动选中My Objects      默认情况下,PLSQL Developer登录后,Brower里会选择All objects,如果你登录的 ...

  8. java.time.format.DateTimeFormatter

    Java的日期与时间 DateTimeFormatter类是Java 8中日期时间功能里,用于解析和格式化日期时间的类,位于java.time.format包下.   1.预定义的DateTimeFo ...

  9. linux 8 -- 管道组合Shell命令进行系统管理

    二十. 通过管道组合Shell命令获取系统运行数据: 1.  输出当前系统中占用内存最多的5条命令:     #1) 通过ps命令列出当前主机正在运行的所有进程.     #2) 按照第五个字段基于数 ...

  10. androidAndroid开发学习--Ionic+Cordova 环境搭建

    我们看 Ionic 能给我们提供什么?  一个样式库,你可以使用它 来 装饰你的 HTML 网页 ,看起来 想 移动程序的 界面,什么 header .content.footer.grid.list ...