Anton 上课题

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.

Examples:

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.

题目大意:Anton要去上两个课程,但是每个课程都有多种选择,问你哪种选择可以使Anton的休息时间更长,并求出休息时间,假如所有的课程都出现了时间重叠,那么输出答案-1。
解题思路:我们只需要找出第一个课程的最早结束时间和第二个课程的最晚结束时间然后相减就得出了答案,因为我们并不知道先上哪个课程最好,所以我们把两种情况都算出来然后比较最优解就行了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std;
const int MAX=;
int n,m; struct De
{
int a,b;
};
De xang[MAX],bian[MAX]; bool cmp(De x,De y)
{
return x.b>y.b;
} bool cep(De x,De y)
{
return x.a>y.a;
} int main()
{
scanf("%d",&n);
int ans1,ans2;
for(int i=;i<n;i++)
scanf("%d %d",&xang[i].a,&xang[i].b);
scanf("%d",&m);
for(int i=;i<m;i++)
scanf("%d %d",&bian[i].a,&bian[i].b);
sort(xang,xang+n,cmp);
sort(bian,bian+m,cep);
ans1=bian[].a-xang[n-].b;
sort(xang,xang+n,cep);
sort(bian,bian+m,cmp);
ans2=xang[].a-bian[m-].b;
if(ans1<=&&ans2<=) printf("0\n");
else printf("%d\n",ans1>ans2?ans1:ans2);
return ;
}

 

Anton 上课题的更多相关文章

  1. 【腾讯Bugly干货分享】OCS——史上最疯狂的iOS动态化方案

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/zctwM2Wf8c6_sxT_0yZvXg 导语 在 ...

  2. 年终巨献 史上最全 ——LINQ to SQL语句

    LINQ to SQL语句(1)之Where 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句.Where操 ...

  3. JVM 平台上的各种语言的开发指南

    JVM 平台上的各种语言的开发指南 为什么我们需要如此多的JVM语言? 在2013年你可以有50中JVM语言的选择来用于你的下一个项目.尽管你可以说出一大打的名字,你会准备为你的下一个项目选择一种新的 ...

  4. 写在MongoCola在Github上获得200个Star之后

    MongoCola MongoCola是一个开源的MongoDB管理工具. 由于最初版本的设计失误,所以现在只能在Windows上使用,虽然可以在OSX上使用,但是非常别扭. 契机 MongoCola ...

  5. DB层面上的设计 分库分表 读写分离 集群化 负载均衡

    第1章  引言 随着互联网应用的广泛普及,海量数据的存储和访问成为了系统设计的瓶颈问题.对于一个大型的 互联网应用,每天几十亿的PV无疑对数据库造成了相当高的负载.对于系统的稳定性和扩展性造成了极大的 ...

  6. 对话机器学习大神Yoshua Bengio(上)

    Yoshua Bengio教授(个人主页)是机器学习大神之一,尤其是在深度学习这个领域.他连同Geoff Hinton老先生以及 Yann LeCun(燕乐存)教授,缔造了2006年开始的深度学习复兴 ...

  7. Spark Streaming揭秘 Day20 动态Batch size实现初探(上)

    Spark Streaming揭秘 Day20 动态Batch size实现初探(上) 今天开始,主要是通过对动态Batch size调整的论文的解析,来进一步了解SparkStreaming的处理机 ...

  8. SaaS系列介绍之六: SaaS模式分析(上)

    1 引言 如果我们想要更多的玫瑰花,就必须种植更多的玫瑰树.                            ________姚群<成功激励格言精选> SaaS模式是个新兴的话题,有 ...

  9. 在MVC中利用uploadify插件实现上传文件的功能

    趁着近段的空闲时间,开发任务不是很重,就一直想把以前在仓促时间里所写的多文件上传功能改一下,在网上找了很多例子,觉得uploadify还可以,就想用它来试试.实现自己想要的功能.根据官网的开发文档,同 ...

随机推荐

  1. Android 开发 知晓各种id信息 获取线程ID、activityID、内核ID

    /** * Returns the identifier of this process's user. * 返回此进程的用户的标识符. */ Log.e(TAG, "Process.myU ...

  2. [UnityShader基础]05.模板测试

    参考链接: https://blog.csdn.net/u011047171/article/details/46928463 https://blog.csdn.net/JohnBlu/articl ...

  3. JavaScript学习-1

    本章目录: --------①数据类型. --------②定义变量. --------③类型转换. --------④运算符. --------⑤比较符. --------⑥if语句. ------ ...

  4. 深度学习原理与框架-递归神经网络-时间序列预测(代码) 1.csv.reader(进行csv文件的读取) 2.X.tolist(将数据转换为列表类型)

    1. csv.reader(csvfile) # 进行csv文件的读取操作 参数说明:csvfile表示已经有with oepn 打开的文件 2. X.tolist() 将数据转换为列表类型 参数说明 ...

  5. git的简单命令

    git pull 拉下最新版本. git add . git commit 输入提交信息 esc返回 :wq保存 git push .git保存着当前仓库的信息.git bash here的时候,要确 ...

  6. 安装Caffe纪实

    第一章 引言 在ubuntu16.04安装caffe,几乎折腾了一个月终于成功;做一文章做纪要,以便日后查阅.总体得出的要点是:首先,每操作一步,必须知道如何检验操作的正确性;笔者的多次失误是因为配置 ...

  7. Android Studio模拟器磁盘空间不足(Not enough disk space to run AVD)

    在Android Studio中运行模拟器时,提示Error: Not enough disk space to run AVD '....'. Exiting.是说安装模拟的磁盘空间不足,导致无法运 ...

  8. TXLSReadWriteII5 单元格读写

    unit Main; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...

  9. mysql创建索引以及对索引的理解

    创建表的时候创建索引   创建索引是指在某个表的一列或多列上建立一个索引,以便提高对表的访问速度.创建索引有3种方式,这3种方式分别是创建表的时候创建索引.在已经存在的表上创建索引和使用ALTER T ...

  10. Java中的包装数据类型

    基本类型 包装器类型 boolean Boolean char Character int Integer byte Byte short Short long Long float Float do ...