Description
三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶。第一个农民在300时刻(从5点开始计时,秒为单位)给他的牛挤奶,一直到1000时刻。第二个农民在700时刻开始,在
1200时刻结束。第三个农民在1500时刻开始2100时刻结束。期间最长的至少有一个农民在挤奶的连续时间为900秒(从300时刻到1200时刻),而最长的无人挤奶的连续时间(从挤奶开始一直到挤奶结束)为300秒(从1200时刻到1500时刻)。
你的任务是编一个程序,读入一个有N个农民(1 <= N <=
5000)挤N头牛的工作时间列表,计算以下两点(均以秒为单位):
• 最长至少有一人在挤奶的时间段。
• 最长的无人挤奶的时间段。
Input
Line 1:
一个整数N。
Lines 2..N+1:
每行两个小于1000000的非负整数,表示一个农民的开始时刻与结束时刻。
Output
一行,两个整数,即题目所要求的两个答案。
Sample Input
3
300 1000
700 1200
1500 2100
Sample Output
900 300
解题思路:先读入数据,把开始至结束时刻用数组标记,并且先求出整个数据的最大值和最小值,再用循环对已经标记的进行统计,统计出题目所要求的最长至少有一人在挤奶的时间段和最长的无人挤奶的时间段,最后输出即可。
程序:
var
a:array[1..1000001]of shortint;
n,max,x,y,i,j,max1,max2,s,s2,min:longint;
begin
readln(n);
s:=1;
min:=maxlongint;
for i:=1 to n do
begin
readln(x,y);
if y>max then max:=y;
if x
for j:=x to y-1 do
a[j]:=1;
end;
for i:=min+1 to max do
begin
if (a[i]=1) and (a[i-1]=1) then inc(s);
if (a[i]=0) and (a[i-1]=1) then begin if s>max1 then max1:=s; s:=0; s2:=1; end;
if (a[i]=0) and (a[i-1]=0) then inc(s2);
if (a[i]=1) and (a[i-1]=0) then begin if s2>max2 then max2:=s2; s2:=0; s:=1; end;
end;
writeln(max1,' ',max2);
end.