AcWing 1343.挤牛奶

是一道简单的区间合并题目。

有不会区间合并的可以去看y总的模板,这里我就不详细的讲了。

思路:把能合并的区间全部合并出来,然后再去判断合并之后有多少个区间,对于只有一个区间的,需要特判,其他的直接写就行了,也就是相当于模板题的一种变形。

上代码:

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath> 
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<sstream>
#include<map>
#include<limits.h>
#include<set>
#define MAX 100
#define _for(i,a,b) for(int i=a;i<(b);i++)
#define ALL(x) x.begin(),x.end()
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
LL n, m, counts, num;
vector<PII>res;
void hebing() {
    int st = -2e9;
    int ed = -2e9;
    sort(res.begin(), res.end());
    vector<PII>haha;
    for (auto it : res) {
        if (it.first > ed) {
            if (st != -2e9) 
                haha.push_back({ st,ed });

                st = it.first;
                ed = it.second;
            
        }
        else
            ed = max(ed,it.second);
    }
    if (st != -2e9)
        haha.push_back({ st,ed });
    res = haha;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin >> n;
    for (int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        res.push_back({ x,y });
    }
    if (n == 1)
        cout << res[0].second - res[0].first << " " << 0 << endl;
    else {
        int len1 = INT_MIN;
        int len2 = INT_MIN;
        hebing();
        _for(i, 0, res.size()) {
            len1 = max(len1, res[i].second - res[i].first);
        }
        if (res.size() == 1)
            len2 = 0;
        else {
            _for(i, 0, res.size())
                len2 = max(len2, res[i + 1].first - res[i].second);
        }
        cout << len1 << " " << len2;
    }
    return 0;
}

  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值