F - Maximal Intersection --------暴力求解题
You are given n
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 0
in case the intersection is an empty set.
For example, the intersection of segments [1;5]
and [3;10] is [3;5] (length 2), the intersection of segments [1;5] and [5;7] is [5;5] (length 0) and the intersection of segments [1;5] and [6;6] is an empty set (length 0
).
Your task is to remove exactly one segment from the given sequence in such a way that the intersection of the remaining (n−1)
segments has the maximal possible length.
Input
The first line contains a single integer n
(2≤n≤3⋅105
) — the number of segments in the sequence.
Each of the next n
lines contains two integers li and ri (0≤li≤ri≤109) — the description of the i
-th segment.
Output
Print a single integer — the maximal possible length of the intersection of (n−1)
remaining segments after you remove exactly one segment from the sequence.
Examples
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
Note
In the first example you should remove the segment [3;3]
, the intersection will become [2;3] (length 1). Removing any other segment will result in the intersection [3;3] (length 0
).
In the second example you should remove the segment [1;3]
or segment [2;6], the intersection will become [2;4] (length 2) or [1;3] (length 2), respectively. Removing any other segment will result in the intersection [2;3] (length 1
).
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]
(length 7) if you remove the segment [1;5] or the intersection [1;5] (length 4) if you remove the segment [3;10].
题意:给出一些区间(或者点),求去除一个区间后,所共有的最大范围
题解:根据样例与自己画得出规律:所给区间最大值为右侧r的最小值减去左侧l的最大值。然后从第一个点循环求出最大的ans。如果去除点包含lmax,那么l取第二大的值,r同理
ac代码
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
struct k {
long long ll;
long long rr;
}fan[310000];
int l[310000],r[310000];
int main()
{
int n,l1,r1,al,ar;
int ans=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>fan[i].ll;
cin>>fan[i].rr;
l[i]=fan[i].ll;
r[i]=fan[i].rr;
}
sort(l,l+n);
sort(r,r+n);
for(int i=0;i<n;i++)
{
if(fan[i].ll==l[n-1]) al=l[n-2];
else al=l[n-1];
if(fan[i].rr==r[0]) ar=r[1];
else ar=r[0];
ans=max(ans,ar-al);
}
cout<<ans<<endl;
return 0;
}
F - Maximal Intersection --------暴力求解题的更多相关文章
- CF1029C Maximal Intersection 暴力枚举
Maximal Intersection time limit per test 3 seconds memory limit per test 256 megabytes input standar ...
- 2n皇后问题-------递归 暴力求解题与分布讨论题
问题描述 给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋盘中放入n个黑皇后和n个白皇后,使任意的两个黑皇后都不在同一行.同一列或同一条对角线上,任意的两个白皇后都不在同一行.同一列或同一 ...
- Codeforces Round #506 (Div. 3) C. Maximal Intersection
C. Maximal Intersection time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- Blue Jeans---poj3080(kmp+暴力求子串)
题目链接:http://poj.org/problem?id=3080 题意就是求n个长度为60的串中求最长公共子序列(长度>=3):如果有多个输出字典序最小的: 我们可以暴力求出第一个串的所有 ...
- Java【基础学习】之暴力求素数【用数组返回】
Java[基础学习]之暴力求素数[用数组返回] */ import java.util.*; public class Main{ public static void main(String[] a ...
- hdu 4291(矩阵+暴力求循环节)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4291 思路:首先保留求出循环节,然后就是矩阵求幂了. #include<iostream> ...
- UVA725 Division (暴力求解法入门)
uva 725 Division Write a program that finds and displays all pairs of 5-digit numbers that between t ...
- 2019牛客多校第二场F Partition problem 暴力+复杂度计算+优化
Partition problem 暴力+复杂度计算+优化 题意 2n个人分成两组.给出一个矩阵,如果ab两个在同一个阵营,那么就可以得到值\(v_{ab}\)求如何分可以取得最大值 (n<14 ...
- poj 2187 Beauty Contest (凸包暴力求最远点对+旋转卡壳)
链接:http://poj.org/problem?id=2187 Description Bessie, Farmer John's prize cow, has just won first pl ...
随机推荐
- django 中间键 csrf 跨站请求伪造
django中间件和auth模块 Django中间件 由django的生命周期图我们可以看出,django的中间件就类似于django的保安,请求一个相应时要先通过中间件才能到达django后端( ...
- Spring Boot2+Resilience4j实现容错之Bulkhead
Resilience4j是一个轻量级.易于使用的容错库,其灵感来自Netflix Hystrix,但专为Java 8和函数式编程设计.轻量级,因为库只使用Vavr,它没有任何其他外部库依赖项.相比之下 ...
- 四. sql上线平台
一.inception安装使用 inception是一个集审核.执行.备份及生成回滚语句于一身的MySQL自动化运维工具 [root@CentOS ~]# [root@CentOS ~]# wget ...
- SqueezeNet/SqueezeNext简述 | 轻量级网络
SqueezeNet系列是比较早期且经典的轻量级网络,SqueezeNet使用Fire模块进行参数压缩,而SqueezeNext则在此基础上加入分离卷积进行改进.虽然SqueezeNet系列不如Mob ...
- SpringBoot启动源码及自定义starter
为什么springboot工程能够在mian方法中完成启动呢?需要大家掌握的有几个点:1.SPISPI在springboot中是去读取META-INF/spring.factories目录的配置文件内 ...
- Azure Data Box
一,引言 最近博主又要面临考试了,随笔又再次缓慢更新,说起考试,之前在微软的 ms learn的课程上有接触到一个叫 Azure Data Box的,刚好今天也花了一个多小时看了一下相关文档,下面就正 ...
- 极致Web性能 —— SPA性能指南
前言 前端框架时代,为开发体验.效率与页面性能带来,非常大的革命.大家纷纷拿起一系列打包工具(webpack/parcel etc.),配合一系列加载器快速搭建起一个 SPA 页面. SPA 应用带来 ...
- CSS/CSS3常用的样式兼容,样式总结
这篇文章这篇文章主要介绍了css中常用但是又难记的样式作为总结,方便大家学习和使用.包括了‘单行缩略号‘.’css圆角兼容'.‘元素阴影’,‘border取消宽度影响’,‘css3的背景渐变’,‘cs ...
- Bash的特性
Bash的特性 1. tab补全 #命令补全 [root@clf ~]# user #命令补全useradd userdel usermod usernetctl user ...
- day16 本日作业+周末作业
目录 1.编写计数器功能,要求调用一次在原有的基础上加1 2.周末作业 1.编写计数器功能,要求调用一次在原有的基础上加1 def func(): x=0 def counter(): nonloca ...