#include <cstdio>
#include <algorithm>
using namespace std;
struct mike{
int startTime;
int endTime;
}people[5001];
struct Rule{
bool operator ()(const mike &p1,const mike &p2){
if (p1.startTime == p2.startTime)return p1.endTime < p2.endTime;
return p1.startTime < p2.startTime;
}
};
int main()
{
int n;
scanf("%d",&n);
int i;
for (i = 0;i < n;i ++)
scanf("%d %d",&people[i].startTime,&people[i].endTime);
sort (people,people+n,Rule());
int maxTimeHV = 0;
int maxTimeNO = 0;
int start,ennnd;
int sssss,eeeee;
int time,timeNO;
int flag = 0;// flag == 0 表示断掉了 反之没有断
for (i = 1;i < n;i ++){
if (people[i].startTime<=people[i-1].endTime){
if (0 == flag){
start = people[i-1].startTime;
ennnd = max(people[i].endTime,people[i-1].endTime);
}
else {
ennnd = max(ennnd,people[i].endTime);
}
time = ennnd - start;
if (maxTimeHV < time)
maxTimeHV = time;
flag = 1;
}
else {
if (1 == flag)
sssss = people[i-1].endTime;
eeeee = people[i].startTime;
timeNO = eeeee - sssss;
if (maxTimeNO < timeNO)
maxTimeNO = timeNO;
flag = 0;
}
}
printf("%d %d\n",maxTimeHV,maxTimeNO);
return 0;
}
https://www.cnblogs.com/candy99/p/5791511.html
有时间看看大神的