# 【USACO-Chapter1-1.2】【模拟】Milking Cows

【题目描述】

• 最长至少有一人在挤奶的时间段。
• 最长的无人挤奶的时间段。（从有人挤奶开始算起）

【输入格式】(milk2.in)

【输出格式】(milk2.out)

【输入样例】

3
300 1000
700 1200
1500 2100

【输出样例】

900 300

#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn = 5000+10;
struct time
{
int left;
int right;
}times[maxn];
int n;
void init()
{
freopen("milk2.in","r",stdin);
freopen("milk2.out","w",stdout);
}

{
scanf("%d",&n);
for(int i = 1;i <= n;i++)
{
scanf("%d%d",×[i].left,×[i].right);
}
}

int cmp(const void *a,const void *b)
{
return (*(time *)a).left - (*(time *)b).left;
}

int max(int a,int b)
{
return a > b ? a : b;
}

void solve()
{
qsort(&time[1],n,sizeof(times[0]),cmp);
int ans1 = 0,ans2 = 0;
int leftt = times[1].left,rightt = times[1].right;
for(int i = 2;i <= n;i++)
{
ans1 = max(ans1,rightt - leftt);
if(times[i].left <= rightt && times[i].right > rightt)
{
rightt = times[i].right;
}
else if(times[i].left > rightt)
{
ans2 = max(ans2,times[i].left - rightt);
leftt = times[i].left;
rightt = times[i].right;
}
}
if(ans1 == 0)ans1 = rightt - leftt;
printf("%d %d\n",ans1,ans2);
}

int main()
{
init();
}