Thank bạn nhưng nó cũng vậy, lúc thì báo NZEC lúc thì báo chạy lâu lúc thì quá bộ nhớ ... mặc dù trên máy chạy bình thường với số thao tác tới 10000, v lên max 1000000000 (chạy khoản 1s số thao tác là 100000). Các bạn có thể check thuật toán dùm mình không

Thank trước?
Code: |
program qbheap;
var a: array [0..40000] of longint;
c: char;
v,n: longint;
procedure cong(v: longint);
var max,i,vt: longint;
begin
max:=0;
for i:=0 to n do
if (a[i]>=max) and (a[i]<=v) then
begin
max:=a[i];
vt:=i;
if a[i]=v then exit;
end;
for i:=n downto vt+1 do
a[i+1]:=a[i];
a[vt+1]:=v;
inc(n);
end;
procedure tru;
var t,i: longint;
begin
i:=n;
t:=a[n];
while a[i]=t do
begin
a[i]:=0;
dec(n);
dec(i);
end;
end;
procedure xuat;
var t,i: longint;
begin
writeln(n);
for i:=n downto 1 do
writeln(a[i]);
end;
begin
assign(input,'');
assign(output,'');
reset(input);
rewrite(output);
n:=0;
fillchar(a,sizeof(a),0);
while not eof(input) do
begin
read(c);
if (c='+') and (n<15000) then
begin
readln(v);
cong(v);
end else if (c='-') and (n>0) then tru;
end;
xuat;
close(input);
close(output);
end.
|