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. chromedriver与chrome版本对应

    今天把手头有的一些关于selenium测试的资源整理了一下,分享出来. 1. 所有版本chrome下载 是不是很难找到老版本的chrome?博主收集了几个下载chrome老版本的网站,其中哪个下载的是 ...

  2. Windows 7操作系统下PHP 7的安装与配置(图文详解)

    前提博客 Windows 7操作系统下Apache的安装与配置(图文详解) 从官网下载           PHP的官网 http://www.php.net/         特意,新建这么一个目录 ...

  3. selenium通过autoit实现上传和下载

    autoit安装目录如下: AutoIt Windows Info   用于帮助我们识Windows控件信息. Compile Script to.exe 用于将AutoIt生成 exe 执行文件. ...

  4. ftp 上传与下载

    //上传 ftpmg.Upload("", DateTime.Now.ToString("yyyyMMddhhmmss")); //下载 ftpmg.Downl ...

  5. AJPFX关于多态中的动态绑定和静态绑定的总结

    在多态中:成员变量和静态方法编译和运行都看左边:成员方法编译看左边,运行看右边,这是为什么:在Java中存在两种绑定方式,一种为静态绑定,又称作早期绑定.另一种就是动态绑定,亦称为后期绑定1.静态绑定 ...

  6. LN : leetcode 413 Arithmetic Slices

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

  7. Objective-C Memory Management Being Exceptional 异常处理与内存

    Objective-C Memory Management    Being Exceptional  异常处理与内存 3.1Cocoa requires that all exceptions mu ...

  8. Quartz2D知识点聚合案例

    Quartz2D知识点聚合 基本 //画图片 UIImage *image = [UIImage imageNamed:@"阿狸头像"]; [image drawInRect:re ...

  9. 【git】搭建git服务器

    在 Linux 下搭建 Git 服务器 目录 ① 安装 Git ② 服务器端创建 git 用户,用来管理 Git 服务,并为 git 用户设置密码 ③ 服务器端创建 Git 仓库 ④ 客户端 clon ...

  10. Linux内核中TCP SACK机制远程DoS预警通告

    漏洞描述 2019年6月18日,RedHat官网发布报告:安全研究人员在Linux内核处理TCP SACK数据包模块中发现了三个漏洞,CVE编号为CVE-2019-11477.CVE-2019-114 ...