区间

链接

题意:给定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. C# DataGridview用NPOI导出Excel文件

    导出excel我用的是nuget 的NPOI,直接在项目中添加的,引用到项目中,下面是截图: 下面我把ExcelHelper贴出来 public static class ExcelHelper { ...

  2. [翻译] UCZProgressView

    UCZProgressView UCZProgressView is a circular progress indicator with cool animations for image load ...

  3. 使用linux远程登录另一台linux

    可以用ssh命令行方式登录.对方需要开启ssh服务.   ssh [-l login_name] [-p port] [user@]hostname   例如,使用root用户登录 192.168.0 ...

  4. EditPlus 自动格式化js、html、css,以EditPlus 文本编辑器v3.41(1145)为例

    工具/原料   edtools.rar 方法/步骤     下载工具包:edtools.rar ,解压后放到磁盘的一个目录,如D:/edTools   打开ED,打开“工具”-“配置用户工具”,在弹出 ...

  5. python-面向过程编程

    面向过程: 核心是过程.过程指的是解决问题的步骤,设计一条流水线,机械式的思维方式. 优点:复杂的问题流程化,进而简单化

  6. PHP常用符号和函数

    (转)最近在写PHP程序的时候发现了一些特殊的PHP符号,例如连续小于符号,三个小于符号,eot,eod,echo示例,print示例等,突然间 发现用这么久的PHP了,竟然连PHP的基本符号都没有认 ...

  7. python第二十二课——list函数

    演示list类型中常用的一些函数: 1.append(obj):将obj元素追加到列表的末尾 lt=['路费','佐罗','山治','乔巴','乌索普','纳米桑'] #append(): lt.ap ...

  8. HDU5343:MZL's Circle Zhou(SAM,记忆化搜索DP)

    Description Input Output Sample Input Sample Output Solution 题意:给你两个串,分别从两个里面各选出一个子串拼到一起,问能构成多少个本质不同 ...

  9. programming-languages学习笔记--第9部分

    programming-languages学习笔记–第9部分 */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre.src ...

  10. 20155314 2016-2017-2 《Java程序设计》实验一 Java开发环境的熟悉(macOS + IDEA)

    20155314 2016-2017-2 <Java程序设计>实验一 Java开发环境的熟悉(macOS + IDEA) 实验内容 使用JDK编译.运行简单的Java程序: 使用IDEA ...