一题枚举,比较好实现的方法是,sort开始的时间。在枚举的时候,用pre和last维护区间。
那么最长的挤奶时间是ans1 = max(ans1,last - pre);
间隔时间在枚举的时候很好找,只要下一头奶牛的开始时间大于last便有间隔(已sort)
/**
PROG:milk2
LANG:C++
ID:DickensTone
**/
#include<iostream>
#include<fstream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 5000 + 5;
struct node
{
int beg;
int en;
}person[maxn];
bool cmp(node x, node y)
{
if(x.beg < y.beg) return 1;
else if(x.beg == y.beg) return x.en < y.en;
else return 0;
}
int main()
{
freopen("milk2.in", "r", stdin);
freopen("milk2.out", "w", stdout);
int n;
while(scanf("%d", &n) == 1)
{
for(int i = 0; i < n; i++)
scanf("%d%d", &person[i].beg, &person[i].en);
sort(person, person + n, cmp);
int ans1 = 0, ans2 = 0;
int pre = person[0].beg, last = person[0].en;
for(int i = 1; i < n; i++)
{
ans1 = max(ans1, last - pre);
if(person[i].beg > last)
{
ans2 = max(ans2, person[i].beg - last);
pre = person[i].beg;
last = person[i].en;
}
if(person[i].en > last)
last = person[i].en;
}
ans1 = max(ans1, last - pre);
printf("%d %d\n", ans1, ans2);
}
return 0;
}