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. runit git-daemon-run 等错误

    正在处理用于 man-db (2.7.5-1) 的触发器 ... 正在设置 runit (2.1.2-3ubuntu1) ... start: 无法连接到 Upstart: Failed to con ...

  2. Hive[6] HiveQL 查询

    6.1   SELECT ... FROM 语句    hive> SELECT name,salary FROM employees;    --普通查询 hive>SELECT e.n ...

  3. 在xampp修改密码

    1.选择 服务器--账号--修改密码 2.在密码 一栏输入新密码 3.刷新页面会得到如下页面 此时,该页面提醒我们检查配置文件中的主机.用户名和密码 4.打开配置文件 路径为 xampp -> ...

  4. Python线程间事件通知

    Python事件机制 事件机制:这是线程间最简单的通信机制:一个线程发送事件,其他线程等待事件事件机制使用一个内部的标志,使用set方法进行使能为True,使用clear清除为falsewait方法将 ...

  5. Delphi的Edit控件中只能输入数字且只能输入一个小数点

    使用这种功能必须使用 OnKeyPress 事件,该事件是在窗体中获得键盘输入的焦点,并且在用户按键时发生.OnKeyPress 事件中有个重要参数:Key.Key 参数为Char 型,它能够获得用户 ...

  6. 【杂题总汇】HDU-6406 Taotao Picks Apples

    [HDU 6406]Taotao Picks Apples 多校赛的时候多写了一行代码就WA了……找了正解对拍,在比赛结束后17分钟AC了

  7. Maven - 依赖范围<scope></scope>

    6种:

  8. django+xadmin在线教育平台(十二)

    6-4 用form实现登录-1 上面我们的用户登录的方法是基于函数来做的.本节我们做一个基于类方法的版本. 要求对类的继承有了解. 基础教程中基本上都是基于函数来做的,其实更推荐基于类来做.基于类可以 ...

  9. Go单元测试与基准测试

    Go单元测试 Go单元测试框架,遵循规则整理如下: 1.文件命名规则: 含有单元测试代码的go文件必须以_test.go结尾,Go语言测试工具只认符合这个规则的文件 单元测试文件名_test.go前面 ...

  10. cacti和nagios监控web平台搭建

    在linux的运维中对服务器的监控,时刻了解服务器的状态是确保服务能够正常允许的条件,linux的服务监控平台有很多, cacti 下面对cacti(仙人掌),一种比较流行的开源监控软件做安装配置 具 ...