【扫描线】Educational Codeforces Round 4 D. The Union of k-Segments
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的更多相关文章
- 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 ...
- Educational Codeforces Round 5
616A - Comparing Two Long Integers 20171121 直接暴力莽就好了...没什么好说的 #include<stdlib.h> #include&l ...
- Educational Codeforces Round 58 (Rated for Div. 2) 题解
Educational Codeforces Round 58 (Rated for Div. 2) 题目总链接:https://codeforces.com/contest/1101 A. Min ...
- Educational Codeforces Round 68 E. Count The Rectangles
Educational Codeforces Round 68 E. Count The Rectangles 传送门 题意: 给出不超过\(n,n\leq 5000\)条直线,问共形成多少个矩形. ...
- Educational Codeforces Round 65 (Rated for Div. 2)题解
Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...
- Educational Codeforces Round 64 (Rated for Div. 2)题解
Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
随机推荐
- 018 [工具软件]截图贴图注释 Snipaste
Snipaste 是一个截图贴图工具,绿色免费.官方主页:https://zh.snipaste.com/. 三大功能: 1.截图,可以自动识别窗口的各元素,可以精准到像素调整截图区域大小. 2.贴图 ...
- Oracle分区表例子
分区表 Oracle提供的分区方法有以下几种. 1.范围分区(range) 范围分区是应用范围比较广的表分区方式,它是以列的值的范围来作为分区的划分条 件,将记录存放到列值所在的 range分区中. ...
- 构建微服务开发环境5————安装Node.js
[内容指引] 下载Node.js: Mac下安装Node.js: Windows下安装Node.js; 查看node和npm的版本. 一.下载Node.js 访问Node.js官网:https://n ...
- 深入学习数据结构之bitmap(四)
Bitmap,今天我们来分析一下bitmap的实现原理以及它的使用场景. 一.使用场景: 1.对于大量数据(几千个数据的就不要在废话了),且无重复或者可以忽略重复的数字.为啥这里要强调无重复,因为在b ...
- hihocoder编程练习赛52-3 部门聚会
思路: 树形dp. 实现: #include <bits/stdc++.h> using namespace std; ; int n, a[MAXN], in[MAXN]; vector ...
- poj1930 Dead Fraction
思路: 循环小数化分数,枚举所有可能的循环节,取分母最小的那个. 实现: #include <iostream> #include <cstdio> #include < ...
- 如何用 CSS 绘制各种形状
自适应的椭圆 1.自适应的椭圆 实现方式是通过border-radius这个属性:border-radius它可以单独指定水平和垂直半径.用 / 分隔这两个值.并且该属性的值不仅可以接受长度值,还能接 ...
- t470安装win7
终于把win7安装好了,写了个文档 https://files.cnblogs.com/files/cookies9/t470%E5%AE%89%E8%A3%85win7%E6%96%B9%E6%B3 ...
- 关于mapState和mapMutations和mapGetters 和mapActions辅助函数的用法及作用(三)-----mapGetters
简单的理解: const getters = { newCount: function(state){ return state.count += 5; } } ------------------- ...
- Bootstrap modal使用及点击外部不消失的解决方法
这篇文章主要为大家详细介绍了Bootstrap modal使用及点击外部不消失的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了Bootstrap modal使用及点击 ...