记录编号 102246 评测结果 AAAAAAAA
题目名称 挤牛奶 最终得分 100
用户昵称 Gravatar传奇 是否通过 通过
代码语言 Pascal 运行时间 0.007 s
提交时间 2014-05-17 21:04:29 内存使用 0.18 MiB
显示代码纯文本
  1. program usaco1_2_1;
  2. const
  3. maxn=5000;
  4. type
  5. node2=^node3;
  6. node3=record
  7. l,r:longint;
  8. next:node2;
  9. end;
  10. node=record
  11. l,r:longint;
  12. end;
  13. var
  14. a:array[1..maxn] of node;
  15. n,i:longint;
  16. head,p:node2;
  17. procedure qp(l,r:longint);
  18. var
  19. i,j,x:longint;
  20. y:node;
  21. begin
  22. i:=l; j:=r;
  23. x:=a[(l+r) div 2].l;
  24. repeat
  25. while a[i].l>x do inc(i);
  26. while a[j].l<x do dec(j);
  27. if i<=j then
  28. begin
  29. y:=a[i];a[i]:=a[j];a[j]:=y;
  30. inc(i); dec(j);
  31. end;
  32. until i>j;
  33. if l<j then qp(l,j);
  34. if i<r then qp(i,r);
  35. end;
  36. function max(a,b:longint):longint;
  37. begin
  38. if a>b then exit(a)
  39. else exit(b);
  40. end;
  41. procedure hebing(q:node2);
  42. begin
  43. while q^.next<>nil do
  44. begin
  45. if q^.r>=q^.next^.l then
  46. begin
  47. q^.r:=max(q^.r,q^.next^.r);
  48. q^.next:=q^.next^.next;
  49. end
  50. else
  51. q:=q^.next;
  52. end;
  53. end;
  54. procedure find(q:node2);
  55. var
  56. max1,max2,k:longint;
  57. begin
  58. max1:=0;
  59. max2:=0;
  60. k:=q^.r;
  61. while q<>nil do
  62. begin
  63. if q^.r-q^.l>max1 then
  64. max1:=q^.r-q^.l;
  65. if q^.l-k>max2 then
  66. max2:=q^.l-k;
  67. k:=q^.r;
  68. q:=q^.next;
  69. end;
  70. writeln(max1,' ',max2);
  71. end;
  72. begin
  73. assign(input,'milk2.in');
  74. assign(output,'milk2.out');
  75. reset(input);
  76. rewrite(output);
  77.  
  78. readln(n);
  79. for i:=1 to n do
  80. readln(a[i].l,a[i].r);
  81. qp(1,n);
  82. new(head);
  83. head^.next:=nil;
  84. for i:=1 to n do
  85. begin
  86. new(p);
  87. p^.l:=a[i].l; p^.r:=a[i].r;
  88. p^.next:=head^.next;
  89. head^.next:=p;
  90. end;
  91. hebing(head^.next);
  92. find(head^.next);
  93. close(input);
  94. close(output);
  95. end.