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 1220. Stacks

why i have got Memory limit exceeded ?
Posted by Canhtoan 26 Oct 2008 22:05
I have got this in test 10 ,can anyone tell me ?
i only use an array 1..1000 of pointer ?
but my program :898 kb in test 10 ?
Const   Fi      ='';
        Go      ='';
        Limit   =1001;
Type    pt      =^pts;
                Pts=record
                        Data:Longint;
                        Link:Pt;
                end;
Var     f,g     :text;
        N       :Longint;
        St      :array[1..Limit] of pt;
Procedure OpenFile;
Begin
        Assign(f,fi); Reset(f);
        Assign(g,go); Rewrite(g);
End;

Procedure CloseFile;
begin
        Close(f); Close(g);
End;

Procedure Push(i,j:Longint);
var      p:pts;
Begin
        New(p); p^.data:=i;
        p^.link:=St[j]; ST[j]:=p;
End;

Function  Pop(j:Integer):Longint;
Var     p       :Pt;
Begin
        Pop := St[j]^.data;
        P := St[j]^.link;
        Dispose(St[j]);
        St[j] :=P;
End;

Procedure Solve;
Var     i,a,b:Longint;
        s:string;
        Ch:char;
Begin
        For i:=1 to 1000 do St[i]:=Nil;
        Readln(f,n);
        For i:=1 to n do
        BEgin
                S:='';
                While True do
                BEgin
                        Read(f,ch);
                        if ch=' ' then Break;
                        S:=S+ch;
                End;
                if S='PUSH' Then
                Begin
                        Readln(f,a,b);
                        Push(B,a);
                End
                Else
                Begin
                        Readln(f,a);
                        Writeln(g,Pop(a));
                End;
        End;
End;

Begin
        OpenFile;
        Solve;
        CloseFile;
End.

Edited by author 29.10.2008 09:53
Re: why i have got Memory limit exceeded ?
Posted by N.M.Hieu ( DHSP ) 29 Oct 2008 01:31
Try this test :
N = 100000
PUSH 1 100
PUSH 1 100
...
PUSH 1 100
( there are only "PUSH"s )
In this case, your memory will be 100000 * sizeof( pts ) ~ 800KB ( > 0.75 MB ).
That doesn't include the memory for running your program.
Re: why i have got Memory limit exceeded ?
Posted by Canhtoan 29 Oct 2008 10:11
Const   Fi      ='';
        Go      ='';
        Limit   =100001;
Var     f,g     :text;
        N       :Longint;
        Labels  :array[1..Limit] of Integer;
        A       :array[1..Limit] of Longint;
Procedure OpenFile;
Begin
        Assign(f,fi); Reset(f);
        Assign(g,go); Rewrite(g);
End;

Procedure CloseFile;
begin
        Close(f); Close(g);
End;

Procedure Solve;
Var     i,j,last,x:Longint;
        s:string[5];
        Ch:char;
Begin
        Readln(f,n);
        Last:=0;
        For i:=1 to n do
        BEgin
                S:='';
                While True do
                BEgin
                        Read(f,ch);
                        if ch=' ' then Break;
                        S:=S+ch;
                End;
                if S='PUSH' Then
                Begin
                        Inc(last);
                        Readln(f,Labels[Last],A[Last]);
                End
                Else
                Begin
                        Readln(f,X);
                        For j:=last downto 1 do
                          if Labels[j]=x then
                          Begin
                                Labels[j]:=0;
                                Writeln(g,A[j]);
                                Break;
                          End;
                End;
        End;
End;

Begin
        OpenFile;
        Solve;
        CloseFile;
End.

Edited by author 29.10.2008 10:15
Re: why i have got Memory limit exceeded ?
Posted by Canhtoan 29 Oct 2008 10:13
in vietnamese :
anh Hiếu :lần này em chỉ sử dụng 2 mảng 1..100000 of
longint và integer ,như vậy mem chỉ mất 0,6 mb
nhưng nó vẫn bị MLE :( , ở test 10 :(
Canhtoan wrote 29 October 2008 10:11
Const   Fi      ='';
        Go      ='';
        Limit   =100001;
Var     f,g     :text;
        N       :Longint;
        Labels  :array[1..Limit] of Integer;
        A       :array[1..Limit] of Longint;
Procedure OpenFile;
Begin
        Assign(f,fi); Reset(f);
        Assign(g,go); Rewrite(g);
End;

Procedure CloseFile;
begin
        Close(f); Close(g);
End;

Procedure Solve;
Var     i,j,last,x:Longint;
        s:string[5];
        Ch:char;
Begin
        Readln(f,n);
        Last:=0;
        For i:=1 to n do
        BEgin
                S:='';
                While True do
                BEgin
                        Read(f,ch);
                        if ch=' ' then Break;
                        S:=S+ch;
                End;
                if S='PUSH' Then
                Begin
                        Inc(last);
                        Readln(f,Labels[Last],A[Last]);
                End
                Else
                Begin
                        Readln(f,X);
                        For j:=last downto 1 do
                          if Labels[j]=x then
                          Begin
                                Labels[j]:=0;
                                Writeln(g,A[j]);
                                Break;
                          End;
                End;
        End;
End;

Begin
        OpenFile;
        Solve;
        CloseFile;
End.

Edited by author 29.10.2008 10:14

Edited by author 29.10.2008 10:14
Re: why i have got Memory limit exceeded ?
Posted by N.M.Hieu ( DHSP ) 29 Oct 2008 20:34
Because on Timus, integer = longint (Pascal) = 4 bytes.
You should use Word or SmallInt (2 bytes) for array "Labels".
Good luck
Re: why i have got Memory limit exceeded ?
Posted by Canhtoan 29 Oct 2008 21:10
thanks to mr.N.M.Hieu ( DHSP ) :)