Cod sursa(job #604253)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 21 iulie 2011 13:08:01
Problema Numarare triunghiuri Scor 80
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.04 kb
Program nr_triunghiuri;
 var n,i,j,l,s:integer;
     a:array [1..800] of integer;
     b1,b2: array [1..1 shl 16] of char;
     k:longint;
     fi,fo:text;
 procedure qsort(l,r:integer);
 var k,i,j,y:integer;
 begin
  i:=l; j:=r;
   k:=a[(l+r) div 2];
 repeat
  while a[i]<k do inc(i);
   while a[j]>k do dec(j);
 if i<=j then
              begin
               y:=a[i];
                a[i]:=a[j];
                  a[j]:=y;
                     inc(i); dec(j);
              end;
 until i>=j;
  if l<j then qsort(l,j);
   if i<r then qsort(i,r);
  end;
begin
assign(fi,'nrtri.in');
 assign(fo,'nrtri.out');
 settextbuf(fi,b1);
 settextbuf(fo,b2);
reset(fi);
 rewrite(fo);
readln(fi,n);
 for i:=1 to n do read(fi,a[i]);
 qsort(1,n);
 i:=1; s:=1;
 while i<=n-2 do begin
  j:=i+s;
   for l:=j+1 to n do
    if a[l]<=a[i]+a[j] then inc(k)
                         else break;
   inc(s);
  if j=n-1 then begin
                 inc(i);
                 s:=1;
                 end;
   end;
 write(fo,k);
close(fo);
end.