vietthaitink21 viết:
QUOTE:
thanghungkhi viết:
QUOTE:
iamquang95 viết:
QUOTE:
25 bạn ạ
Mình cám ơn, mình cũng ra 25, không biết sai chỗ nào mà cứ bị 30,77 mãi ^^
1/3 số test <=5000 mà, nếu bạn không làm với độ phức tạp O(N) thì 30,77 là đúng rồi mà
Anh xem qua Code của em với ạ, em nghĩ là O(N) mà ^^
top:=1;
num:=n-1;
stack[top]:=a[1];
id[top]:=1;
for i:=2 to n do begin
if a[i]>stack[top] then begin
while (stack[top]<a[i]) and (top>0) do begin
if id[top]<i-1 then inc(num);
dec(top);
end;
inc(top);stack[top]:=a[i];id[top]:=i;
if (stack[1]<>stack[top]) and (id[1]<>id[top]) then inc(num);
end else if a[i]=stack[top] then begin
top1:=top;
while stack[top1]=a[i] do begin
if id[top1]<i-1 then inc(num);
dec(top1);
end;
inc(top);stack[top]:=a[i];id[top]:=i;
if (stack[1]<>stack[top]) and (id[1]<>id[top]) then inc(num);
end else if a[i]<stack[top] then begin
inc(top);stack[top]:=a[i];id[top]:=i;
end;
PS: Để thẻ code khó đọc quá nên e để như vậy ạ ^^