Cod sursa(job #41124)

Utilizator petrecgClinciu Glisca Petre petrecg Data 27 martie 2007 23:04:17
Problema Numarare triunghiuri Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.68 kb
var f:text;i,j,l,x,y,s,n,z:longint;a:array[1..1000]of longint;

begin
 assign(f,'nrtri.in');reset(f);read(f,n);
 for i:=1 to n do read(f,a[i]);close(f);
 for i:=1 to n-1 do for j:=i+1 to n do
   if a[i]>a[j] then begin x:=a[i];a[i]:=a[j];a[j]:=x;end;
 a[n+1]:=(a[n]+a[n-1])*2;
 for i:=1 to n-2 do for j:=i+1 to n-1 do
  begin
   x:=j+1;y:=n;l:=j;z:=0;
   while(x<=y)do
    begin
     l:=(x+y)div 2;
     if(a[l]<=a[i]+a[j])and(a[l+1]>a[i]+a[j])then begin z:=1;break;end;
     if(a[l]<=a[i]+a[j])and(a[l+1]<=a[i]+a[j])
      then x:=l+1 else if x<y then y:=l else break;
    end;
   if z=1 then s:=s+l-j;
  end;
 assign(f,'nrtri.out');rewrite(f);write(f,s);close(f);
end.