描述
三个农民每天清晨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
Executing...
Test 1: TEST OK [0.011 secs, 1184 KB]
Test 2: TEST OK [0.000 secs, 1180 KB]
Test 3: TEST OK [0.000 secs, 1180 KB]
Test 4: TEST OK [0.011 secs, 1184 KB]
Test 5: TEST OK [0.108 secs, 1184 KB]
Test 6: TEST OK [0.000 secs, 1180 KB]
Test 7: TEST OK [0.000 secs, 1184 KB]
Test 8: TEST OK [0.011 secs, 1184 KB]
All tests OK.
Your program ('milk2') produced all correct answers! This is your submission #10 for this problem. Congratulations!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | { ID:cxj6661 PROB:milk2 LANG:PASCAL } Program milk2; Const fin='milk2.in'; fou='milk2.out'; Var n,i,j,a,c,i1,i2,j1,j2,max1,max2,n1,min,max:longint; b:array[0..1000000]of boolean; begin assign(input,fin); reset(input); assign(output,fou); rewrite(output); readln(n); fillchar(b,sizeof(b),0); min:=5000; max:=0; for i:=1 to n do begin readln(a,c); if a<min then min:=a; if c>max then max:=c; for j:=a+1 to c do b[j]:=true; end; close(input); max1:=0; max2:=0; n1:=0; for i:=min+1 to max do begin if ((i=1)or(b[i-1]=false))and(b[i]) then n1:=1 else if b[i] then inc(n1); if n1>max1 then max1:=n1; end; n1:=0; for i:=min+1 to max do begin if ((i=1)or(b[i-1]=true))and(b[i]=false) then n1:=1 else if b[i]=false then inc(n1); if n1>max2 then max2:=n1; end; writeln(max1,' ',max2); close(output); end. |