zhengruioi 470 区间
区间
题意:给定n个区间[li,ri]。你可以选出任意一些区间,设选出的区间个数是s,[l,r]是这些区间的交,求min(s,r-l+1)的最大值。 N≤3×105
分析:首先按照左端点排序,然后依次加入每条线段。加入后判断min(s, r-l+1)哪个大。如果s大,那么说明答案受限制与区间交,所以可以考虑去掉一些线段,去掉右端点最小的(堆维护);如果s小,那么继续加入线段即可。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
struct Edge{
int l, r;
bool operator < (const Edge &A) const { return l < A.l; }
} A[N];
priority_queue<int, vector<int>, greater<int> > q; int main() {
int n = read();
for (int i = ; i <= n; ++i) A[i].l = read(), A[i].r = read();
sort(A + , A + n + );
int ans = , cnt = ;
q.push(A[].r);
for (int i = ; i <= n; ++i) {
cnt ++;
q.push(A[i].r); ;
ans = max(ans, min(cnt, q.top() - A[i].l + ));
while (!q.empty() && q.top() - A[i].l + < cnt) {
cnt --;
q.pop();
ans = max(ans, min(cnt, q.top() - A[i].l + ));
}
ans = max(ans, min(cnt, q.top() - A[i].l + ));
}
cout << ans;
return ;
}
zhengruioi 470 区间的更多相关文章
- 做题记录 To 2019.2.13
2019-01-18 4543: [POI2014]Hotel加强版:长链剖分+树形dp. 3653: 谈笑风生:dfs序+主席树. POJ 3678 Katu Puzzle:2-sat问题,给n个变 ...
- HDU 5316——Magician——————【线段树区间合并区间最值】
Magician Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- ASP.NET Core应用针对静态文件请求的处理[2]: 条件请求与区间请求
通过调用ApplicationBuilder的扩展方法UseStaticFiles注册的StaticFileMiddleware中间件帮助我们处理针对文件的请求.对于StaticFileMiddlew ...
- SQL Server 随机数,随机区间,随机抽取数据rand(),floor(),ceiling(),round(),newid()函数等
在查询分析器中执行:select rand(),可以看到结果会是类似于这样的随机小数:0.36361513486289558,像这样的小数在实际应用中用得不多,一般要取随机数都会取随机整数.那就看下面 ...
- codevs 1082 线段树练习 3(区间维护)
codevs 1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...
- codevs 1082 线段树区间求和
codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...
- [LeetCode] Find Right Interval 找右区间
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- [LeetCode] Non-overlapping Intervals 非重叠区间
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
- [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
随机推荐
- 关于Nodejs开发桌面应用。NW.js 和 Electron 优缺点分析对比
从开发角度来说,选择用 nw.js 还是 election ,区别其实不是很大.大部分工作还是在自己的 javascript 和 HTML 上.国内比较有名的,比如微信web开发工具.钉钉都是基于 n ...
- Session管理
request.session.set_expiry(10) #设置10s后session失效request.session.get_expire_at_browser_close() #查看sess ...
- 关于cocos2dx的textfield事件响应
time:2015/08/27 1. textfiled cocostudio的输入框,看源代码知道他是直接继承widget的,但是真正的输入框功能是label实现的,是Textfield类的一个成员 ...
- vs2012配置
ogre1.9 http://m.blog.csdn.net/blog/u013025310/20869871 http://www.cnblogs.com/zhouxin/p/4258849.htm ...
- November 18th 2016 Week 47th Friday
Get a livelihood and then practise virtue. 先谋生,而后修身. If you can't earn a life, all the things you ha ...
- multi_index_container
转自:https://blog.csdn.net/buptman1/article/details/38657807 multi_index_container: Boost Multi-index ...
- jQuery获取radio选中项checked的几种方法
如何知道radio哪个选项被选中了,也就是获取checked的radio项.这是个很简单的任务,这里我总结了几个常用的方法,体验一下jQuery的灵活. 具体的 HTML 和 jQuery 代码可以右 ...
- Java8新特性 -- Lambda基础语法
Lambda 表达式的基础语法: Java8引入了一个新的操作符 “->” 该操作符称为箭头操作符或Lambda操作符, 该操作符将Lambda表达式拆分为两部分: 左侧: Lambda表达式 ...
- 利用mpvue开发微信小程序
最近公司部门负责人提出需求需要开发一款微信小程序,由于本人之前是做前端开发的,对于小程序开发一窍不通,但是很多时候我们都是把不会做变成我会学.于是便在网上寻找小程序开发教程,相比于相生的小程序开发,本 ...
- C# 生成自签名CA证书
"; string signatureAlgorithm = "SHA1WithRSA"; // Generate RSA key pair var rsaGenerat ...