vj p1357
此乃水题也!
既然如此,为何还要写这个题解呢?当然,他水了,我不能水,所以水题就用树来完成!
先建个排序二叉树,然后来个中序遍历,一样搞定!
速度很快!有图有真相:
代码如下
type
link=^point;
point=record
l,r:link;
da,t:longint;
end;
var
head:link;
procedure add(h:link;m:longint);
var
t:link;
begin
if m<h^.da then
begin
if h^.l<>nil then
begin
add(h^.l,m);
end
else
begin
new(t);
t^.l:=nil;t^.r:=nil;
t^.da:=m;
t^.t:=1;
h^.l:=t;
end;
end
else
if m=h^.da then inc(h^.t)
else
if m>h^.da then
begin
if h^.r<>nil then
begin
add(h^.r,m);
end
else
begin
new(t);
t^.l:=nil;t^.r:=nil;
t^.da:=m;
t^.t:=1;
h^.r:=t;
end;
end;
end;
procedure print(h:link);
begin
if h<>nil then
begin
print(h^.l);
writeln(h^.da,' ',h^.t);
print(h^.r);
end;
end;
procedure init;
var
n,i,a:longint;
begin
new(head);head^.l:=nil;head^.r:=nil;
readln(n);
readln(a);head^.da:=a;head^.t:=1;
for i:=2 to n do
begin
readln(a);
add(head,a);
end;
end;
begin
init;
print(head);
end.
link=^point;
point=record
l,r:link;
da,t:longint;
end;
var
head:link;
procedure add(h:link;m:longint);
var
t:link;
begin
if m<h^.da then
begin
if h^.l<>nil then
begin
add(h^.l,m);
end
else
begin
new(t);
t^.l:=nil;t^.r:=nil;
t^.da:=m;
t^.t:=1;
h^.l:=t;
end;
end
else
if m=h^.da then inc(h^.t)
else
if m>h^.da then
begin
if h^.r<>nil then
begin
add(h^.r,m);
end
else
begin
new(t);
t^.l:=nil;t^.r:=nil;
t^.da:=m;
t^.t:=1;
h^.r:=t;
end;
end;
end;
procedure print(h:link);
begin
if h<>nil then
begin
print(h^.l);
writeln(h^.da,' ',h^.t);
print(h^.r);
end;
end;
procedure init;
var
n,i,a:longint;
begin
new(head);head^.l:=nil;head^.r:=nil;
readln(n);
readln(a);head^.da:=a;head^.t:=1;
for i:=2 to n do
begin
readln(a);
add(head,a);
end;
end;
begin
init;
print(head);
end.