按区间长度降序排序
维护区间指针 [l, r],第 l ~ r 条线段
表示当前区间可以满足条件
那么 r 后移一定不是更优的
因此 l 前移,使得 r 后移
过程中取最小值更新 answer

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <cstring>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. #define LL long long
  11.  
  12. #define gc getchar()
  13. inline int read() {int x = ; char c = gc; while(c < '' || c > '') c = gc;
  14. while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
  15. inline LL read_LL() {LL x = ; char c = gc; while(c < '' || c > '') c = gc;
  16. while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
  17. #undef gc
  18.  
  19. const int N = 1e6 + ;
  20.  
  21. struct Node {int l, r, len;} G[N >> ];
  22. int A[N];
  23. int Max[N << ], W[N << ], F[N << ];
  24. int n, m;
  25.  
  26. inline bool Cmp(Node a, Node b) {return a.len > b.len;}
  27.  
  28. #define lson jd << 1
  29. #define rson jd << 1 | 1
  30.  
  31. void Push_down(int jd) {
  32. F[lson] += F[jd], F[rson] += F[jd];
  33. Max[lson] += F[jd], Max[rson] += F[jd];
  34. F[jd] = ;
  35. }
  36.  
  37. void Sec_G(int l, int r, int jd, int x, int y, int num) {
  38. if(x <= l && r <= y) {
  39. Max[jd] += num;
  40. F[jd] += num;
  41. return ;
  42. }
  43. if(F[jd]) Push_down(jd);
  44. int mid = (l + r) >> ;
  45. if(x <= mid) Sec_G(l, mid, lson, x, y, num);
  46. if(y > mid ) Sec_G(mid + , r, rson, x, y, num);
  47. Max[jd] = max(Max[lson], Max[rson]);
  48. }
  49.  
  50. bool use[N];
  51.  
  52. int main() {
  53. n = read(), m = read();
  54. int tot = ;
  55. for(int i = ; i <= n; i ++) {
  56. G[i].l = read(), G[i].r = read(), G[i].len = G[i].r - G[i].l; A[++ tot] = G[i].l, A[++ tot] = G[i].r;
  57. }
  58. sort(G + , G + n + , Cmp);
  59. sort(A + , A + tot + );
  60. for(int i = ; i <= n; i ++) {
  61. G[i].l = lower_bound(A + , A + tot + , G[i].l) - A;
  62. G[i].r = lower_bound(A + , A + tot + , G[i].r) - A;
  63. }
  64. int R = , Answer = (int)1e9 + ;
  65. for(int i = ; i <= n; i ++) {
  66. if(R == n) break;
  67. if(use[i] == ) {
  68. Sec_G(, tot, , G[i].l, G[i].r, );
  69. use[i] = ;
  70. }
  71. while(Max[] < m && R < n) {
  72. R ++;
  73. Sec_G(, tot, , G[R].l, G[R].r, );
  74. use[R] = ;
  75. if(Max[] >= m) {
  76. Answer = min(Answer, G[i].len - G[R].len);
  77. break;
  78. }
  79. }
  80. if(use[i]) {
  81. Sec_G(, tot, , G[i].l, G[i].r, -);
  82. use[i] = ;
  83. }
  84. }
  85. if(Answer == (int)1e9 + ) cout << "-1";
  86. else cout << Answer;
  87. return ;
  88. }

luogu 1712的更多相关文章

  1. luogu 1712 区间(线段树+尺取法)

    题意:给出n个区间,求选择一些区间,使得一个点被覆盖的次数超过m次,最小的花费.花费指的是选择的区间中最大长度减去最小长度. 坐标值这么大,n比较小,显然需要离散化,需要一个技巧,把区间转化为半开半闭 ...

  2. Luogu 魔法学院杯-第二弹(萌新的第一法blog)

    虽然有点久远  还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题  沉迷游戏,伤感情 #include <queue> ...

  3. luogu p1268 树的重量——构造,真正考验编程能力

    题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...

  4. HDU 1712 裸分组dp

    http://acm.hdu.edu.cn/showproblem.php?pid=1712 N门课M天复习,第i门课花费j天获得的效益是dp[i][j] 求最大效益 分组背包,同一门课不能选两次 三 ...

  5. [luogu P2170] 选学霸(并查集+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...

  6. [luogu P2647] 最大收益(贪心+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...

  7. HDU 1712 ACboy needs your help (分组背包模版题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 有n门课,和m天时间.每门课上不同的天数有不同的价值,但是上过这门课后不能再上了,求m天里的最大 ...

  8. hdu 1712 ACboy needs your help 分组背包

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 Problem ...

  9. HDU 1712 ACboy needs your help 典型的分组背包

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 ACboy needs your help Time Limit: 1000/1000 MS ( ...

随机推荐

  1. docker 实践六:dockerfile 详解

    本篇开始来学习关于 dockerfile 的知识. 注:环境为 CentOS7,docker 19.03. dockerfile 是⼀个⽂本格式的配置⽂件, ⽤户可以使⽤ dockerfile 来快速 ...

  2. Vue.js源码全方位深入解析--学习笔记

    模板中的插入变量是如何渲染到DOM上的? initMixin(Vue)->_init->$options-> $mount()当执行该挂载方法时DOM变化 为什么可以通过this访问 ...

  3. 在论坛中出现的比较难的sql问题:41(循环替换 循环替换关键字)

    原文:在论坛中出现的比较难的sql问题:41(循环替换 循环替换关键字) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.

  4. mvc伪静态

    方法一:IIS配置伪静态 方法二:项目配置伪静态 网站配置文件Web.config <system.webServer> <handlers> <add name=&qu ...

  5. mysql 系统变量

    show variables; ---------------------------------+-------------------------------------------------- ...

  6. 查询并批量插入数据的Sql命令

    INSERT INTO student(id,xuesheng,yuwen,shuxue,yingyu) SELECT id,xuesheng,yuwen,shuxue,yingyu FROM stu ...

  7. 2019年6月车型数据Access数据库+缩略图 更新于2019年6月5日.

    工作需要才来采集的, 数据来源某卡汽车网, 分享出来给需要的人吧, 本着分享的精神, 我就不猥琐的放到csdn下载了 本来是sql server的, 我导出到access了, 也方便大家查看. 顺手抓 ...

  8. py网络编程学习笔记

    一.异常处理 异常就是程序运行时发生错误的信号(在程序出现错误时,则会产生一个异常,若程序没有处理它,则会抛出该异常,程序的运行也随之终止),在python中,错误触发的异常如下 而错误分为两种: 1 ...

  9. 外网Telnet虚拟机,及nat配置等

    环境整体是使用GNS3,通过cloud真实连接到虚拟机 cloud1 nginx主机 cloud2 nginx主机https连接 cloud3 Internet测试主机 下面贴每个网络设备配置,特别注 ...

  10. PHP危险函数的持续学习

    记录下遇到过的PHP危险函数 0x01 escapeshellarg()与escapeshellsmd()联合 先给出官方的定义: escapeshellarg ( string $arg ) : s ...