ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1126. Magnetic Storms

Magnetic storm : Please help, I tried many tests but it get WA
Posted by raxtinhac 30 May 2002 19:30
Here is my program :


const   max     = 15000;

       fi    = ‘magnetic.inp’;

var     a,tt,cs          :array[1..max] of longint;

        m,n,x             :longint;

procedure doicho(r,c :longint);
var     tam          :longint;
begin
  tam := a[r]; a[r] := a[c]; a[c] := tam;
  cs[ tt[r] ] := c; cs[ tt[c] ] := r;
  tam := tt[r]; tt[r] := tt[c]; tt[c] := tam;
end;


procedure downheap(r :longint);
var     c            :longint;
begin
  while r*2 <= n do
  begin
    c := r*2;
    if c < n then
      if a[c] < a[c+1] then inc(c);

    if a[r] < a[c] then doicho(r,c)
                   else exit;
    r := c;
  end;
end;


procedure upheap( r :longint);
var     c         :longint;
begin
  while r > 1 do
  begin
    c := r div 2;
    if a[c] < a[r] then doicho(r,c)
                   else exit;
    r := c;
  end;
end;

{---------------------------------------------------------------------
------}

procedure them;
var   t        :longint;
begin
  inc(m); if m > n then m := 1;
  t := cs[m];
  a[t] := x;
  upheap(t);
  downheap(t);
end;

{---------------------------------------------------------------------
------}

begin
  assign(f, fi); reset(f);
  readln(f, m);

  for n := 1 to m do
  begin
    readln(f, a[n]);
    tt[n] := n; cs[n] := n;
    if a[n] = -1 then halt;
    upheap(n);
  end;

  repeat
    writeln(a[1]);
    readln(f, x);
    if x = -1 then break;
    them;
  until false;
  close(f);
end.