# Cod sursa(job #21931)

Utilizator Data 25 februarie 2007 10:06:58 Numarare triunghiuri 0 fpc done Arhiva de probleme 1.7 kb
``````Var
db:longint;
i,j,a,b,c,k:word;
n:word;
et,v:array[-1..801] of 0..30000;
f:text;
kesz,jo:boolean;
Function szetvalogat(e,u:word):word;
var seged:integer;
begin
seged:=et[e];
while e<u do begin
while (e<u) and (et[u]>=seged) do
dec(u);
if e<u then begin
et[e]:=et[u];
inc(e);
while (e<u) and (et[e]<seged) do
inc(e);
et[u]:=et[e];
dec(u);
end;
end;
et[e]:=seged;
szetvalogat:=e;
end;
procedure gyors(ah,fh:integer);
var k:integer;
begin
if ah<fh then begin
k:=szetvalogat(ah,fh);
gyors(ah,k-1);
gyors(k+1,fh);
end;
end;
function min(a,b,e,u,c:integer):integer;
begin
if a>=b then if b>=c then min:=u
else if a>=c then min:=e
else min:=0;
if b>a then if a>=c then min:=e
else if b>=c then min:=u
else min:=0;
end;
function binker(e,u:integer):integer;
var k,x:integer;
begin
if e>u then binker:=0
else begin
k:=(e+u) div 2;
c:=a-b;

if (e=u) or (u-e=1) then begin x:=min(et[e],et[u],e,u,c);binker:=x;exit;end;

if (e<>u)and(u-e<>1) then begin
if et[k]>c then binker:=binker(e,k-1)
else if et[k]=c then binker:=k;
end
else if (e<>u)and(u-e<>1) and (et[k]<c) then binker:=binker(k+1,u);

end;
end;

begin
Assign(f,'nrtri.in');
reset(f);
for i:=1 to n do
close(f);
gyors(1,n);
db:=0;
i:=n;
while i>2 do begin
a:=et[i];
j:=i-1;
while j>1 do begin
b:=et[j];
k:=binker(1,j-1);
if k<>0 then db:=db+(j-k);
j:=j-1;
end;
i:=i-1;
end;
assign(f,'nrtri.out');
rewrite(f);
writeln(f,db);
close(f)
end.``````