【codeforces 785B】Anton and Classes

【题目链接】:http://codeforces.com/contest/785/problem/B

【题意】

给你两个时间各自能够在哪些时间段去完成;
让你选择两个时间段来完成这两件事情;
要求两段时间的间隔最长(休息的时间最长)

【题解】

考虑最后的答案;
只会是

    (l,r).....(left,right)

    (left,right)...(l,r)

    else


这3种情况
对于第一种记录第一件事右端点的最小值,第二件事左端点的最大值;
对于第二种记录第二件事右端点的最小值,第一件事左端点的最大值;
用这两种情况尝试更新答案;
如果答案不为0,则输出它,否则剩下的情况都是相交的;直接输出0就好;

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 110;

int l, r, left, right,zdl=0,zxr=1e9,zdleft=0,zxright=1e9;
int n, m,x,y,ans = 0;

void input_data()
{
    rei(n);
    rep1(i, 1, n)
    {
        rei(x), rei(y);
        zxr = min(zxr, y);
        zdl = max(zdl, x);
    }
    rei(m);
    rep1(i, 1, m)
    {
        rei(x), rei(y);
        zxright = min(zxright, y);
        zdleft = max(zdleft, x);
    }
}

void get_ans()
{
    if (zxr <= zdleft)
        ans = max(ans, zdleft - zxr);
    if (zxright <= zdl)
        ans = max(ans, zdl - zxright);
}

void o()
{
    cout << ans << endl;
}

int main()
{
    //freopen("F:\rush.txt", "r", stdin);
    input_data();
    get_ans();
    o();
    //printf("
%.2lf sec 
", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626553.html