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 ( ...
随机推荐
- JQuery EasyUI框架
1. JQuery EasyUI框架概述 1.1. JQuery EasyUI是什么东西 答:JQuery EasyUI就是一套基础JQuery的富客户端的UI框架.像这些将常用的控件封装成一个UI ...
- 单选按钮(CheckBox)
import React, { useState, useEffect } from 'react' import PropTypes from 'prop-types' import _ from ...
- 手写PE结构解析工具
PE格式是 Windows下最常用的可执行文件格式,理解PE文件格式不仅可以了解操作系统的加载流程,还可以更好的理解操作系统对进程和内存相关的管理知识,而有些技术必须建立在了解PE文件格式的基础上,如 ...
- vue 写一个炫酷的轮播图
效果如上图: 原理: 1.利用css 的 transform 和一些其他的属性,先选五张将图片位置拍列好,剩余的隐藏 2.利用 js 动态切换类名,达到切换效果 css代码如下 .swiper-cer ...
- 在论坛中出现的比较难的sql问题:42(动态行转列 考勤时间动态列)
原文:在论坛中出现的比较难的sql问题:42(动态行转列 考勤时间动态列) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.
- 查看IIS错误日志
部署在IIS中的程序,难免出现数据产生异常 在事件查看器中,可以看出来具体的错误信息,代码定位
- 如何通过wlst部署应用程序到weblogic12c上
适用版本 Oracle WebLogic Server - Version 10.3 and laterInformation in this document applies to any plat ...
- Git Git 已被其他开发删除的远程分支,本地依旧显示,如何删除?
- Android笔记(十三) Android中的基本组件——文本
Android中常用的文本组件有 普通文本框(TextView)和编辑框(EditText)两种 EditText是TextView的子类,作用就是在界面上显示文本,区别是EditText允许用户编辑 ...
- javascript之ECMAScript:语法的操作标准
一.如何书写一个javascript代码 javascript代码需要写在javascript标签中才会生效,而javascript标签可以写在任何地方,但考虑到规范化及页面的加载问题,最好是写在bo ...