是一道简单的区间合并题目。
有不会区间合并的可以去看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;
}