区间

链接

题意:给定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 区间的更多相关文章

  1. 做题记录 To 2019.2.13

    2019-01-18 4543: [POI2014]Hotel加强版:长链剖分+树形dp. 3653: 谈笑风生:dfs序+主席树. POJ 3678 Katu Puzzle:2-sat问题,给n个变 ...

  2. HDU 5316——Magician——————【线段树区间合并区间最值】

    Magician Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  3. ASP.NET Core应用针对静态文件请求的处理[2]: 条件请求与区间请求

    通过调用ApplicationBuilder的扩展方法UseStaticFiles注册的StaticFileMiddleware中间件帮助我们处理针对文件的请求.对于StaticFileMiddlew ...

  4. SQL Server 随机数,随机区间,随机抽取数据rand(),floor(),ceiling(),round(),newid()函数等

    在查询分析器中执行:select rand(),可以看到结果会是类似于这样的随机小数:0.36361513486289558,像这样的小数在实际应用中用得不多,一般要取随机数都会取随机整数.那就看下面 ...

  5. codevs 1082 线段树练习 3(区间维护)

    codevs 1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...

  6. codevs 1082 线段树区间求和

    codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...

  7. [LeetCode] Find Right Interval 找右区间

    Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...

  8. [LeetCode] Non-overlapping Intervals 非重叠区间

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

  9. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

随机推荐

  1. .net下log4net的使用

    这里以控制台应用程序为例 首先是要添加引用: 安装后可以看到项目中多了log4net的引用: 添加应用程序配置文件app.config,配置log4net <?xml version=" ...

  2. [UI] 精美UI界面欣赏[10]

    精美UI界面欣赏[10]

  3. Configuration Manager 和内容位置(包源文件)

    Configuration Manager 2007 中的内容位置涉及 Configuration Manager 2007 客户端如何查找播发和软件更新的包源文件.当客户端需要查找内容时,它会将内容 ...

  4. 我的第一个springboot应用+maven依赖管理

    第一步:使用Eclipse创建maven工程SpringBootFirst:工程目录如下 第二步:编写依赖文件pom.xml <project xmlns="http://maven. ...

  5. wrap装饰器包

    import wrapt # without argument in decorator @wrapt.decorator def logging(wrapped, instance, args, k ...

  6. libcurl-7.54.1附加zlib1.2.11的编译

    手上有个小程序需要通过HTTP协议通信,选择了出名的libcurl作为支持库.由于网上的教程多是命令行编译,本人记性不好,比较讨厌记住一堆命令,因此折腾了一天通过VS对libcurl和zlib进行了编 ...

  7. 团队作业1——团队展示&教辅宝

    1.队名:PHILOSOPHER 2.队员学号: [组长]金盛昌(201421122043).刘文钊(20142112255).陈笑林(201421122042). 张俊逸(201421122044) ...

  8. python第十六课——ascii码

    2.ascii码 美国设计出来的一张编码表,将涉及的字符都编号了,底层仍然还是进行二进制的运算: 记住:3个范围段 1).'0' --> 码值:48 2).'A' --> 码值:65 3) ...

  9. 关于前端惰性加载(jquery_lazyload)的使用和原理分析

    1.前言 有时我们会有这样的需求,当网页有很多张图片的时候,我们不希望一次性就把图片加载完,而是希望当浏览器滑动到指定位置的时候再加载,这样可以节省带宽,它也能帮助减轻服务器负载.那么这种需求就需要利 ...

  10. Day8 Servlet

    HttpServletRequest 说明 公共接口类HttpServletRequest继承自ServletRequest.客户端浏览器发出的请求被封装成为一个HttpServletRequest对 ...