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

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string> using namespace std; #define LL long long #define gc getchar()
inline int read() {int x = ; char c = gc; while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
inline LL read_LL() {LL x = ; char c = gc; while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
#undef gc const int N = 1e6 + ; struct Node {int l, r, len;} G[N >> ];
int A[N];
int Max[N << ], W[N << ], F[N << ];
int n, m; inline bool Cmp(Node a, Node b) {return a.len > b.len;} #define lson jd << 1
#define rson jd << 1 | 1 void Push_down(int jd) {
F[lson] += F[jd], F[rson] += F[jd];
Max[lson] += F[jd], Max[rson] += F[jd];
F[jd] = ;
} void Sec_G(int l, int r, int jd, int x, int y, int num) {
if(x <= l && r <= y) {
Max[jd] += num;
F[jd] += num;
return ;
}
if(F[jd]) Push_down(jd);
int mid = (l + r) >> ;
if(x <= mid) Sec_G(l, mid, lson, x, y, num);
if(y > mid ) Sec_G(mid + , r, rson, x, y, num);
Max[jd] = max(Max[lson], Max[rson]);
} bool use[N]; int main() {
n = read(), m = read();
int tot = ;
for(int i = ; i <= n; i ++) {
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;
}
sort(G + , G + n + , Cmp);
sort(A + , A + tot + );
for(int i = ; i <= n; i ++) {
G[i].l = lower_bound(A + , A + tot + , G[i].l) - A;
G[i].r = lower_bound(A + , A + tot + , G[i].r) - A;
}
int R = , Answer = (int)1e9 + ;
for(int i = ; i <= n; i ++) {
if(R == n) break;
if(use[i] == ) {
Sec_G(, tot, , G[i].l, G[i].r, );
use[i] = ;
}
while(Max[] < m && R < n) {
R ++;
Sec_G(, tot, , G[R].l, G[R].r, );
use[R] = ;
if(Max[] >= m) {
Answer = min(Answer, G[i].len - G[R].len);
break;
}
}
if(use[i]) {
Sec_G(, tot, , G[i].l, G[i].r, -);
use[i] = ;
}
}
if(Answer == (int)1e9 + ) cout << "-1";
else cout << Answer;
return ;
}

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. 十九、eMMC驱动框架分析

    一.MMC简介 eMMC在封装中集成了一个控制器,提供标准接口并管理Nand Flash,使得手机厂商就能专注于产品开发的其它部分,并缩短向市场推出产品的时间. 对于我们来说,eMMC就是在Nand ...

  2. Spring的事件监听ApplicationListener

    ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成ApplicationContext的事件机制. 如果容器中存在Applica ...

  3. Luogu5307 [COCI2019] Mobitel 【数论分块】【递推】

    题目分析: 对于向上取整我们总有,$\lceil \frac{\lceil \frac{n}{a} \rceil}{b} \rceil = \lceil \frac{n}{a*b} \rceil$这个 ...

  4. Python中的单例模式的几种实现方式和优化以及pyc文件解释(转)

    原文:https://www.cnblogs.com/huchong/p/8244279.html 另一篇关于.pyc文件是什么?  原文: http://blog.sina.com.cn//s/bl ...

  5. POJ1322Chocolate--概论DP

    题目在这里 每次从包装中取出一块巧克力并放在桌子上.如果桌子上有两个相同颜色的巧克力,则将这两个丢掉.如果包中有C种颜色的巧克力(颜色均匀分布),从包装中取出N个巧克力后,桌子上确实有M个巧克力的概率 ...

  6. 【转载】C#使用as关键字将对象转换为指定类型

    在C#的编程开发过程中,很多时候涉及到数据类型的转换,可使用强制转换的方式,不过强制转换数据类型有时候会抛出程序异常错误,可以使用as关键字来进行类型的转换,如果转换成功将返回转换后的对象,如果转换不 ...

  7. Java 之 字符输出流[writer]

    一.字符输出流 java.io.Writer 抽象类是表示用于写出字符流的所有类的超类,将指定的字符信息写出到目的地.   它定义了字节输出流的基本共性功能方法. void write(int c) ...

  8. STM8 定时器

    中断映射表 对应stm8_interrupt.c #pragma vector=1 __interrupt void TRAP_IRQHandler(void) { } #pragma vector= ...

  9. Excel导入+写入数据库

    1.引用服务 2.前端 <h2>这里是上传Excel功能页面</h2> <div> <form action="/Improve_Excel/get ...

  10. 利用EasyUI 数据网格(DataGrid)的loader属性实现后端分页

    该属性在easyui官方文档中并没有详细阐述,通过简单的资料查询和摸索,实现了easyUI数据网格的后端分页功能. 在官网文档中这样阐述loader属性: 定义如何从远程服务器加载数据.返回false ...