Anton and Classes

Anton likes to play chess. Also he likes to do programming. No wonder that he decided to attend chess classes and programming classes.

Anton has n variants when he will attend chess classes, i-th variant is given by a period of time (l1, i, r1, i). Also he has m variants when he will attend programming classes, i-th variant is given by a period of time (l2, i, r2, i).

Anton needs to choose exactly one of n possible periods of time when he will attend chess classes and exactly one of m possible periods of time when he will attend programming classes. He wants to have a rest between classes, so from all the possible pairs of the periods he wants to choose the one where the distance between the periods is maximal.

The distance between periods (l1, r1) and (l2, r2) is the minimal possible distance between a point in the first period and a point in the second period, that is the minimal possible |i - j|, where l1 ≤ i ≤ r1 and l2 ≤ j ≤ r2. In particular, when the periods intersect, the distance between them is 0.

Anton wants to know how much time his rest between the classes will last in the best case. Help Anton and find this number!

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of time periods when Anton can attend chess classes.

Each of the following n lines of the input contains two integers l1, i and r1, i (1 ≤ l1, i ≤ r1, i ≤ 109) — the i-th variant of a period of time when Anton can attend chess classes.

The following line of the input contains a single integer m (1 ≤ m ≤ 200 000) — the number of time periods when Anton can attend programming classes.

Each of the following m lines of the input contains two integers l2, i and r2, i (1 ≤ l2, i ≤ r2, i ≤ 109) — the i-th variant of a period of time when Anton can attend programming classes.

Output

Output one integer — the maximal possible distance between time periods.

Example

Input
3
1 5
2 6
2 3
2
2 4
6 8
Output
3
Input
3
1 5
2 6
3 7
2
2 4
1 4
Output
0

Note

In the first sample Anton can attend chess classes in the period (2, 3) and attend programming classes in the period (6, 8). It's not hard to see that in this case the distance between the periods will be equal to 3.

In the second sample if he chooses any pair of periods, they will intersect. So the answer is 0.

给两个事件各自能够在哪些时间段去完成;

让你选择两个时间段来完成这两件事情;

要求两段时间的间隔最长(休息的时间最长);

只需要贪心计算即可,如果a事件先b事件后,那么用b的最大开始时间减去a的最小结束时间。

同理,如果b事件先a事件后,那么用a的最大开始时间减去b的最小结束时间。

判断两者谁更大,如果都是负数,则输出0(没有结果)。

 1 #include<bits/stdc++.h>
2 using namespace std;
3 int main()
4 {
5 int n;
6 cin>>n;
7 int a1=0,b1=2000000000;
8 int a2=0,b2=2000000000;
9 for(int i=0;i<n;i++)
10 {
11 int a,b;
12 cin>>a>>b;
13 if(a>a1) a1=a;
14 if(b<b1) b1=b;
15 }
16 int m;
17 cin>>m;
18 for(int i=0;i<m;i++)
19 {
20 int a,b;
21 cin>>a>>b;
22 if(a>a2) a2=a;
23 if(b<b2) b2=b;
24 }
25 int ans=max(a1-b2,a2-b1);
26 if(ans>0) cout<<ans;
27 else
28 cout<<"0";
29 return 0;
30 }

CodeForce-785B Anton and Classes(简单贪心)的更多相关文章

  1. CodeForces 785B Anton and Classes

    简单判断. 找第一类区间中$R$最大的,以及第二类区间中$L$最小的,判断距离. 找第二类区间中$R$最大的,以及第一类区间中$L$最小的,判断距离. 两种情况取个最大值即可. #include &l ...

  2. CF 628C --- Bear and String Distance --- 简单贪心

    CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...

  3. Uva 11729 Commando War (简单贪心)

    Uva 11729  Commando War (简单贪心) There is a war and it doesn't look very promising for your country. N ...

  4. Codeforces Round #404 (Div. 2) B. Anton and Classes 水题

    B. Anton and Classes 题目连接: http://codeforces.com/contest/785/problem/B Description Anton likes to pl ...

  5. CDOJ 1502 string(简单贪心)

    题目大意:原题链接 相邻两个字母如果不同,则可以结合为前一个字母,如ac可结合为a.现给定一个字符串,问结合后最短可以剩下多少个字符串 解体思路:简单贪心 一开始读题时,就联想到之前做过的一道题,从后 ...

  6. ACM_发工资(简单贪心)

    发工资咯: Time Limit: 2000/1000ms (Java/Others) Problem Description: 作为广财大的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日 ...

  7. ACM_Ruin of Titanic(简单贪心)

    Ruin of Titanic Time Limit: 2000/1000ms (Java/Others) Problem Description: 看完Titanic后,小G做了一个梦.梦见当泰坦尼 ...

  8. CF #404 (Div. 2) B. Anton and Classes (贪心)

    题意:有一个小朋友,即喜欢下象棋,还喜欢编程,于是他打算上这两种课的兴趣班,这两种课有着不同的上课时间,他想让两堂课之间的休息时间最多,问最大时间是多少 思路:看到这道题的第一反应就是贪心,于是用结构 ...

  9. 【codeforces 785B】Anton and Classes

    [题目链接]:http://codeforces.com/contest/785/problem/B [题意] 给你两个时间各自能够在哪些时间段去完成; 让你选择两个时间段来完成这两件事情; 要求两段 ...

随机推荐

  1. Linux文件系统只读 解决方案:

    Linux系统Read-only file system,文件系统只读排查解决方案:文件系统只读机制:当文件系统自身的校验机制发现文件系统存在问题时,为避免文件系统受到进一步的损坏,系统会把文件系统设 ...

  2. VBA·Function的基础使用

    阅文时长 | 0.27分钟 字数统计 | 440字符 主要内容 | 1.引言&背景 2.基本结构 3.Demo示例 4.声明与参考资料 『VBA·Function的基础使用』 编写人 | SC ...

  3. Sqli-Labs less20-22

    less-20 第20关成功登陆之后会产生一个cookie,作为下次登陆的凭证(可以用于登陆其他人的qq空间) 这里我依然使用burp suite,其实火狐和谷歌上有许多插件可以改http头和cook ...

  4. 优化 SQL 语句的步骤

    优化 SQL 语句的步骤 1.分析MySQL服务器当前的状态信息 SHOW SESSION STATUS; SHOW SESSION STATUS LIKE 'Com_%' //当前会话下所有语句类型 ...

  5. DOS创建/删除/描述 windows服务

    1.以管理员运行cmd ,输入 sc create test binPath= 程序路径\xxx.exe,主要 "="后面必须要空格 在服务里面查看结果 2.设置服务格式: sc ...

  6. 12.SpringMVC之拦截器

    1.拦截器概述 1.1 什么是拦截器? Spring MVC中的拦截器(Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户请求并作相应的处理.例如通过拦截器可以 ...

  7. Map 综述(一):彻头彻尾理解 HashMap

    转载自:https://blog.csdn.net/justloveyou_/article/details/62893086 摘要: HashMap是Map族中最为常用的一种,也是 Java Col ...

  8. Mybatis一对一、一对多、多对多查询。+MYSQL

    场景:使用三张数据表:student学生表.teacher教师表.position职位表 一个学生可以有多为老师.一位老师可以有多个学生.但是一个老师只能有一个职位:教授.副教授.讲师:但是一个职位可 ...

  9. 【CSS复合选择器、元素显示模式、背景】前端小抄(3) - Pink老师自学笔记

    [CSS复合选择器.元素显示模式.背景]前端小抄(3) 本学习笔记是个人对 Pink 老师课程的总结归纳,转载请注明出处! 一.CSS的复合选择器 1.1 什么是复合选择器 在 CSS 中,可以根据选 ...

  10. sychronized

    说一下 synchronized 底层实现原理? synchronized可以保证方法或者代码块在运行时, 同一时刻只有一个方法可以进入到临界区, 同时它还可以保证共享变量的内存可见性. Java中每 ...