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!
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 one integer — the maximal possible distance between time periods.
3 1 5 2 6 2 3 2 2 4 6 8
3
3 1 5 2 6 3 7 2 2 4 1 4
0
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.
思路:思维惯性把我带进了误区。因为B题看过了那么多人,就只看了样例,大致看了题目的意思。
就开始着手敲代码,但只考虑了先上棋课,再去程序课,其实反过来也可以的,大意了。。。。
题目大意是有一只小朋友很纠结,既喜欢棋,又喜欢程序,然后报了两种课程,有不同的上课时间。
但是小朋友只需一样上一节。问题来了,小朋友想在两节课之间休息时间足够多。若是没有休息时间,就输出0;
我没有用结构体排序,太麻烦。直接列举了我想要的两种情况。躺过。
代码:
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<list>
using namespace std;
typedef long long LL;
int main()
{
int n,m;
while(~scanf("%d",&n))
{
int x1,x2,y1,y2,x3,y3;
scanf("%d%d",&x1,&y1);
x3=x1,y3=y1;
for(int i=1;i<n;i++)
{
scanf("%d%d",&x2,&y2);
if(y2<y1) {x1=x2;y1=y2;}
if(x2>x3){ x3=x2; y3=y2;}
}
scanf("%d",&m);
int l1,l2,r1,r2,l3,r3;
scanf("%d%d",&l1,&r1);
l3=l1,r3=r1;
for(int i=1;i<m;i++)
{
scanf("%d%d",&l2,&r2);
if(l2>l1) {l1=l2; r1=r2;}
if(r2<r3) { r3=r2; l3=l2;}
}
int ans=max(l1-y1,x3-r3);
if(ans<0)
ans=0;
printf("%d\n",ans);
}
}

本文介绍了一个问题:如何为爱好下棋和编程的小朋友选择最佳的课程时间安排,以确保两节课之间的休息时间最大化。通过分析不同课程的时间段,提出了解决方案并提供了具体的实现代码。
5000

被折叠的 条评论
为什么被折叠?



