D. The Union of k-Segments
 

You re given n segments on the coordinate axis Ox and the number k. The point is satisfied if it belongs to at least k segments. Find the smallest (by the number of segments) set of segments on the coordinate axis Ox which contains all satisfied points and no others.

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 106) — the number of segments and the value of k.

The next n lines contain two integers li, ri ( - 109 ≤ li ≤ ri ≤ 109) each — the endpoints of the i-th segment. The segments can degenerate and intersect each other. The segments are given in arbitrary order.

Output

First line contains integer m — the smallest number of segments.

Next m lines contain two integers aj, bj (aj ≤ bj) — the ends of j-th segment in the answer. The segments should be listed in the order from left to right.

Sample test(s)
input
  1. 3 2
    0 5
    -3 2
    3 8
output
  1. 2
    0 2
    3 5
input
  1. 3 2
    0 5
    -3 3
    3 8
output
  1. 1
    0 5

 题意:给你一n条线段,一个k,问你有k跳线段以上相交的线段有多少

题解:按照 左右端点区分,x,y一起排序,找增加至k个左端点加入左答案,减少到k-1个端点加入右答案,注意一个点的线段

  1. //meek
  2. ///#include<bits/stdc++.h>
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <cmath>
  6. #include <string>
  7. #include <cstring>
  8. #include <algorithm>
  9. #include <queue>
  10. #include <map>
  11. #include <set>
  12. #include <stack>
  13. #include <sstream>
  14. #include <queue>
  15. using namespace std ;
  16. typedef long long ll;
  17. #define mem(a) memset(a,0,sizeof(a))
  18. #define pb push_back
  19. #define fi first
  20. #define se second
  21. #define MP make_pair
  22. inline ll read()
  23. {
  24. ll x=,f=;
  25. char ch=getchar();
  26. while(ch<''||ch>'')
  27. {
  28. if(ch=='-')f=-;
  29. ch=getchar();
  30. }
  31. while(ch>=''&&ch<='')
  32. {
  33. x=x*+ch-'';
  34. ch=getchar();
  35. }
  36. return x*f;
  37. }
  38. //****************************************
  39.  
  40. const int N=+;
  41. const ll INF = 1ll<<;
  42. const int inf = ;
  43. const int mod= ;
  44.  
  45. int n,k,x,y,t[N];
  46. pair<int,int> Line[N];
  47. vector<int >ans1,ans2;
  48. int main() {
  49. scanf("%d%d",&n,&k);
  50. int cnt = ;
  51. for(int i=;i<=n;i++) {
  52. scanf("%d%d",&x,&y);
  53. Line[cnt++] = MP(x,-);
  54. Line[cnt++] = MP(y,);
  55. }
  56. sort(Line+,Line+cnt);
  57. for(int i=;i<cnt;i++)
  58. {
  59. t[i] = t[i-] - Line[i].se;
  60. if(t[i]==k&&t[i-]==k-)
  61. ans1.pb(Line[i].fi);
  62. }
  63. memset(t,,sizeof(t));
  64. for(int i=;i<cnt;i++)
  65. {
  66. t[i] = t[i-] - Line[i].se;
  67. if(t[i]==k-&&t[i-]==k)
  68. ans2.pb(Line[i].fi);
  69. }
  70. if(ans1.size()!=ans2.size()) ans2.pb(Line[cnt-].fi);
  71. cout<<ans1.size()<<endl;
  72. for(int i=;i<ans1.size();i++)
  73. cout<<ans1[i]<<" "<<ans2[i]<<endl;
  74. return ;
  75. }

代码

Educational Codeforces Round 4 D. The Union of k-Segments 排序的更多相关文章

  1. 【扫描线】Educational Codeforces Round 4 D. The Union of k-Segments

    http://codeforces.com/contest/612/problem/D [题解] http://blog.csdn.net/strokess/article/details/52248 ...

  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 65 (Rated for Div. 2)题解

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

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

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

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

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

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

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

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

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

  9. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

随机推荐

  1. windows phone版的一个儿教app

    昨天下午看见一个园友写的一篇关于儿教的api,看了也就两三个接口,所以数据处理应该不会太复杂,主要是界面的效果,要求可能比较高.于是我就重新自己写了一个app,实现很简单,花的时间比较多的地方应该是在 ...

  2. IOS中的UINavigationController(导航控制器)

    UINavigationController UINavigationControlle:导航控制器,是iOS中最常用的多视图控制器之一,它用来管理多个试图控制器 导航控制器可以认为是管理控制器的控制 ...

  3. JS如何获取多个相同class标签并分别再输出各自的文本

    function getClass(boo) { var span = $("span.w");//获取所有的span标签 <span class="w" ...

  4. Python实现nb(朴素贝叶斯)

    Python实现nb(朴素贝叶斯) 运行环境 Pyhton3 numpy科学计算模块 计算过程 st=>start: 开始 op1=>operation: 读入数据 op2=>ope ...

  5. To add private variable to this Javascript literal object

    You can use number as function/variable name, the numberic name can't be accessed from parent scope, ...

  6. Valuable site on github

    https://thegrid.io/?utm_source=adwords&utm_medium=cpc&utm_campaign=thegrid-display-english&a ...

  7. 20145103 《Java程序设计》第3周学习总结

    20145103 <Java程序设计>第3周学习总结 教材学习内容总结 第四章我首先了解了CPU与内存的关系,栈与堆的关系.要产生对象必须先定义类,类是对象的设计图,对象是累的实例.以类名 ...

  8. Mysql数据库表排序规则不一致导致联表查询,索引不起作用问题

    Mysql数据库表排序规则不一致导致联表查询,索引不起作用问题 表更描述: 将mysql数据库中的worktask表添加ishaspic字段. 具体操作:(1)数据库worktask表新添是否有图片字 ...

  9. eclipse中设置中文javadoc+如何查看class的中文javadoc

    一.  eclipse中设置中文javadoc 1.先到http://download.java.net/jdk/jdk-api-localizations/jdk-api-zh-cn/publish ...

  10. Kali Linux 命令集

    系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...