luogu 1712
按区间长度降序排序
维护区间指针 [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的更多相关文章
- luogu 1712 区间(线段树+尺取法)
题意:给出n个区间,求选择一些区间,使得一个点被覆盖的次数超过m次,最小的花费.花费指的是选择的区间中最大长度减去最小长度. 坐标值这么大,n比较小,显然需要离散化,需要一个技巧,把区间转化为半开半闭 ...
- Luogu 魔法学院杯-第二弹(萌新的第一法blog)
虽然有点久远 还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题 沉迷游戏,伤感情 #include <queue> ...
- luogu p1268 树的重量——构造,真正考验编程能力
题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...
- HDU 1712 裸分组dp
http://acm.hdu.edu.cn/showproblem.php?pid=1712 N门课M天复习,第i门课花费j天获得的效益是dp[i][j] 求最大效益 分组背包,同一门课不能选两次 三 ...
- [luogu P2170] 选学霸(并查集+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...
- [luogu P2647] 最大收益(贪心+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...
- HDU 1712 ACboy needs your help (分组背包模版题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 有n门课,和m天时间.每门课上不同的天数有不同的价值,但是上过这门课后不能再上了,求m天里的最大 ...
- hdu 1712 ACboy needs your help 分组背包
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 Problem ...
- HDU 1712 ACboy needs your help 典型的分组背包
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 ACboy needs your help Time Limit: 1000/1000 MS ( ...
随机推荐
- SAS学习笔记51 SAS数据集
结构 SAS数据集是关系型的,包含两个部分:描述部分(变量)和数据部分(观察值) 形式 SAS系统中共有两种类型的数据集: 1.SAS 数据文件(SAS datafiles) 2.SAS 数据视窗(S ...
- 编写Postgres扩展之一:基础
原文:http://big-elephants.com/2015-10/writing-postgres-extensions-part-i/ 编译:Tacey Wong Postgres提供了广泛的 ...
- Centos 配置eth0 提示Device does not seem to be present -- 转载
http://www.cnblogs.com/fbwfbi/archive/2013/04/29/3050907.html 移动虚拟机造成网卡无法识别 一.故障现象: [root@c1node01 ~ ...
- 【转】JRE和JDK的区别
用一幅图来宏观的看一下 从图中可以看出JDK包含JRE包含JVM. JDK:java development kit (java开发工具) JRE:java runtime environment ( ...
- stm32和sd卡
SD卡从容量上讲分两种:标准容量和大容量,最小的是标准容量,小于等于2G 其中的访问关系如下: SD卡分为两种模式:认证模式和传输模式,每一个模式包含着不同的状态,如下 以下主要讲其初始化过程: SD ...
- Apache老版本下载地址
有时候我们想做测试或者使用旧版本软件时,往往发现官方网站已经下架,以Apache为例: Apache旧版本下载地址:http://archive.apache.org/dist/httpd/ .... ...
- 第十三篇:socket网络编程
本篇主要介绍网络编程的基础,以及UDP/TCP网络的socket编程,关于UDP套接字聊天器的实现.以及基于TCP套接字的服务器/客户端的实现上传下载功能. 一.网络通信 关于网络通信即通过网络(介质 ...
- Linux下相关性能指标数据采集
1)如何查看某个服务进程所占本机物理内存及百分比情况?可以使用Linux命令行查看,也可以使用脚本实现.如下以查看本机kube-nginx服务进程为例: [root@kevin ~]# pidof k ...
- 十年种树----小白的起点save
大家好,给大家介绍一下我自己.各平台通用ID:琴鬼白羊,男,一个24岁学习采矿工程的在读研究僧,一个24岁还在想学习计算机的小白. 非洲经济学家Dambisa Moyo在他的<dead aid& ...
- HDU_2717_Catch That Cow
很短的 BFS 队列 HDU_2717_Catch That Cow #include<iostream> #include<algorithm> #include<cs ...