PersonalCorpus 版 (精华区)

156481 05:59:07 7 Mar 2002
Bozhang,HIT,P.R.China 1136 Pascal Accepted 0.09 sec 147K


type BTnode = record
                    v:word;
                    l,r:word;
                    used:boolean;
              end;
var
    mt:array[1..3000] of BTnode;
    a:array[1..3000] of word;
    i,n:word;
function FindNullNode(st,en:word):word;
var i:word;
begin
    for i:=st to en do if not mt[i].used then break;
    if i<=en then result:=i else result:=0;
end;
procedure MakeTree(c,d,p:word);
var i:word;
begin
    if c>d then exit;
    mt[p].used:=true;
    mt[p].v:=a[d];
    if c=d then exit;
    //l
    for i:=c to d-1 do if a[i]>a[d] then break;
    if i<>c then begin
        mt[p].l:=FindNullNode(1,3000);
        MakeTree(c,i-1,mt[p].l);
    end;
    for i:=d-1 downto c do if a[i]<a[d] then break;
    if i<>d-1 then begin
        mt[p].r:=FindNullNode(1,3000);
        MakeTree(i+1,d-1,mt[p].r);
    end;
end;
procedure PrintIt(c:word);
begin
    with mt[c] do begin
        if r<>0 then PrintIt(r);
        if l<>0 then PrintIt(l);
        write(v,' ');
    end;
end;
begin
    fillchar(mt,sizeof(mt),0);
    readln(n);
    for i:=1 to n do begin
        while eoln(input) do readln;
        read(a[i]);
    end;
    MakeTree(1,n,1);
    PrintIt(1);
end.
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.763毫秒