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

I use the algorithm of binary search, but still get WA.
Posted by Koala 13 Feb 2003 10:53
{This is my program. I really appreciate your help.}
program p1126;

const
  maxn=25000;

var
  a,c:array [1..maxn] of longint;
  m,n,k,tail,i,j:longint;

  procedure insert(x:longint);
  var
    low,high,mid:longint;
  begin
    low:=1; high:=tail;
    while low<high do
    begin
      mid:=(low+high) shr 1;
      if x>=c[mid] then low:=mid+1 else high:=mid;
    end;
    move(c[low],c[low+1],sizeof(longint)*(tail-low+1));
    inc(tail);
    c[low]:=x;
  end;

  procedure delete(x:longint);
  var
    low,high,mid:longint;
  begin
    low:=1; high:=tail; mid:=0;
    while low<high do
    begin
      mid:=(low+high) shr 1;
      if x=c[mid] then break;
      if x<c[mid] then high:=mid-1 else low:=mid+1;
    end;
    move(c[mid+1],c[mid],sizeof(longint)*(tail-mid));
    dec(tail);
  end;

begin
  read(m);
  n:=0;
  read(k);
  while k<>-1 do
  begin
    inc(n);
    a[n]:=k;
    read(k);
  end;

  c[1]:=-maxlongint; c[2]:=maxlongint;
  tail:=2;
  for j:=1 to m do insert(a[j]);
  writeln(c[tail-1]);

  i:=0;
  for j:=m+1 to n do
  begin
    inc(i);
    delete(a[i]);
    insert(a[j]);
    writeln(c[tail-1]);
  end;
end.