Codeforces Round #404 (Div. 2) B. Anton and Classes 水题
題目連接:
http://codeforces.com/contest/785/problem/B
Description
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.
Sample Input
3
1 5
2 6
2 3
2
2 4
6 8
Sample Output
3
Hint
題意
給你n個區間,再給你m個區間,然后從上面區間選出來一個區間,從下面選出來一個區間。
然后問你在這兩個區間選出兩個點來,這兩個點的最近距離是多少。
題解:
顯然最遠的就是左邊界減去右邊界的這種情況。
如果都為負數,那么就說明答案顯然為0.
代碼
#include<bits/stdc++.h> using namespace std;int l1,r1,l2,r2; int n,m,a,b; int main(){cin>>n;for(int i=0;i<n;i++){cin>>a>>b;if(i==0)l1=a,r1=b;else{l1=max(a,l1);r1=min(b,r1);}}cin>>m;for(int i=0;i<m;i++){cin>>a>>b;if(i==0)l2=a,r2=b;else{l2=max(a,l2);r2=min(b,r2);}}cout<<max(0,max(l1-r2,l2-r1))<<endl; }轉載于:https://www.cnblogs.com/qscqesze/p/6564263.html
總結
以上是生活随笔為你收集整理的Codeforces Round #404 (Div. 2) B. Anton and Classes 水题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python使用urllib模块开发的多
- 下一篇: 基于JAVA实现的排序算法总结