[題目描述]
Milking Cows
擠牛奶
三個農民每天清晨5點起牀,然後去牛棚給3頭牛擠奶。第一個農民在300時刻(從5點開始計時,秒爲單位)給他的牛擠奶,一直到1000時刻。第二個農民在700時刻開始,在 1200時刻結束。第三個農民在1500時刻開始2100時刻結束。期間最長的至少有一個農民在擠奶的連續時間爲900秒(從300時刻到1200時刻),而最長的無人擠奶的連續時間(從擠奶開始一直到擠奶結束)爲300秒(從1200時刻到1500時刻)。
你的任務是編一個程序,讀入一個有N個農民(1 <= N <= 5000)擠N頭牛的工作時間列表,計算以下兩點(均以秒爲單位):
- 最長至少有一人在擠奶的時間段。
- 最長的無人擠奶的時間段。
PROGRAM NAME: milk2
INPUT FORMAT
Line 1: |
一個整數N。 |
Lines 2..N+1: |
每行兩個小於1000000的非負整數,表示一個農民的開始時刻與結束時刻。 |
SAMPLE INPUT (file milk2.in)
OUTPUT FORMAT
一行,兩個整數,即題目所要求的兩個答案。
SAMPLE OUTPUT (file milk2.out)
{
ID: zane2951
PROG: milk2
LANG: PASCAL
}
program milk2;
var
x,y,lx,ly:array[0..5011] of longint;
ans1,ans2,n,i,tot,xx,yy:longint;
//-----------qs------------
procedure qs(s,t:longint);
var
i,j,cex,cey,tmp:longint;
begin
i:=s; j:=t; cex:=x[(i+j)>>1]; cey:=y[(i+j)>>1];
repeat
while (x[i]<cex) or (x[i]=cex) and (y[i]<cey) do inc(i);
while (x[j]>cex) or (x[j]=cex) and (y[j]>cey) do dec(j);
if i<=j then
begin
tmp:=x[i]; x[i]:=x[j]; x[j]:=tmp;
tmp:=y[i]; y[i]:=y[j]; y[j]:=tmp;
inc(i); dec(j);
end;
until i>j;
if i<t then qs(i,t); if s<j then qs(s,j);
end;
//----------main-----------
begin
assign(input,'milk2.in'); reset(input);
assign(output,'milk2.out'); rewrite(output);
readln(n);
for i:=1 to n do readln(x[i],y[i]);
qs(1,n); tot:=0; ans1:=0;
x[n+1]:=1111111; y[n+1]:=1111111;
xx:=x[1]; yy:=y[1];
for i:=2 to n+1 do
if (x[i]>=xx) and (x[i]<=yy) then
if y[i]>yy then yy:=y[i] else else
begin
if yy-xx>ans1 then ans1:=yy-xx;
inc(tot);
lx[tot]:=xx;
ly[tot]:=yy;
xx:=x[i];
yy:=y[i];
end;
ans2:=0;
for i:=2 to tot do
if lx[i]-ly[i-1]>ans2 then ans2:=lx[i]-ly[i-1];
writeln(ans1,'':1,ans2);
close(input); close(output);
end.