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. 最简单的struts实例介绍

    struts2环境配置   struts2框架,大多数框架都在使用.由于工作需要,开始做Java项目.先学个struts2. 一.下载struts2 有好多版本,我下载的是struts-2.2.1.1 ...

  2. IOS利用Core Text对文字进行排版 - 转

    原贴地址:http://hi.baidu.com/jwq359699768/blog/item/5df305c893413d0a7e3e6f7b.html core text 这个包默认是没有的,要自 ...

  3. 设置当前导航栏(navigationController)的标题

    一般在有导航navigationController的情况下,要设置页面的标题很简单 self.title = @"测试"; 也可以 self.navigationItem.tit ...

  4. domain admin域管理员

    当计算机加入到域后,默认将"Domain Admins"组赋予了本地系统管理员的权限.也就是说,在计算机添加到域,成为域的成员主机的过程中,系统将会自动把"Domain ...

  5. BufferedStream

    处理流,包在别的流上面的流,相当于包到别的管道上面的管道. 缓冲刘: 缓冲流,就是带小桶的带缓冲区的. 那两个方法知道一下就好了,不必深究…… bis.read()    read一下读一个出来 bi ...

  6. Python 学习之Virtualenv

    Virtualenv是一个python环境的隔离工具,主要解决库的隔离和权限问题 Refer:中文版Virtualevn解释 用virtualenv创建多个python环境 我们360如何使用pyth ...

  7. LN : leetcode 413 Arithmetic Slices

    lc 413 Arithmetic Slices 413 Arithmetic Slices A sequence of number is called arithmetic if it consi ...

  8. Struts2------拦截器和标签库和注解开发

    一.解析Struts2源码中拦截器的执行 客户端请求Action,执行前端控制器,在前端控制器内部创建了Action的代理类,调用代理类的execute方法,在execute方法内部执行ActionI ...

  9. php查询快递信息

    $code = 'shunfeng'; $invoice = '952255884068'; $test = getExpressDelivery($code,$invoice); function ...

  10. sql server 2008 r2 无法定位到数据库文件目录

    像这样,选择数据库文件时, 无法定位到文件夹目录,子目录下的都不显示.明明选择的这个文件夹里还有很多子文件夹,却显示不了. 解决方法: 在此文件夹上右击,属性-安全 添加红框中的用户就可以了.