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来维护答案即可.同时,如果 ...
随机推荐
- IOS启动页动画(uiview 淡入淡出效果 )2
Appdelegate里面右个这个函数,只要它没结束,你的等待界面就不会消失.以在启动的时候做些动画 - (BOOL)application:(UIApplication *)application ...
- com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException: fallback method wasn't found: serviceError([class java.lang.String]) 异常
在使用spring cloud 的 Hystrix 后可能会遇到 如下截图错误: 后台代码如下: 找了好一会经过分析参数方法和原方法参数步一致造成: 修改后代码如下:
- EasyPlayer播放器浏览器ActiveX/OCX插件RTSP播放/抓拍/录像功能调用说明
EasyPlayerPro与EasyPlayer-RTSP新增ocx多窗口播放功能 这里以EasyPlayerPro为例,使用方法如下: 打开播放器文件夹,进入Bin/C++目录,可以看到reg.ba ...
- Django之CURD插件
什么是CURD? CURD顾名思义就是create,update,rearch,delete(所谓的增删改查). 当我们接到一个项目的时候,夸夸夸的就写完表结构,然后就一直写增删改查,增删改查,写了一 ...
- IOS navigationItem 设置返回button,title图片和rightBarButtonItem
1.自己定义返回button UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" st ...
- IOS - 执行时 (多态)
一 多态概述 多态指同一操作作用于不同的对象.能够有不同的解释.产生不同的执行结果.它是面向对象程序设计(OOP)的一个重要特征,动态类型能使程序直到执行时才确定对象的所属类.其详细 ...
- onclick事件表示方法
onclick事件表示方法 1.第一种是直接在html中插入onclick事件 & ...
- python 发送邮件的两种方式【终极篇】
一,利用python自带的库 smtplib简单高效 from email.mime.multipart import MIMEMultipart from email.mime.text impor ...
- LyX中文配置
环境:OS X 10.9; MacTeX-2014; LyX Version 2.1.0 LyX是一个“WYSIWYM”(What You See Is What You Mean)的文字排版系统.其 ...
- Swap file "/etc/.hosts.swp" already exists! [O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it,
非正常关闭vi编辑器时会生成一个.swp文件 非正常关闭vi编辑器时会生成一个.swp文件 关于swp文件 使用vi,经常可以看到swp这个文件,那这个文件是怎么产生的呢,当你打开一个文件,vi就会生 ...