http://codeforces.com/contest/1029/problem/C

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.

Input

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.

Output

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.

Examples
input

Copy
4
1 3
2 6
0 4
3 3
output

Copy
1
input

Copy
5
2 6
1 3
0 4
1 20
0 4
output

Copy
2
input

Copy
3
4 5
1 2
9 20
output

Copy
0
input

Copy
2
3 10
1 5
output

Copy
7

代码:

#include <bits/stdc++.h>
using namespace std; #define inf 0x3f3f3f3f
const int maxn = 300010 + 10;
int N; struct Node {
int l;
int r;
}S[maxn], Q[maxn], A[maxn]; int main() {
scanf("%d", &N);
S[0].r = inf, S[0].l = -inf;
for(int i = 1; i <= N; i ++) {
scanf("%d%d", &A[i].l, &A[i].r);
S[i].l = max(S[i - 1].l, A[i].l);
S[i].r = min(S[i - 1].r, A[i].r);
} Q[N + 1].r = inf, Q[N + 1].l = -inf;
for(int i = N; i >= 1; i --) {
Q[i].l = max(A[i].l, Q[i + 1].l);
Q[i].r = min(A[i].r, Q[i + 1].r);
} int ans = 0;
for(int i = 1; i <= N; i ++) {
ans = max(ans, (min(Q[i + 1].r, S[i - 1].r) - max(Q[i + 1].l, S[i - 1].l)));
}
printf("%d\n", ans);
return 0;
}

  

CodeForces C. Maximal Intersection的更多相关文章

  1. 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 ...

  2. CF1029C Maximal Intersection 暴力枚举

    Maximal Intersection time limit per test 3 seconds memory limit per test 256 megabytes input standar ...

  3. Codeforces | CF1029C 【Maximal Intersection】

    论Div3出这样巨水的送分题竟然还没多少人AC(虽说当时我也没A...其实我A了D...逃) 这个题其实一点都不麻烦,排序都可以免掉(如果用\(priority \_ queue\)的话) 先考虑不删 ...

  4. codeforces 803C Maximal GCD(GCD数学)

    Maximal GCD 题目链接:http://codeforces.com/contest/803/problem/C 题目大意: 给你n,k(1<=n,k<=1e10). 要你输出k个 ...

  5. Codeforces 803C. Maximal GCD 二分

    C. Maximal GCD time limit per test: 1 second memory limit per test: 256 megabytes input: standard in ...

  6. Codeforces 340B - Maximal Area Quadrilateral (计算几何)

    Codeforces Round #198 (Div. 2) 题目链接:Maximal Area Quadrilateral Iahub has drawn a set of \(n\) points ...

  7. F - Maximal Intersection --------暴力求解题

    You are given n segments on a number line; each endpoint of every segment has integer coordinates. S ...

  8. Codeforces 803C. Maximal GCD

    题目链接:http://codeforces.com/contest/803/problem/C 中了若干trick之后才过... k个数的严格递增序列最小权值和就是${n*(n+1)/2}$,枚举这 ...

  9. 【数学】codeforces C. Maximal GCD

    http://codeforces.com/contest/803/problem/C [题意] 给定两个数n,k(1 ≤ n, k ≤ 10^10) 要你输出k个数,满足以下条件: ①这k个数之和等 ...

随机推荐

  1. File类,递归

    File类 File文件和目录路径名的抽象表示形式.即,Java中把文件或者目录(文件夹)都封装成File对象. File类包含     路径    path E:\...     目录 direct ...

  2. filter 过滤器从数组中选择一个子集

    输入过滤器可以通过一个管道字符和一个过滤器添加到指令中,该过滤器后面跟着一个冒号:冒号后面是一个模型名称. <!DOCTYPE html><html><head>& ...

  3. Java 单词 day seven

    Constructor Constructor Constructor Constructor Constructor Constructor Constructor Constructor Cons ...

  4. 内置函数SQLCODE和SQLERRM的使用

    由于ORACLE的错信息最大长度是512字节,为了得到完整的错误提示信息,我们可用 SQLERRM 和 SUBSTR 函数一起得到错误提示信息,方便进行错误,特别是如果WHEN OTHERS异常处理器 ...

  5. html和node.js实现websocket

    websocket websocket是HTML5开始提供的一种单个TCP连接上进行全双工通讯的协议.它让客户端和服务端之间的数据交换变得更加简单,允许服务端主动向客户端推送数据.浏览器和服务器只需要 ...

  6. 洛谷P1437 [HNOI2004]敲砖块(dp)

    题目背景 无 题目描述 在一个凹槽中放置了 n 层砖块.最上面的一层有n 块砖,从上到下每层依次减少一块砖.每块砖 都有一个分值,敲掉这块砖就能得到相应的分值,如下图所示. 14 15 4 3 23 ...

  7. 动态代理和AOP

    之前说过了我对IOC的理解,这篇文章说以下我对动态代理和基本的对AOP的理解. 所谓动态代理就是,在运行时,动态创建实现了一组指定接口的实现类对象. 比如有: interface A { } inte ...

  8. Effective Approaches to Attention-based Neural Machine Translation(Global和Local attention)

    这篇论文主要是提出了Global attention 和 Local attention 这个论文有一个译文,不过我没细看 Effective Approaches to Attention-base ...

  9. Poweroj:来自学长的善意:ZQ的杀龙之旅(状压BFS)

    传送门:https://www.oj.swust.edu.cn/problem/show/2794 来自学长的善意:ZQ的杀龙之旅 Time Limit: 15000 MS Memory Limit: ...

  10. 30-RoutingMiddleware介绍以及MVC引入

    1-构建路由 public class Startup { // This method gets called by the runtime. Use this method to add serv ...