http://codeforces.com/contest/612/problem/D

【题解】

http://blog.csdn.net/strokess/article/details/52248062

【题意】

在数轴x上,给定n个线段和一个值k,问被这n条线段覆盖了至少k次的区间有多少个,输出每一个。

【思路】

扫描线思想。把这n个线段的左右端点放在一起从小到大排序,用cnt记录当前区间被覆盖了多少次,遇到左端点cnt++,遇到右端点cnt--,当cnt==k时记录这时的左端点和右端点。

当左端点和右端点相等时先考虑左端点再考虑右端点,具体为什么可以考虑第二个样例。

因此我们可以很方便地用pair实现这一点,用0标记左端点,用1标记右端点。pair默认排序按first优先,second其次。

【Accepted】

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<utility> using namespace std;
const int maxn=1e6+;
typedef long long ll;
const ll mod=1e9+; vector<pair<int,int> > v;
vector<int> ans;
int n,k;
int main()
{
while(~scanf("%d%d",&n,&k))
{
v.clear();
ans.clear();
for(int i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
v.push_back(make_pair(x,));
v.push_back(make_pair(y,));
}
sort(v.begin(),v.end());
int sz=v.size();
int cnt=;
for(int i=;i<sz;i++)
{
// cout<<v[i].first<<" "<<v[i].second<<endl;
if(v[i].second==)
{
cnt++;
if(cnt==k)
{
ans.push_back(v[i].first);
}
}
else
{
if(cnt==k)
{
ans.push_back(v[i].first);
}
cnt--;
}
}
printf("%d\n",ans.size()/);
for(int i=;i<ans.size()/;i++)
{
printf("%d %d\n",ans[*i],ans[*i+]);
}
}
return ;
}

扫描线

【扫描线】Educational Codeforces Round 4 D. The Union of k-Segments的更多相关文章

  1. Educational Codeforces Round 4 D. The Union of k-Segments 排序

    D. The Union of k-Segments   You re given n segments on the coordinate axis Ox and the number k. The ...

  2. Educational Codeforces Round 5

    616A - Comparing Two Long Integers    20171121 直接暴力莽就好了...没什么好说的 #include<stdlib.h> #include&l ...

  3. Educational Codeforces Round 58 (Rated for Div. 2) 题解

    Educational Codeforces Round 58 (Rated for Div. 2)  题目总链接:https://codeforces.com/contest/1101 A. Min ...

  4. Educational Codeforces Round 68 E. Count The Rectangles

    Educational Codeforces Round 68 E. Count The Rectangles 传送门 题意: 给出不超过\(n,n\leq 5000\)条直线,问共形成多少个矩形. ...

  5. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

  6. Educational Codeforces Round 64 (Rated for Div. 2)题解

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

  7. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  8. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  9. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

随机推荐

  1. jmeter正则表达式提取器使用

    引用名称:请求中的参数需要引用的名称 正则表达式:从结果集中提取数据,例如从数据库查询结果中提取数据 模板:$1$表示提取表达式中的第一个值,$n$以此类推 匹配数字:0代表随机,1代表第一个值,n代 ...

  2. 关于springMVC传参问题

    今天写项目,碰到一个以前灭有注意到的问题,一般情况下使用springMVC @Controller注解之后,被此注解标记的方法的参数名只需要跟页面表单的标签的name的值相同即可拿到页面的值,但是如果 ...

  3. 平衡图片负载,提升web站点访问体验

    最近给分公司做官方网站,内网测试一切ok,发布至云端后,体验惊人——公司外网网速渣渣(十几k~几十k),更加要命的是,网站的高清图,根本就加载不出来,几秒,十几秒过去了,仍然在转圈圈,如下图... 于 ...

  4. Saleae Logic添加NEC IR协议

    一.下载需要用到的代码 Git clone https://github.com/LiveOverflow/NECAnalyzer.git git clone --recursive https:// ...

  5. js 日期时间大小比较

    <body> 开始时间:<input onfocus="setday(this)" id="startTime" name="sta ...

  6. mongodb用户权限管理(二)

    数据库 分配用户权限 有了创建语法,和参数说明,接下来开始实践. 注意,还有一点,账号是跟着数据库绑定的,在那个库里授权,就在那个库里验证(auth) 否则会失败 创建 账号管理授权权限 的账号 &g ...

  7. codevs 1082 线段树练习 3 --分块练习

    时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[ ...

  8. 诊断:RHEL7安装11.2RAC时root.sh错误ohasd failed to start

    RHEL 7.5中安装11gRAC时,在grid infrastructure的root.sh执行时,报错: # /oracle/product/11g/grid/root.sh ... Adding ...

  9. fastclick.js插件使用

    引入插件步骤 ①在HTML页面中添加  <script type='application/javascript' src='/path/to/fastclick.js'></scr ...

  10. ID字段不采用数据库自增长的几点理由

    一个小程序,最初采用了 SqlServer 数据库,后来为了便于部署,转而采用了 Firebird 嵌入式数据库.在重构代码转到 Firebird 的过程中,对“数据实体的数据表的ID字段是否应该使用 ...