http://codeforces.com/problemset/problem/785/B Anton and Classes

本文介绍了一种简单的区间比较算法,该算法用于找出两组区间中,每个区间的右端点最小值与左端点最大值之间的差距,并输出较大差距值。通过记录每组区间内的左端点最小值和右端点最大值,实现快速比较。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录两个组内区间l值的最小最大值,r值得最小最大值即可。

#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#define MAX 100005
#define mod 998244353ll
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
int main() {
    freopen("a.txt", "r", stdin);
    freopen("b.txt", "w", stdout);
    int n, m, l, r, a1, b1, a2, b2;
    cin >> n;
    a1 = b2 = INF, b1 = a2 = -1;
    for (int i = 0; i < n; ++i) {
        scanf("%d%d", &l, &r);
        a1 = min(a1, r);
        a2 = max(a2, l);
    }
    cin >> m;
    for (int i = 0; i < m; ++i) {
        scanf("%d%d", &l, &r);
        b1 = max(b1, l);
        b2 = min(b2, r);
    }
    if (b1 - a1 > a2 - b2) {
        if (b1 - a1 > 0)cout << b1 - a1 << endl;
        else cout << 0 << endl;
    }
    else {
        if (a2 - b2 > 0) cout << a2 - b2 << endl;
        else cout << 0 << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值