usaco 1.2:Milking Cows

来源:互联网 发布:王者和lol的区别 知乎 编辑:程序博客网 时间:2024/05/16 04:41

 描述

三个农民每天清晨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)

Line 1:

一个整数N。

Lines 2..N+1:

每行两个小于1000000的非负整数,表示一个农民的开始时刻与结束时刻。

OUTPUT FORMAT:

(file milk2.out)

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

 

 SAMPLE INPUT

3300 1000700 12001500 2100

SAMPLE OUTPUT

900 300
题很简单,但我很是纠结了一阵子。。调试功力不够啊。。。
不过这道题有代码了。。
{
ID:gy_kw1
PROG:milk2
LANG:PASCAL
}
const
  maxn=5000;
  maxm=1000000;
type
  arr=array[1..maxn] of record
    s:longint;
    t:longint;
  end;
var
  n,total1,total2:longint;
  a:arr;
  h:array[1..maxm] of boolean;
procedure init;
var
  i,j:longint;
begin
  assign(input,'milk2.in');
  assign(output,'milk2.out');
  reset(input);
  rewrite(output);
  fillchar(h,sizeof(h),false);
  readln(n);
  for i:=1 to n do
  begin
    read(a[i].s,a[i].t);
    for j:=a[i].s+1 to a[i].t do
      h[j]:=true;
  end;
end;
procedure main;
var
  i,j,k,x,mins,maxt,max1,max2:longint;
begin
  mins:=1000000;maxt:=0;
  for i:=1 to n do
  begin
    if a[i].s<mins then mins:=a[i].s;
    if a[i].t>maxt then maxt:=a[i].t;
  end;
  i:=1;k:=0;x:=0;
  while h[i]=false do inc(i);
  max1:=0;max2:=0;
  repeat
    while h[i] do
    begin
      inc(i);
      inc(k);
    end;
    if k>max1 then max1:=k;
    k:=0;
    if i>=maxt then
    begin
      total1:=max1;
      total2:=max2;
      exit;
    end;
    while h[i]=false do
    begin
      inc(x);
      inc(i);
    end;
    if x>max2 then max2:=x;
    x:=0;
  until i>maxt;
end;
procedure outit;
begin
  close(input);
  close(output);
end;
begin
  init;
  main;
  writeln(total1,' ',total2);
  outit;
end.
开个哈希表扫描就是了
需要注意的是输入数据如 300 900 那么处理的时候要从301 开始到900,切记。。。