zoukankan      html  css  js  c++  java
  • Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A. Math Problem

    【题目链接】A题链接

    【题目类型】模拟

    【题目大意】给你n段区间要求你求解,求解一个最小答案区间满足这个区间与这n个区间最少都有一个共同点

    【解题思路】
    可以看一下图片
    在这里插入图片描述
    这两个线段实际上只需要【R1,R1】就可以了

    在这里插入图片描述
    这种情况要想要答案区间最小,那么选最小的右边和最大的左边(这也就是最关键的思路)
    而你最后的到的ansl值比ansr的值要搭,那就说明是第一幅图那种情况,就是存在那么一个点,刚好都在这些区间上,所以直接输出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" << '
    ';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 << '
    ';
        }}
     
    }
    
  • 相关阅读:
    Markdown基本语法
    Hexo 集成 Gitalk 评论系统
    hexo添加百度统计
    tf.nn.softmax
    tf.InteractiveSession()与tf.Session()
    归一化、标准化、正则化的区别
    感受野的概念
    CNN中的low-level feature 与high-level feature
    安装vncserver后导致Ubuntu循环登录进入不了桌面的问题原因及解决办法
    python3 + Tensorflow + Faster R-CNN训练自己的数据
  • 原文地址:https://www.cnblogs.com/ygbrsf/p/12519592.html
Copyright © 2011-2022 走看看