Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A. Math Problem

[Link title] A problem link

[Title] analog type

[Title] to the effect that you n segment interval requires you to solve, solving one of the least satisfying answer to this interval and this interval n intervals have at least one thing in common

[Problem-solving ideas]
can look at the pictures
Here Insert Picture Description
of these two segments in fact only [R1, R1] on it

Here Insert Picture Description
This situation should want to answer a minimum interval, then choose the minimum and maximum of the right to the left (which is the most critical thinking)
and your last to ansl value than the value ansr to take, it shows the first one Figure that case, is that there is then a point just in these sections, so you can direct the output 0

/**
 *    This code has been written by YueGuang, feel free to ask me question. Blog: http://www.yx.telstudy.xyz
 *    created:
 */
#include <cstdio>
#include <iostream>
#include <set>
#include <map>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#define REP(i, a, b) for(int i = a; i < b; i++)
#define REP_(i, a, b) for(int i = a; i <= b; i++)
#define sl(n) scanf("%lld", &n);
#define si(n) scanf("%d", &n);
#define RepAll(a) for(auto x: a)
#define cout(ans) cout << ans << endl;
typedef long long ll;
 
using namespace std;
const int maxn = 1e5+50;
 
struct pl{
    int l;
    int r;
}a[maxn];
bool cmp(struct pl a, struct pl b){
    return a.l < b.l;
}
int main(){
    //freopen("in.txt", "r", stdin);
    int t; scanf("%d", &t);
    while(t--){
        int l, r, ansl, ansr;
        int n; scanf("%d", &n);
 
        REP(i , 0, n){
            cin >> a[i].l >> a[i].r;
        }
        if(n == 1){cout << "0" << '\n';continue;}
        sort(a, a+n, cmp);
        ansl = a[0].l, ansr = a[0].r;
        for(int i = 1; i < n; i++){
            ansl = max(a[i].l, ansl);
            ansr = min(a[i].r, ansr);
        }
        if(ansr > ansl) cout << "0" << endl;
        else{ cout << ansl-ansr << '\n';
    }}
 
}
Published 20 original articles · won praise 3 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43382350/article/details/103230050