您的位置:首页 > 其它

Milking Cows 挤牛奶

2011-09-04 20:55 417 查看

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)

3

300 1000

700 1200

1500 2100

OUTPUT FORMAT

一行,两个整数,即题目所要求的两个答案.

SAMPLE OUTPUT (file milk2.out)

900 300

=========================================================================

初始状态的处理...

==========================================================================
{
ID:jie19952
PROG:milk2
LANG:PASCAL
}

type
re=record
s,e:longint;
end;
var
n:longint;
ti:array[1..5000]of re;
procedure init;
begin
assign(input,'milk2.in');
assign(output,'milk2.out');
reset(input); rewrite(output);
end;

procedure terminate;
begin
close(input); close(output);
halt;
end;

procedure qsort(s,t:longint);
var
tem:re;
i,j,x,x1:longint;
begin
i:=s; j:=t; x:=ti[(s+t) shr 1].s; x1:=ti[(s+t) shr 1].e;
repeat
while (x<ti[j].s)or((x=ti[j].s)and(x1<ti[j].e)) do dec(j);
while (x>ti[i].s)or((x=ti[i].s)and(x1>ti[i].e)) do inc(i);
if i<=j then
begin
tem:=ti[i];
ti[i]:=ti[j];
ti[j]:=tem;
inc(i); dec(j);
end;
until i>j;
if i<t then qsort(i,t);
if s<j then qsort(s,j);
end;

procedure main;
var
i:longint;
last,first:longint;
ans1,ans2:longint;
begin
readln(n);
ans1:=0; ans2:=0;
for i:=1 to n do
begin
readln(ti[i].s,ti[i].e);
if ans1<ti[i].e-ti[i].s then
ans1:=ti[i].e-ti[i].s;
end;

qsort(1,n);
first:=ti[1].s;
last:=ti[1].e;

for i:=2 to n do
begin
if ti[i].s<=last then
begin
if last<ti[i].e then
last:=ti[i].e;
end
else begin
if ans1<last-first then
ans1:=last-first;
if ans2<ti[i].s-last then
ans2:=ti[i].s-last;
first:=ti[i].s;
last:=ti[i].e;
end;
end;
writeln(ans1,' ',ans2);
end;

begin
init;
main;
terminate;
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: