usaco section1.2
Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer begins milking his cow at time 300 (measured in seconds after 5 am) and ends at time 1000. The second farmer begins at time 700 and ends at time 1200. The third farmer begins at time 1500 and ends at time 2100. The longest continuous time during which at least one farmer was milking a cow was 900 seconds (from 300 to 1200). The longest time no milking was done, between the beginning and the ending of all milking, was 300 seconds (1500 minus 1200).
Your job is to write a program that will examine a list of beginning and ending times for N (1 <= N <= 5000) farmers milking N cows and compute (in seconds):
- The longest time interval at least one cow was milked.
- The longest time interval (after milking starts) during which no cows were being milked.
PROGRAM NAME: milk2
INPUT FORMAT
Line 1: | The single integer |
Lines 2..N+1: | Two non-negative integers less than 1000000, the starting and ending time in seconds after 0500 |
SAMPLE INPUT (file milk2.in)
3 300 1000 700 1200 1500 2100
OUTPUT FORMAT
A single line with two integers that represent the longest continuous time of milking and the longest idle time.SAMPLE OUTPUT (file milk2.out)
900 300 简单的模拟。。。但是sqybi说什么可以用线段树还有什么离散化。。听不懂。。他的程序也读不大懂。。。
自己随便模拟了一下。。。结果一看和maigo大牛的做法基本相同。。。。汗死。。。。
别人表以为我是抄滴。。。。。
this is my program。。。。
-----------------------------------我叫分界线-----------------------------------
{
ID:Parachutes
PROG:milk2
LANG:PASCAL
}
type ss=record
start:longint;
finish:longint;
end;
var i,n,j,s2,s3,b,k:longint;
s1:ss;
s:array[1..1000000] of ss;
begin
assign(input,'milk2.in'); reset(input);
assign(output,'milk2.out'); rewrite(output);
readln(n);
for i:=1 to n do readln(s[i].start,s[i].finish);
for i:=1 to n-1 do
for j:=i+1 to n do
if s[i].start>s[j].start then begin
s1:=s[i];
s[i]:=s[j];
s[j]:=s1;
end;
b:=0; k:=0;
s2:=s[1].start; s3:=s[1].finish;
for i:=2 to n do
if s[i].start<=s3 then begin if s[i].finish>s3 then
s3:=s[i].finish
end
else begin
if s3-s2>b then b:=s3-s2;
if s[i].start-s3>=k then k:=s[i].start-s3;
s2:=s[i].start;
s3:=s[i].finish;
end;
if s3-s2>b then b:=s3-s2;
writeln(b,' ',k);
close(input);
close(output);
end.
呵呵。有点状态了。。。这几天要多做题。。。。