【扫描线】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 ...
随机推荐
- 438 Find All Anagrams in a String 找出字符串中所有的变位词
详见:https://leetcode.com/problems/find-all-anagrams-in-a-string/description/ C++: class Solution { pu ...
- 188 Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV
假设你有一个数组,其中第 i 个元素是第 i 天给定股票的价格.设计一个算法来找到最大的利润.您最多可以完成 k 笔交易.注意:你不可以同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 详见: ...
- apache设置无缓存
打开httpd.conf 开启扩展 确保开启 LoadModule headers_module modules/mod_headers.so 添加配置项 并添加以下配置,跟据文件类型来让浏览器每次都 ...
- VUE 入坑系列 一 事件
html代码 <div id="app"> <button v-on:click="counter += 1">加1</butto ...
- JS性能分析(测试代码运行时间)
//性能优化 console.time("timer"); for(var i=0;i<10000;i++){} console.timeEnd("timer&qu ...
- Android(java)学习笔记193:ContentProvider使用之获得系统联系人信息01
1.系统联系人的数据库(3张最重要的表) (1)raw_contacts 联系人表 保存联系人的id contact_id (2)data 数据表 保存联系人的数据 ( ...
- vue vueRouter vuex Axios webpack 前端常用内容
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中.
- CAD参数绘制批注(com接口)
C#中实现代码说明: private void DrawComment() { MxDrawComment com = new MxDrawComment(); MxDrawPoint pt = ne ...
- 01CSS使用方法
CSS使用方法 内联定义 内联定义即是在对象的标记内使用对象的style属性定义适用其的样式表属性. 内部样式表 <style type="text/css"></style> ...
- js 上传图片、压缩、旋转
亲测 <!doctype html> <html> <head> <meta charset="utf-8"> <title& ...