算法练习:区间合并
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef pair<int,int> PII;
vector<PII> segs;
void merge(vector<PII> &segs)
{
vector<PII> res;
sort(segs.begin(),segs.end());
int st=-2e9,ed=-2e9;
for(auto seg:segs)
{
if(ed<seg.first)
{
if(st!=-2e9) res.push_back({st,ed});
st=seg.first;
ed=seg.second;
}
else ed=max(ed,seg.second);
}
if(st!=-2e9) res.push_back({st,ed});
segs=res;
}
int main()
{
int n;
cin>>n;
int ans1=0,ans2=0;
for(int i=0;i<n;i++)
{
int l,r;
cin>>l>>r;
segs.push_back({l,r});
}
merge(segs);
for(int i=0;i<segs.size();i++)
{
ans1=max(ans1,segs[i].second-segs[i].first);
if(i==0) ans2=0;
else ans2=max(ans2,segs[i].first-segs[i-1].second);
}
cout<<ans1<<" "<<ans2<<endl;
return 0;
}