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. 3s 简介

    "3S"技术是英文遥感技术(Remote Sensing RS).地理信息系统(Geographical information System GIS).全球定位系统(Global ...

  2. LINQ TO SQL:操作有层次关系的对象

    对于关系型数据与对象数据之间最大的隔阂就是由标识列连接起来的行(关系型数据)与由集合保存的对象(对象数据)之间的冲突. 例如某个Subject对象(也就是数据库中的Subject表),从Subject ...

  3. 不使用库函数,编写函数int strcmp(char *source, char *dest) 相等返回0,不等返回-1;

    答案:一. int strcmp(char  *source, char *dest) { /* assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stder ...

  4. ASIHTTP 框架,同步、 异步请求、 上传 、 下载

    ASIHTTPRequest详解 ASIHTTPRequest 是一款极其强劲的 HTTP 访问开源项目.让简单的 API 完成复杂的功能,如:异步请求,队列请求,GZIP 压缩,缓存,断点续传,进度 ...

  5. 【BZOJ5020】[THUWC 2017]在美妙的数学王国中畅游 泰勒展开+LCT

    [BZOJ5020][THUWC 2017]在美妙的数学王国中畅游 Description 数字和数学规律主宰着这个世界. 机器的运转, 生命的消长, 宇宙的进程, 这些神秘而又美妙的过程无不可以用数 ...

  6. git reset和git revert

    1 git reset commit-id 直接回到某次提交,该次commit-id之后的提交都会被删除. --hard,将index和本地都恢复到指定的commit版本. 2 git revert ...

  7. Django 路飞学成书写规范的总结

    路飞学成书写规范的总结 命名 项目/文件/函数 -> 小写+下划线 类 驼峰式 路由 将每个功能的路由进行分割, 或者做上注释 类和方法的注释 每个类都要注释是干什么的 每个方法也要进行注释标明 ...

  8. centos7 使用 maven

    http://www.cnblogs.com/jackluo/archive/2013/02/06/2901816.html

  9. centos7 Authentication failure

    root@localhost ~]#su bash-4.2$ su Password: su: Authentication failure //这里切换的是系统用户,现在还不清楚为什么postgre ...

  10. samsung n143 brightness on linux mint

    sudo vi /etc/default/grub Find the line which says GRUB_CMDLINE_LINUX="" enter acpi_backli ...