usaco1.2 milk3 2009.10.22
描述
三个农民每天清晨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:
(file milk2.in)
第一行:一个整数N。
第二行: 2..N+1:
每行两个小于1000000的非负整数,表示一个农民的开始时刻与结束时刻。
OUTPUT FORMAT:
(file milk2.out)
仅一行,两个整数,用空格分开,即题目所要求的两个答案。
SAMPLE INPUT
3
300 1000
700 1200
1500 2100
SAMPLE OUTPUT
900 300
总结:
1.用数组记录每个时间段,一个时刻占一个空间,true表示有人,false表示没人。
然后循环一个一个处理,刚开始以为这样会超时,没想到一下就过了。
2.第一次看题时,没有领会清楚题意,我以为头尾的时间都包括,结果是,头的时间包括,尾的时间不包括。
遇到时间点和时间段的题一定要分析题目和样例搞清楚到底是什么.
program
{
ID:
PROG: milk2
LANG: PASCAL
}
program milk2;
var
a:array[0..1000000]of boolean;{标记时间点}
n,i,j,x,y,max,min,la,lb,aa,bb:longint;
f1,f2:text;
begin
assign(f1,'milk2.in');
reset(f1);
assign(f2,'milk2.out');
rewrite(f2);
readln(f1,n);
fillchar(a,sizeof(a),false);
max:=0;
min:=0;
lb:=0;
la:=1000002;
for i:=1 to n do
begin
readln(f1,x,y);
if x<la then la:=x;
if y>lb then lb:=y;
for j:=x to y-1 do
if not(a[j]) then a[j]:=true;
end;
for i:=la to lb do
begin
if a[i] then inc(max) else
begin
if max>aa then aa:=max;
max:=0;
end;
if not(a[i]) then inc(min) else
begin
if min>bb then bb:=min;
min:=0;
end;
end;
writeln(f2,aa,' ',bb);
close(f1);
close(f2);
end.
data
TASK: milk2LANG: PASCAL
Executing...
Test 1: TEST OK [0.008 secs]
Test 2: TEST OK [0.008 secs]
Test 3: TEST OK [0.008 secs]
Test 4: TEST OK [0.008 secs]
Test 5: TEST OK [0.12 secs]
Test 6: TEST OK [0.008 secs]
Test 7: TEST OK [0.008 secs]
Test 8: TEST OK [0.02 secs]
All tests OK.
Your program ('milk2') produced all correct answers! This is your
submission #45 for this problem. Congratulations!