Guest User

Untitled

a guest
May 8th, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.46 KB | None | 0 0
  1. type pc=array[1..3] of longint;
  2. var h:array[1..40000] of pc;
  3. a,t,c:pc;
  4. i,n,k,z:longint;
  5. res,m:int64;
  6. procedure swap(var a,b:pc);
  7. var y:pc;
  8. begin
  9.   y:=a;
  10.   a:=b;
  11.   b:=y;
  12. end;
  13. procedure sd;
  14. var i,x:longint;
  15. begin
  16.   i:=1;
  17.   x:=1;
  18.   while 2*x<=k do
  19.   begin
  20.     if h[2*x][1]<h[i][1] then i:=2*x;
  21.     if (2*x+1<=k) and (h[2*x+1][1]<h[i][1]) then i:=2*x+1;
  22.     if x=i then break;
  23.     swap(h[i],h[x]);
  24.     x:=i;
  25.   end;
  26. end;
  27. procedure remove;
  28. begin
  29.   h[1]:=h[k];
  30.   Dec(k);
  31.   sd;
  32. end;
  33. procedure add(a:pc);
  34. var c:longint;
  35. begin
  36.   Inc(k);
  37.   h[k]:=a;
  38.   c:=k;
  39.   while (c>1) and (h[c div 2][1]>h[c][1]) do
  40.   begin swap(h[c],h[c div 2]); c:=c div 2; end;
  41. end;
  42. begin
  43. assign(input,'horizon.in'); reset(input);
  44. assign(output,'horizon.out'); rewrite(output);
  45. readln(n);
  46. k:=0;
  47. res:=0;
  48. for i:=1 to n do
  49. begin
  50.   for z:=1 to 3 do read(a[z]);
  51.   add(a);
  52. end;
  53. a:=h[1];
  54. remove;
  55. while k>0 do
  56. begin
  57.   t:=h[1];
  58.   if (t[1]>=a[2]) then
  59.   begin
  60.     m:=a[2]-a[1];
  61.     res:=res+m*a[3];
  62.     a:=t;
  63.     remove;
  64.     continue;
  65.   end;
  66.   if a[3]<=t[3] then
  67.   begin
  68.     m:=t[1]-a[1];
  69.     res:=res+m*a[3];
  70.     if a[2]>t[2] then
  71.     begin
  72.       c[1]:=t[2];
  73.       c[2]:=a[2];
  74.       c[3]:=a[3];
  75.       add(c);
  76.     end;
  77.     a:=t;
  78.     remove;
  79.   end
  80.   else begin
  81.     if a[2]<t[2] then
  82.     begin
  83.       t[1]:=a[2];
  84.       h[1]:=t;
  85.       sd;
  86.     end
  87.     else remove;
  88.   end;
  89. end;
  90. m:=a[2]-a[1];
  91. res:=res+m*a[3];
  92. writeln(res);
  93. close(input); close(output);
  94. end.
Advertisement
Add Comment
Please, Sign In to add comment