fork(9) download
  1. {
  2.  Author:wzx961008
  3.  Problem:JSOI 2008-help
  4.  Verdict:Accepted
  5.  Language:PASCAL
  6.  Run Time:0.200s
  7.  AC Date:2011-4-9
  8. }
  9. const oo=maxlongint;
  10. type point=record
  11. x,y:longint;
  12. end;
  13. stage=record
  14. l,r,h:longint;
  15. end;
  16. var left,right:array[0..1000]of qword;
  17. stages:array[0..1000]of stage;
  18. i,j,stage_num,max_drop:longint;
  19. start:point;
  20. b:boolean;
  21. function min(a,b:qword):qword;
  22. begin
  23. if a<b then exit(a)
  24. else exit(b);
  25. end;
  26. procedure qsort(l,r:longint);
  27. var s:stage;
  28. i,j,m:longint;
  29. begin
  30. i:=l; j:=r; m:=stages[(l+r)>>1].h;
  31. repeat
  32. while stages[i].h>m do inc(i);
  33. while stages[j].h<m do dec(j);
  34. if i<=j then begin
  35. s:=stages[i]; stages[i]:=stages[j]; stages[j]:=s;
  36. inc(i); dec(j);
  37. end;
  38. until i>j;
  39. if l<j then qsort(l,j);
  40. if i<r then qsort(i,r);
  41. end;
  42. begin
  43. readln(stage_num,start.x,start.y,max_drop);
  44. for i:=1 to stage_num do
  45. with stages[i] do
  46. readln(l,r,h);
  47. qsort(1,stage_num);
  48. stages[0].l:=start.x; stages[0].r:=start.x; stages[0].h:=start.y;
  49. for i:=stage_num downto 0 do begin
  50. b:=true;
  51. for j:=i+1 to stage_num do
  52. if (stages[i].l>=stages[j].l)and(stages[i].l<=stages[j].r) then begin
  53. b:=false;
  54. if stages[i].h-stages[j].h<=max_drop then left[i]:=min(left[j]+(stages[i].l-stages[j].l),right[j]+(stages[j].r-stages[i].l))
  55. else left[i]:=oo;
  56. break;
  57. end;
  58. if b then
  59. if stages[i].h<=max_drop then left[i]:=0
  60. else left[i]:=oo;
  61. b:=true;
  62. for j:=i+1 to stage_num do
  63. if (stages[i].r>=stages[j].l)and(stages[i].r<=stages[j].r) then begin
  64. b:=false;
  65. if stages[i].h-stages[j].h<=max_drop then right[i]:=min(left[j]+(stages[i].r-stages[j].l),right[j]+(stages[j].r-stages[i].r))
  66. else right[i]:=oo;
  67. break;
  68. end;
  69. if b then
  70. if stages[i].h<max_drop then right[i]:=0
  71. else right[i]:=oo;
  72. end;
  73. if min(left[0],right[0])+start.y<oo then writeln(min(left[0],right[0])+start.y)
  74. else writeln(-1);
  75. end.
stdin
3 8 17 20
0 10 8
0 10 13
4 14 3
compilation info
Free Pascal Compiler version 2.2.0 [2009/11/16] for i386
Copyright (c) 1993-2007 by Florian Klaempfl
Target OS: Linux for i386
Compiling prog.pas
prog.pas(54,67) Warning: Variable "left" does not seem to be initialized
prog.pas(54,102) Warning: Variable "right" does not seem to be initialized
Linking prog
74 lines compiled, 0.0 sec
2 warning(s) issued
stdout
23