codeforces 612D The Union of k-Segments (线段排序)
4 seconds
256 megabytes
standard input
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.
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.
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.
3 2
0 5
-3 2
3 8
2
0 2
3 5
3 2
0 5
-3 3
3 8
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 (线段排序)的更多相关文章
- CodeForces - 612D 思维
题意: 给你n个线段和一个整数k,你需要找出来所有能被任意k条线段同时覆盖的区间个数的最小值,并按从左到右的顺序输出每个区间. 题解: 对于题目输入的n个线段的左端点L,右端点R,把它们分开放在结构体 ...
- 合并k个已排序的链表 分类: leetcode 算法 2015-07-09 17:43 3人阅读 评论(0) 收藏
最先想到的是把两个linked lists 合并成一个. 这样从第一个开始一个一个吞并,直到所有list都被合并. class ListNode:# Definition for singly-lin ...
- SqlServer中的UNION操作符在合并数据时去重的原理以及UNION运算符查询结果默认排序的问题
本文出处:http://www.cnblogs.com/wy123/p/7884986.html 周围又有人在讨论UNION和UNION ALL,对于UNION和UNION ALL,网上说的最多的就是 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...
- Codeforces 610D Vika and Segments 线段树+离散化+扫描线
可以转变成上一题(hdu1542)的形式,把每条线段变成宽为1的矩形,求矩形面积并 要注意的就是转化为右下角的点需要x+1,y-1,画一条线就能看出来了 #include<bits/stdc++ ...
- CodeForces 754D Fedor and coupons ——(k段线段最大交集)
还记得lyf说过k=2的方法,但是推广到k是其他的话有点麻烦.现在这里采取另外一种方法. 先将所有线段按照L进行排序,然后优先队列保存R的值,然后每次用最小的R值,和当前的L来维护答案即可.同时,如果 ...
随机推荐
- UFLDL深度学习笔记 (五)自编码线性解码器
UFLDL深度学习笔记 (五)自编码线性解码器 1. 基本问题 在第一篇 UFLDL深度学习笔记 (一)基本知识与稀疏自编码中讨论了激活函数为\(sigmoid\)函数的系数自编码网络,本文要讨论&q ...
- Android Studio 默认的快捷键
参考资料: 1.http://stormzhang.com/devtools/2014/12/09/android-studio-tutorial3/ Action Mac OSX Win/Linux ...
- python获取shell输出(转)
From:http://www.cnblogs.com/snow-backup/p/5035792.html python中获取shell命令输出的方法: 1. import subproces ...
- selenium驱动Firefox跳转页慢慢慢的问题(待验证)
转载至http://www.cnblogs.com/yicaifeitian/p/5198871.html 为了解决这个问题,我是查了很多资料,解决方案是百度出来的.抱歉,我忘记出处在哪了,代码如下: ...
- android事件拦截处理机制具体解释
前段时间刚接触过android手机开发.对它的事件传播机制不是非常了解,尽管网上也查了相关的资料,可是总认为理解模模糊糊,似是而非,于是自己就写个小demo測试了一下. 总算搞明确了它的详细机制.写下 ...
- mysql海量数据条件删除
1. 问题描述:现在存在两个表,具体表结构及记录数如下所示: mysql> desc user_mapping; +------------+------------------+------+ ...
- 获取unity prefab的预览图像
官方的api可以直接获取预览图像,如下所示: Tex=AssetPreview.GetAssetPreview(Object m)as Texture; 但是如果prefab是组合体的话(即一个pre ...
- 第8章 Foundation Kit介绍
本文转载至 http://blog.csdn.net/mouyong/article/details/16947321 Objective-C是一门非常精巧实用的语言,目前我们还没有研究完它提供的全 ...
- JS 创建对象(常见的几种方法)
贴个代码先: function O(user,pwd){ //use constructor this.user=user; this.pwd=pwd; this.get=get; return th ...
- Educational Codeforces Round 1 (C) (atan2 + long double | 大数)
这题只能呵呵了. 东搞西搞,折腾快一天,最后用了一个800多行的代码AC了. 好好的题目你卡这种精度干啥. 还有要卡您就多卡点行不,为什么long double 又可以过... 废了N长时间写个了不管 ...