Codeforces Round #506 (Div. 3) C. Maximal Intersection
3 seconds
256 megabytes
standard input
standard output
You are given nn segments on a number line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.
The intersection of a sequence of segments is such a maximal set of points (not necesserily having integer coordinates) that each point lies within every segment from the sequence. If the resulting set isn't empty, then it always forms some continuous segment. The length of the intersection is the length of the resulting segment or 00 in case the intersection is an empty set.
For example, the intersection of segments [1;5][1;5] and [3;10][3;10] is [3;5][3;5] (length 22), the intersection of segments [1;5][1;5] and [5;7][5;7] is [5;5][5;5](length 00) and the intersection of segments [1;5][1;5] and [6;6][6;6] is an empty set (length 00).
Your task is to remove exactly one segment from the given sequence in such a way that the intersection of the remaining (n−1)(n−1)segments has the maximal possible length.
The first line contains a single integer nn (2≤n≤3⋅1052≤n≤3⋅105) — the number of segments in the sequence.
Each of the next nn lines contains two integers lili and riri (0≤li≤ri≤1090≤li≤ri≤109) — the description of the ii-th segment.
Print a single integer — the maximal possible length of the intersection of (n−1)(n−1) remaining segments after you remove exactly one segment from the sequence.
- 4
1 3
2 6
0 4
3 3
- 1
- 5
2 6
1 3
0 4
1 20
0 4
- 2
- 3
4 5
1 2
9 20
- 0
- 2
3 10
1 5
- 7
In the first example you should remove the segment [3;3][3;3], the intersection will become [2;3][2;3] (length 11). Removing any other segment will result in the intersection [3;3][3;3] (length 00).
In the second example you should remove the segment [1;3][1;3] or segment [2;6][2;6], the intersection will become [2;4][2;4] (length 22) or [1;3][1;3](length 22), respectively. Removing any other segment will result in the intersection [2;3][2;3] (length 11).
In the third example the intersection will become an empty set no matter the segment you remove.
In the fourth example you will get the intersection [3;10][3;10] (length 77) if you remove the segment [1;5][1;5] or the intersection [1;5][1;5] (length 44) if you remove the segment [3;10][3;10].
题意:给出n个区间,然后你可以删除一个区间,问你剩下的区间的交集的最大长度是多少
思路:我们回到原始的求所有区间的交集,其实就是 距离左边最近的右边界-距离右边最近的左边界
所以这个问题我们可以排个序,然后我们枚举要删除的边界,但是我们范围是10^5 n^2算法肯定不行,但我们又需要遍历,如果是nlogn的话肯定可以
这个时候我们可以考虑set,内有自动排序的功能,也是由红黑树组成优化了时间复杂度,但是说可能会记录重复的,那我们就要使用 multiset,和set的区别就在于能记录重复
然后我们枚举每个删除的区间即可
- #include <bits/stdc++.h>
- using namespace std;
- int l[], r[];
- multiset<int> a, b;
- int main(){
- int n;
- scanf("%d", &n);
- for(int i=;i<=n;i++){
- scanf("%d%d", &l[i], &r[i]);
- a.insert(l[i]);
- b.insert(r[i]);
- }
- int ans = ;
- for(int i=;i<=n;i++){
- a.erase(a.find(l[i]));
- b.erase(b.find(r[i]));
- ans = max(ans, *b.begin()-*a.rbegin());//取最大
- a.insert(l[i]);
- b.insert(r[i]);
- }
- printf("%d\n", ans);
- }
还有种 优先队列的写法更加快
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int main() {
- int n,l[],r[];
- priority_queue<int> ml, mr;
- scanf("%d", &n);
- for (int i=;i<n;++i) {
- scanf("%d %d", l+i, r+i);
- ml.push(l[i]);
- mr.push(-r[i]);
- }
- int ans = ;
- bool bl, br;
- for (int i=;i<n;++i) {
- bl = br = ;
- if (ml.top()==l[i]) {
- bl = ;
- ml.pop();
- }
- if (mr.top()==-r[i]) {
- br = ;
- mr.pop();
- }
- ans = max(ans, -mr.top()-ml.top());
- if (bl) ml.push(l[i]);
- if (br) mr.push(-r[i]);
- }
- printf("%d\n", ans);
- return ;
- }
Codeforces Round #506 (Div. 3) C. Maximal Intersection的更多相关文章
- Codeforces Round #506 (Div. 3) 题解
Codeforces Round #506 (Div. 3) 题目总链接:https://codeforces.com/contest/1029 A. Many Equal Substrings 题意 ...
- Codeforces Round #506 (Div. 3) D-F
Codeforces Round #506 (Div. 3) (中等难度) 自己的做题速度大概只尝试了D题,不过TLE D. Concatenated Multiples 题意 数组a[],长度n,给 ...
- Codeforces Round #506 (Div. 3) E
Codeforces Round #506 (Div. 3) E dfs+贪心 #include<bits/stdc++.h> using namespace std; typedef l ...
- Codeforces Round #506 (Div. 3) A-C
CF比赛题解(简单题) 简单题是指自己在比赛期间做出来了 A. Many Equal Substrings 题意 给个字符串t,构造一个字符串s,使得s中t出现k次;s的长度最短 如t="c ...
- Codeforces Round #198 (Div. 2) B. Maximal Area Quadrilateral
B. Maximal Area Quadrilateral time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces Round #547 (Div. 3) B.Maximal Continuous Rest
链接:https://codeforces.com/contest/1141/problem/B 题意: 给n个数,0代表工作,1代表休息,求能连续最大的休息长度. 可以连接首尾. 思路: 求普通连续 ...
- Codeforces Round #506 (Div. 3)
题解: div3水的没有什么意思 abc就不说了 d题比较显然的就是用hash 但是不能直接搞 所以我们要枚举他后面那个数的位数 然后用map判断就可以了 刚开始没搞清楚数据范围写了快速乘竟然被hac ...
- Codeforces Round #506 (Div. 3) D. Concatenated Multiples
D. Concatenated Multiples You are given an array aa, consisting of nn positive integers. Let's call ...
- Codeforces Round #506 (Div. 3) - D. Concatenated Multiples(思维拼接求是否为k的倍数)
题意 给你N个数字和一个K,问一共有几种拼接数字的方式使得到的数字是K的倍数,拼接:“234”和“123”拼接得到“234123” 分析: N <= 2e5,简单的暴力O(N^2)枚举肯定超时 ...
随机推荐
- centOS 6.5采用python+nginx+uwsgi实现爬金十财经日历
上一篇中有关于安装nginx.python.uwsgi的过程,这里不再重述.下面是有关在具体布署中的一些过程和问题处理 一.因为用到了bs4(BeautifulSoup)\paste\lxml所以这些 ...
- vm15安装MACOS
VMWare15 安装 Mac OS 系统文章目录VMWare15 安装 Mac OS 系统安装环境工具准备准备工作MAC虚拟机设置启动MAC前准备工作安装系统安装VMware Tool注意事项参考链 ...
- 【洛谷p1313】计算系数
(%%%hmr) 计算系数[传送门] 算法呀那个标签: (越来越懒得写辽)(所以今天打算好好写一写) 首先(ax+by)k的计算需要用到二项式定理: 对于(x+y)k,有第r+1项的系数为:Tr+1= ...
- java 字符串截取的几种方式(转)
众所周知,java提供了很多字符串截取的方式.下面就来看看大致有几种. 1.split()+正则表达式来进行截取. 将正则传入split().返回的是一个字符串数组类型.不过通过这种方式截取会有很大的 ...
- const typedef 和指针的问题(这里必须初始化的才初始化了,不必须的则没有初始化)
这里很容易搞混: tyepdef double dou;//这里是dou是double的别名 #include<iostream> using namespace std; int mai ...
- 基于jquery实现页面loading加载效果
实现loading 加载提示 ······ 透明遮罩 居中效果 具体代码如下: CSS样式部分: <style type="text/css"> .background ...
- hdu1569-方格取数-二分图网络流
方格取数(2) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- Beta阶段——第6篇 Scrum 冲刺博客
Beta阶段--第6篇 Scrum 冲刺博客 标签:软件工程 一.站立式会议照片 二.每个人的工作 (有work item 的ID) 昨日已完成的工作 人员 工作 林羽晴 完成了函数的编写,提供报表数 ...
- Linux五种IO模型(同步 阻塞概念)
Linux五种IO模型 同步和异步 这两个概念与消息的通知机制有关. 同步 所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回.比如,调用readfrom系统调用时,必须等待IO操 ...
- vsftp的安装与配置
1.安装 直接使用yum安装,如果没有网络在其他机器使用yum先离线下载即可,vsftpd一般就自己不需要装其他依赖包 rpm -qa|grep vsftpd #查看是否安装 yum install ...