模拟/usaco 1.2.1 Milking Cows

题意

  给出n个工作时间段 问连续最大空闲时间段和连续最大工作时间段分别是多少

分析

  按照每个时间段的起始排序,记录一下当前的s和t,如果按照循环,下一个时间段在当前t的后面,则更新空闲时间段和s,t

      如果下一个时间段的起始在t之前,则更新t和工作时间段

      第一次WA,没看明白题就写了,第二次WA,尼玛的快排写错了,第三次才AC

Accepted Code

 1 {
 2 ID: jessiel2
 3 PROG: milk2
 4 LANG: PASCAL
 5 }
 6 Program milk2;
 7 Const
 8   Infile = 'milk2.in';
 9   Outfile = 'milk2.out';
10 Var
11   a,b:Array[0..5001]Of Longint;
12   s,t,i,no,yes,n:Longint;
13 Function max(a,b:Longint):Longint;
14 Begin
15   If a>b Then Exit(a) Else Exit(b);
16 End;
17 Procedure qsort(l,r:Longint);
18 Var
19   i,j,mid,t:Longint;
20 Begin
21   i:=l;j:=r;mid:=a[(l+r) Div 2];
22   Repeat
23     While a[i]<mid Do Inc(i);
24     While a[j]>mid Do Dec(j);
25     If i<=j Then Begin
26       t:=a[i];a[i]:=a[j];a[j]:=t;
27       t:=b[i];b[i]:=b[j];b[j]:=t;
28       Inc(i);Dec(j);
29     End;
30   Until i>j;
31   If i<r Then qsort(i,r);
32   If l<j Then qsort(l,j);
33 End;
34 Begin
35   Assign(input,infile);Reset(input);
36   Assign(output,outfile);Rewrite(output);
37   ReadLn(n);
38   For i:=1 To n Do Begin
39     ReadLn(a[i],b[i]);
40   End;
41   qsort(1,n);
42   no:=0;yes:=b[1]-a[1];
43   s:=a[1];
44   t:=b[1];
45   For i:=2 To n Do Begin
46     If a[i]<=t Then Begin
47       If b[i]>t Then t:=b[i];
48       yes:=max(yes,t-s);
49     End Else Begin
50       no:=max(no,a[i]-t);
51       s:=a[i];t:=b[i];
52     End;
53   End;
54   WriteLn(yes,' ',no);
55   CLose(input);Close(output);
56 End.

 

posted @ 2013-05-06 20:52  Rinyo  阅读(274)  评论(0编辑  收藏  举报