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

Please, give me a hint. My program gets Time Limit.
Posted by Avanesov 23 Mar 2002 15:48
program p1126;
var
   a : array[1..14000] of integer;
   i, m : integer;
   x  : integer;
   max: integer;
function findmax : integer;
var k : integer;
   res : integer;
begin
 res:=0;
 for k:=1 to m do if res<a[k] then res:=a[k];
 findmax:=res;
end;
begin
  readln(m);
  read(x);
  max:=0;
  i:=1;
  while (x<>-1) and (i<=m) do
  begin
    if max< x then max:=x;
    a[i]:=x;
    inc(i);
    read(x);
  end;
  Writeln(max);
  i:=1;

  while x<>-1 do
  begin
  if a[i]= max then
  begin
    a[i]:=x;
    max:=findmax;
  end else
  begin
   a[i]:=x;
   if a[i]>max then max:=a[i];
  end;
  Writeln(max);

  i:=i mod m +1 ;
  read(x);
  end;

end.
use suitable data structure (-)
Posted by MadPsyentist/Sam 25 Mar 2002 10:20
Re: use suitable data structure (-)
Posted by Junjie Liang 25 Mar 2002 16:45
The first time I read this problem, I thought of using a heap.
However, I could not implement it properly. How do you know which
number to delete off the heap each time? I only know how to delete
the max/min number (depending on min/max heap). Any
hints/tips/help/aids will be appreciated. :)
Re: use suitable data structure (-)
Posted by MadPsyentist/Sam 26 Mar 2002 14:42
look at the sample input
3
10
11
10
0 , delete 10 becase it's not in the period to consider anymore
0 , delete 11
0 , delete 10
1 , delete 0
so on...

that is ,if you use heap (like me) , your heap must be able to delete not
only the optimal data

in the constrain of this problem (i don't know if it's bigger) , you can do
the deletion in O(lg N)

if still have no idea, feel free to mail to piyawut@se-ed.net